University of Rochester Medical Center
SearchDirectoryNewsEventsStrong HealthURMC Home

SAS Helpful Hints

Biostat Macro Library

Create a date from a string (mmddyy)

%Macro mkdate(achar,adate); * Convert a character(mmddyy) of length 6 or 8 to a date;
    If length(&achar)=6 Then Do;
        yearlen=2;
        * If year<=current year + 2 Then mellinium=2000. Else mellinium=1900;
        If 0+Substr(&achar,5,2)<=Mod(Year(Today()),100)+2 Then mell=2000; Else mell=1900;
    End; Else Do;
        yearlen=4; mell=0;
    End;
    &adate=Mdy(0+Substr(&achar,1,2),0+Substr(&achar,3,2),mell+Substr(&achar,5,yearlen));
    *insert the 15th of the month if the day is missing;
    If &adate<=.Z Then &adate=Mdy(0+Substr(&achar,1,2),15,mell+Substr(&achar,5,yearlen));
    If &adate<=.Z And Index(&achar,'U')>0 Then &adate=.U; *Unknown;
    If &adate<=.Z And Index(&achar,'M')>0 Then &adate=.M; *Missing;
%mend mkdate;

Data work.B;
    Set work.A;
    %mkdate(string,dob);
    Format dob date9.;
Run;

Please send your comments and suggestions about this web page to A. Watts (watts@bst.rochester.edu)