Suppose you want to determine if the visit dates in the PATIENTS data set are between June 1, 1998 and October 15, 1999. You can use a DATA step approach, much the same way you did when checking that numeric variables were within a specified range. The only difference is that the range boundaries have to be SAS dates. Let’s see how this works. Program 4-1 checks for dates in the specified range and ignores missing values.
LIBNAME CLEAN "C:CLEANING"; DATA _NULL_; TITLE "Dates before June 1, 1998 or after October 15, 1999"; FILE PRINT; SET CLEAN.PATIENTS(KEEP=VISIT PATNO); IF VISIT LT '01JUN1998'D AND VISIT NE . OR VISIT GT '15OCT1999'D THEN PUT PATNO= VISIT= MMDDYY10.; RUN; |
The key to this program is the use of the date constants (also called date literals) in the IF statement. If you want SAS to turn a date into a SAS date (the number of days from 1/1/1960), the dates must be written in this fashion. Date constants are written as a two-digit day, a three-character month name, and a two- or four-digit year, placed in single or double quotes, and followed by a lowercase or uppercase ‘D’. You also need to add a date format in the PUT statement so that the date will be printed in a standard date format. Output from Program 4-1 is shown next.
Dates before June 1, 1998 or after October 15, 1999 PATNO=XX5 VISIT=05/07/1998 PATNO=010 VISIT=10/19/1999 PATNO=003 VISIT=11/12/1999 PATNO=028 VISIT=03/28/1998 PATNO=029 VISIT=05/15/1998 |
3.128.168.87