Help using Base SAS procedures

date issue

Reply
Frequent Contributor
Frequent Contributor
Posts: 79

date issue

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


Super User
Posts: 11,343

Re: date issue

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

Respected Advisor
Posts: 4,173

Re: date issue

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

Occasional Contributor
Posts: 9

Re: date issue

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

Ask a Question
Discussion stats
  • 3 replies
  • 216 views
  • 2 likes
  • 4 in conversation