DATA Step, Macro, Functions and more

date macro variables

Accepted Solution Solved
Reply
Frequent Contributor
Frequent Contributor
Posts: 134
Accepted Solution

date macro variables

I am still having trouble with date tokens.  I have a date global macro variable and I want to compute another one that is 45 days earlier. I have tried both assigning with and without quotes and using %LET and call symput. I am using SAS 9.4. What am I doing wrong?

 

 

%LET pulldate = 11/29/2016 ;
/*%LET pulldate = '11/29/2016' ; */

Try 1:

data _null_ ;

%global earlyTerm ;
call symputX('earlyTerm' , %sysfunc(input(&pulldate., mmddyy10.)-45)) ;
run;


Try 2:

%LET earlyTerm = %sysfunc(input(&pulldate., mmddyy10.)-45))

%put &earlyterm. ;


Accepted Solutions
Solution
‎01-05-2017 09:50 AM
Contributor
Posts: 24

Re: date macro variables

[ Edited ]

You've got close on your second try.

 

If you really have to use the MM//DD/YYYY format for the PULLDATE var, this will do the trick:

 

* set;
%let pulldate = 11/29/2016 ;
%let earlyTerm = %eval(%sysfunc(inputn(&pulldate,mmddyy10.))-45);

* show;
%put &earlyterm.;
%put %sysfunc(putn(&earlyterm,mmddyy10.));

 

Use inputn/inputc or putn/putc with %sysfunc.

 

Some functions (like input/put) won't work with %sysfunc, more info here:

 

http://support.sas.com/documentation/cdl/en/mcrolref/69726/HTML/default/viewer.htm#p1o13d7wb2zfcnn19...

Also, to perform the calculation (-45) you need to force the expression for evaluation, hence the %eval macro functions.

 

More on %eval: http://support.sas.com/documentation/cdl/en/mcrolref/69726/HTML/default/viewer.htm#n07pr39df9k7m3n1w...

 

%sysevalf will do the trick, but is intended to be used with floating point arithmetic, which is not the case.

 

More on %sysevalf: http://support.sas.com/documentation/cdl/en/mcrolref/69726/HTML/default/viewer.htm#p1d9ypna2tpt16n1x...

 

Hope it helps.

 

Daniel Santos @ www.cgd.pt

View solution in original post


All Replies
Super User
Posts: 19,770

Re: date macro variables

You're close. You're mixing data step and macro logic. I recommend data step if you're not familiar with macro functions.

 

Changes;

  • remove %global
  • Add  third parameter of CALL SYMPUTX to specify global macro variable
  • Remove %sysfunc as not required
  • Add quotation marks around variable
  • Added version with a formatted date
%LET pulldate = 11/29/2016 ;
data _null_ ;
call symputX('earlyTerm' , input("&pulldate.", mmddyy10.)-45, 'g') ;
call symputX('earlyTermDate' , put(input("&pulldate.", mmddyy10.)-45, date9.), 'g') ;
run;

%put &earlyTerm;
%put &earlyTermDate;

 

 

 

PROC Star
Posts: 7,467

Re: date macro variables

You could just use:

 

%let pulldate = %sysevalf('29nov2016'd);
%let earlyTerm =%sysevalf(&pulldate.-45);

 

HTH,

Art, CEO, AnalystFinder.com

 

Solution
‎01-05-2017 09:50 AM
Contributor
Posts: 24

Re: date macro variables

[ Edited ]

You've got close on your second try.

 

If you really have to use the MM//DD/YYYY format for the PULLDATE var, this will do the trick:

 

* set;
%let pulldate = 11/29/2016 ;
%let earlyTerm = %eval(%sysfunc(inputn(&pulldate,mmddyy10.))-45);

* show;
%put &earlyterm.;
%put %sysfunc(putn(&earlyterm,mmddyy10.));

 

Use inputn/inputc or putn/putc with %sysfunc.

 

Some functions (like input/put) won't work with %sysfunc, more info here:

 

http://support.sas.com/documentation/cdl/en/mcrolref/69726/HTML/default/viewer.htm#p1o13d7wb2zfcnn19...

Also, to perform the calculation (-45) you need to force the expression for evaluation, hence the %eval macro functions.

 

More on %eval: http://support.sas.com/documentation/cdl/en/mcrolref/69726/HTML/default/viewer.htm#n07pr39df9k7m3n1w...

 

%sysevalf will do the trick, but is intended to be used with floating point arithmetic, which is not the case.

 

More on %sysevalf: http://support.sas.com/documentation/cdl/en/mcrolref/69726/HTML/default/viewer.htm#p1d9ypna2tpt16n1x...

 

Hope it helps.

 

Daniel Santos @ www.cgd.pt

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 227 views
  • 3 likes
  • 4 in conversation