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

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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