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

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

1 ACCEPTED SOLUTION

Accepted Solutions
twocanbazza
Quartz | Level 8

One way

%let date1 = 30NOV2012;

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

%put &date1;

%put &date2;

View solution in original post

4 REPLIES 4
Astounding
PROC Star

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.

twocanbazza
Quartz | Level 8

One way

%let date1 = 30NOV2012;

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

%put &date1;

%put &date2;

Astounding
PROC Star

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

chang_y_chung_hotmail_com
Obsidian | Level 7

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

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
  • 4 replies
  • 3788 views
  • 6 likes
  • 4 in conversation