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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 2 replies
  • 433 views
  • 1 like
  • 3 in conversation