I have a macrovariable called &date_today. It has the date9 format, and appears as 23NOV2012. I need to set up a second macrovariable called &date_yesterday, using a %let statement. It should be based on &date_today but subtract one day from it, so that it becomes 22NOV2012.
I've spent two hours looking for a way to accomplish this, and tried numerous approaches, yet it never seems to work. It's terribly annoying, because it's such a simple thing to accomplish in a DATA step. This is the very simple gist of what I want to do: %let &date_yesterday = &date_now -1.
Can anyone please help me resolve this and end my frustrations?
P.S. I know how to set up &date_yesterday independently of the &date_today, using INTNX, but that's not what I need. The new variable must be the equivalent of "&date_today minus one day".
One way
%let date1 = 30NOV2012;
%let date2 = %sysfunc(putn("&date1"d - 1,date9.));
%put &date1;
%put &date2;
If you can use today() instead of &date_today, it becomes a little simpler. But here's a way to handle the problem as is:
%let date_yesterday = %sysfunc( putn( %sysfunc(inputn(&date_today,date9)) - 1, date9));
Note that %sysfunc does not permit PUT and INPUT functions, but requires you to switch to PUTN / PUTC, or INPUTN / INPUTC.
Good luck.
One way
%let date1 = 30NOV2012;
%let date2 = %sysfunc(putn("&date1"d - 1,date9.));
%put &date1;
%put &date2;
SImpler than mine, and it works. I'll vote for Barry's solution!
Wonder if you find these two macros of mine handy:
%*-- two utilities --*;
%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;
%*-- usage example --*;
%let date1 = 30nov2012;
%let barry = %sysfunc(putn("&date1"d-1, date9.));
%let chang = %num2date(%date2num(&date1) - 1, literal=0);
%put barry=&barry chang=&chang;
%*-- on log
barry=29NOV2012 chang=29NOV2012
--*;
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.
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.