4

I want to create a SAS macro which takes a literal date (eg. '31may2011'd) as parameter. Inside the macro I want to transform this into a SAS date value (eg. 18778).

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

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

I use SAS 9.1.3

TechnoCore
  • 1,394
  • 2
  • 16
  • 21

3 Answers3

3

This will work inside or outside of a macro. Don't forget %sysfunc() has a handy optional second parameter which will let you format the output value.

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

or

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

Cheers Rob

Robert Penridge
  • 8,424
  • 2
  • 34
  • 55
2

You can do it using the %sysfunc macro function.

%macro transLiteralDate2Value(literal=);  
  %put literal = &literal.;  
  %put sasdatavalue = %sysfunc(putn(&literal.,8.));
%mend;
%transLiteralDate2Value(literal='31may2011'd);
Laurent de Walick
  • 2,154
  • 14
  • 11
2

It is handy to have a pair of simple conversion macros like mine below. See also my sas-l posting.

%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;
Chang Chung
  • 2,307
  • 1
  • 17
  • 16
  • 1
    Or... as I suggested above. You could just learn to you the %sysfunc and intnx functions. Macros like these do not help you when you move from job to job or deal with other people's code so I think it's better to avoid them. Plus if everyone had their own macro wrappers for simple functions you would never be able to remember all of the macro names anyway. – Robert Penridge Apr 25 '11 at 23:01