12-03-2012 04:32 PM
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".
12-03-2012 05:00 PM
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.
12-05-2012 01:14 PM
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 --*;
%macro num2date(num, format=date10., literal=1);
%let n = %sysfunc(putn(&num,&format));
%if &literal %then "&n"d; %else &n;
%*-- 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
Need further help from the community? Please ask a new question.