开发者

Transform literal date parameter to SAS date value in macro

开发者 https://www.devze.com 2023-02-28 00:29 出处:网络
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).

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 开发者_如何学Cway.

I use SAS 9.1.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


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


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

精彩评论

暂无评论...
验证码 换一张
取 消