Return to Archive

SAS Tip of the Month
July 2009

If I have a character date of '6JUN2009', will this be read the same way as a character date of '06JUN2009'? This was a real life question, the simple answer being 'yes', as the following code demonstrates:

   data _null_;
      txtdate1='6JUN2009';
      numdate1=input(txtdate1,date9.);
      put 'Test Date 1: ' txtdate1= numdate1= date9.;
      txtdate2='06JUN2009';
      numdate2=input(txtdate2,date9.);
      put 'Test Date 2: ' txtdate2= numdate2= date9.;
   run;

   Test Date 1: txtdate1=6JUN2009 numdate1=06JUN2009
   Test Date 2: txtdate2=06JUN2009 numdate2=06JUN2009

This question actually come up when partial date variables (this is where the day, month and year for a particular event are in three separate variables) existed in a dataset and a date is constructed from this. If the three variables are numeric then it is possible to use the MDY function directly, as the following code fragment suggests:

   StartDate=MDY(Start_Month,Start_Day,Start_Year);

But what if one or more of the three partial date variables are character? In this case the month variable is almost always character and has the first three letters for the month -- in this case we can certainly use an informat for the month, do a character to numeric conversion for the character variables, and then execute the function.

There is another better way though in this case, and that is to use a CATS function then an INPUT function with a format, as the following example demonstrates:

   StartDate=INPUT(CATS(Start_Day,Start_Month,Start_Year),DATE9.);

If there are any numeric to character conversions that occur then this should be dealt with inside the CATS function.

Now lets look at an example:

   DATA aedata0;
      INFILE cards;
      INPUT subjid $ 1-4 event $ 6-20 start_d $ 22-23 start_m $ 25-27 start_y $ 29-32;
      ATTRIB start_dt LENGTH=8 FORMAT=date9.;
      start_dt=INPUT(CATS(start_d,start_m,start_y),date9.);
      PUT (_ALL_) (=);
   CARDS;
   0123 HEADACHE        13 MAR 2009
   0123 FEVER           01 APR 2009
   0123 NAUSEA          8  APR 2009
   ;
   RUN;

When the code is run the following appeared in the SAS LOG:

   69   DATA aedata0;
   70      INFILE cards;
   71      INPUT subjid $ 1-4 event $ 6-20 start_d $ 22-23 start_m $ 25-27 start_y $ 29-32;
   72      ATTRIB start_dt LENGTH=8 FORMAT=date9.;
   73      start_dt=INPUT(CATS(start_d,start_m,start_y),date9.);
   74      PUT (_ALL_) (=);
   75   CARDS;

   subjid=0123 event=HEADACHE start_d=13 start_m=MAR start_y=2009 start_dt=13MAR2009
   subjid=0123 event=FEVER start_d=01 start_m=APR start_y=2009 start_dt=01APR2009
   subjid=0123 event=NAUSEA start_d=8 start_m=APR start_y=2009 start_dt=08APR2009
   NOTE: The data set WORK.AEDATA0 has 3 observations and 6 variables.

   79   ;
   80   RUN;

Even though the START_D variable had both a zero led value (observation 2) and a single value (observation 3), SAS still worked out the correct START_DT value.

________________________________
Updated July 1, 2009