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

I want to create a macro variable which takes the date of the day before, and hence, its value changes each day. I am using the following code:

call symputx('PREVIOUS_DATE' , put(intnx('day', today(), -1, 's'), date9.));

But the problem is that if yesterday was a Sunday, then I would require the macro to take the date of the day before yesterday. For example today id 26APR2021, and yesterday 25APR2021 was a sunday, so I want this macro to take value 24APR2021.

 

Is there a way to automate this using an if clause or anything else?

1 ACCEPTED SOLUTION

Accepted Solutions
japelin
Rhodochrosite | Level 12

If you simply want to get the previous day if it is Sunday, you can use the WEEKDAY function.
If you need to take holidays into concern, please refer to the HOLIDAY function.(Only common holidays in the U.S. and Canada.)

 

data _null_;
  dt=intnx('day', today(), -1, 's');
  wk=weekday(dt);
  if wk=1 then dt=dt-1;
  call symputx('PREVIOUS_DATE' , put(dt, date9.));
run;

View solution in original post

2 REPLIES 2
japelin
Rhodochrosite | Level 12

If you simply want to get the previous day if it is Sunday, you can use the WEEKDAY function.
If you need to take holidays into concern, please refer to the HOLIDAY function.(Only common holidays in the U.S. and Canada.)

 

data _null_;
  dt=intnx('day', today(), -1, 's');
  wk=weekday(dt);
  if wk=1 then dt=dt-1;
  call symputx('PREVIOUS_DATE' , put(dt, date9.));
run;
Patrick
Opal | Level 21

Interval WEEKDAY<weekend days>W allows you to do this (with SAS the week starts Sunday so it's WEEKDAY17W). Documentation here.

 

data _null_;
  call symputx('dt',put(intnx('WEEKDAY17W',today(),-1),date9.));
  stop;
run;

%put &=dt;

Run above today on Monday April 26.

33         %put &=dt;
DT=23APR2021

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 666 views
  • 1 like
  • 3 in conversation