TechnoCore
TechnoCore

Reputation: 1404

Transform literal date parameter to SAS date value in macro

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

Upvotes: 4

Views: 4251

Answers (3)

Robert Penridge
Robert Penridge

Reputation: 8513

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

Upvotes: 3

Chang Chung
Chang Chung

Reputation: 2307

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;

Upvotes: 2

Laurent de Walick
Laurent de Walick

Reputation: 2174

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);

Upvotes: 2

Related Questions