BookmarkSubscribeRSS Feed
hdg
Obsidian | Level 7 hdg
Obsidian | Level 7

Hello I have a date issue, Given I have a macro variable

%let startdate = "30apr2012"d;

I want to find the last friday before that start date so example

it should be a macrovariable called myfriday= "27apr2012"d

Thanks for your help


3 REPLIES 3
ballardw
Super User

Maybe not elegant but this seems to work:

%let mydate= %sysfunc(putn(&startdate,f10.)); /* need a numeric value to do arithmetic below*/

%let myfriday = %eval(%sysfunc(weekday(&startdate)) *(-1) -1 + &mydate);

%let myfriday = "%sysfunc(putn(&myfriday,date9.))"d;  /* two lines for readability, might get this to work nesting functions*/

Patrick
Opal | Level 21

Code:

%let startdate = "30apr2012"d;

%let myfriday="%sysfunc(intnx(week.6,&startdate,0,b),date9.)"d;

%put myfriday: &myfriday;

Log:

15         %let startdate = "30apr2012"d;

16         %let myfriday="%sysfunc(intnx(week.6,&startdate,0,b),date9.)"d;

17         %put myfriday: &myfriday;

myfriday: "27APR2012"d

dbiehle
Calcite | Level 5

I think the only caveat is if hdg wants the previous Friday even when the date given is a Friday. Then the code would just need a -0.5 added to the date:

Code:

%LET startdate = "27apr2012"d;

%LET myfriday="%SYSFUNC(INTNX(WEEK.6,&startdate.-0.5,0),DATE9.)"d;

%PUT myfriday= &myfriday.;

Log:

15  %LET startdate = "27apr2012"d;

16  %LET myfriday="%SYSFUNC(INTNX(WEEK.6,&startdate.-0.5,0),DATE9.)"d;

17  %PUT myfriday= &myfriday.;

myfriday= "20APR2012"d

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

Register now

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 3 replies
  • 1149 views
  • 2 likes
  • 4 in conversation