Convert a literal date parameter to a SAS date value in a macro

I want to create a SAS macro that takes a letter date (e.g. '31may2011'd) as a parameter. Inside the macro, I want to convert it to a SAS date value (e.g. 18778).

%macro transLiteralDate2Value(literal=); %put literal = &literal.; %put sasdatavalue = ???; /* how to calculate this value ? */ %mend; %transLiteralDate2Value(literal='31may2011'd); 

Is an elegant way to achieve this? Of course, I could do this by parsing a literal string, but I think there should be a better way.

I am using SAS 9.1.3

+4
source share
3 answers

You can do this using the% sysfunc macro.

 %macro transLiteralDate2Value(literal=); %put literal = &literal.; %put sasdatavalue = %sysfunc(putn(&literal.,8.)); %mend; %transLiteralDate2Value(literal='31may2011'd); 
+2
source

This will work inside or outside the macro. Do not forget that% sysfunc () has a convenient additional second parameter, which allows you to format the output value.

 %let report_date = %sysfunc(sum('01JAN2011'd),best.); 

or

 %let report_date = %sysfunc(putn('01JAN2011'd,best.)); 

Cheers Rob

+3
source

It’s convenient to have a couple of simple conversion macros like mine. See also my sas-l posts .

 %macro date2num(date, informat=anydtdte.); %*-- strip quotations and postfix d from date literal if any. --*; %*-- quotations are intentionally doubled to prevent unmatched error --*; %let date=%sysfunc(prxchange(s/[''""]d?//i,-1,&date)); %sysfunc(inputn(&date,&informat)) %mend date2num; %macro num2date(num, format=date10., literal=1); %local n; %let n = %sysfunc(putn(&num,&format)); %if &literal %then "&n"d; %else &n; %mend num2date; 
+2
source

All Articles