BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
CP2
Pyrite | Level 9 CP2
Pyrite | Level 9

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

1 ACCEPTED SOLUTION

Accepted Solutions
Daniel-Santos
Obsidian | Level 7

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

3 REPLIES 3
Reeza
Super User

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;

 

 

 

art297
Opal | Level 21

You could just use:

 

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

 

HTH,

Art, CEO, AnalystFinder.com

 

Daniel-Santos
Obsidian | Level 7

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

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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