Announcement

Collapse
No announcement yet.

Character Date Macros

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Character Date Macros

    Character Date Macros

    Here are two macros that can be used to convert character date values into numeric SAS dates. These macros can be added to a global, source or study level macro library and then used within COVER, Report or Export SAS code to convert character date fields into a numeric SAS date value. All macro code is attached to this post. This code has not been formally tested.

    Macro %DATE();
    This macro will convert 3 character date fields, Month, Day and Year, into one numeric date field using the mmddyy10. informat. The macro takes 4 named parameters as follows:
    • m = 2 char month variable
    • d = 2 char day variable
    • y = 2 or 4 char year variable
    • numdate = variable for numeric date value


    Example Call
    Say we want to populate the numeric date field BIRTHDT from the three character fields, BIRTHDTM, BIRTHDTD and BIRTHDTY. The macro call would be as follows:
    Code:
    data demog;
       set demog;
       %date(m=birthdtm, d=birthdtd, y=birthdty, numdate=birthdt);
    run;
    Code:
    ****************************************************************
    *
    * Date Macro
    *
    * Purpose: Converts character date values to a valid 
    *          SAS Date value. Checks that all values are 
    *          present.
    * 
    * Named parameters:
    *   m = char month variable
    *   d = char day variable
    *   y = char year variable
    *   numdate = variable for numeric date value
    ****************************************************************;
    %macro date(m=,d=,y=,numdate=);
    	if (&m > '' and &d >'' and &y >'') then do;
    		if compress(&m,'0123456789')='' then do;
    			if compress(&d,'0123456789')='' then do;
    				if compress(&y,'0123456789')='' then do;
    					&numdate=input(compress(&m||'/'||&d||'/'||&y),mmddyy10.);
    				end;
    			end;
    		end;
    	end;
    %mend date;
    Macro %DATEW();
    This macro will convert 3 character date fields, Month, Day and Year, into one numeric date field using the date9. format. The macro takes 4 named parameters as follows:
    • m = 3 char month variable (word)
    • d = 2 char day variable
    • y = 4 char year variable
    • numdate = variable for numeric date value


    Example Call
    Say we want to populate the numeric date field BIRTHDT from the three character fields, BIRTHDTM, BIRTHDTD and BIRTHDTY. The macro call would be as follows:
    Code:
    data demog;
       set demog;
       %datew(m=birthdtm, d=birthdtd, y=birthdty, numdate=birthdt);
    run;
    Code:
    ****************************************************************
    *
    * Date Macro
    *
    * Purpose: Converts character date values to a valid 
    *          SAS Date value using word month and date9. informat. 
    *          Checks that all values are present.
    *          
    * Named parameters:
    *   m = 3 char month variable (word)
    *   d = 2 char day variable
    *   y = 4 char year variable
    *   numdate = variable for numeric date value
    ****************************************************************;
    %macro datew(m=,d=,y=,numdate=);
    	if (&m > '' and &d >'' and &y >'') then do;
    		if compress(&d,'0123456789')='' then do;
    	   		if &m > '' and upcase(&m) in 
    	   		('JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC')  then do;				
    				if compress(&y,'0123456789')='' then do;
    					&numdate=input(compress(&d||&m||&y),date9.);
    				end;
    			end;
    		end;
    	end;
    %mend datew;
    Attached Files
    Last edited by Ronni Rubenstein; 01-05-2007, 01:57 PM.
Working...
X