DATA Step, Macro, Functions and more

How to create a macro date9 variable that's one day less than another macro date9 variable?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 89
Accepted Solution

How to create a macro date9 variable that's one day less than another macro date9 variable?

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? Smiley Happy

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


Accepted Solutions
Solution
‎12-03-2012 05:05 PM
Super Contributor
Posts: 356

Re: How to create a macro date9 variable that's one day less than another macro date9 variable?

Posted in reply to TurnTheBacon

One way

%let date1 = 30NOV2012;

%let date2 = %sysfunc(putn("&date1"d - 1,date9.));

%put &date1;

%put &date2;

View solution in original post


All Replies
Super User
Posts: 5,516

Re: How to create a macro date9 variable that's one day less than another macro date9 variable?

Posted in reply to TurnTheBacon

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.

Solution
‎12-03-2012 05:05 PM
Super Contributor
Posts: 356

Re: How to create a macro date9 variable that's one day less than another macro date9 variable?

Posted in reply to TurnTheBacon

One way

%let date1 = 30NOV2012;

%let date2 = %sysfunc(putn("&date1"d - 1,date9.));

%put &date1;

%put &date2;

Super User
Posts: 5,516

Re: How to create a macro date9 variable that's one day less than another macro date9 variable?

Posted in reply to twocanbazza

SImpler than mine, and it works.  I'll vote for Barry's solution!

Regular Contributor
Posts: 241

Re: How to create a macro date9 variable that's one day less than another macro date9 variable?

Posted in reply to TurnTheBacon

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

🔒 This topic is solved and locked.

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

Discussion stats
  • 4 replies
  • 1301 views
  • 6 likes
  • 4 in conversation