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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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