Hi All,
Please help me below query .
I need date for last friday for every month
like
JAN2021 -> 29/01/2021
FEB2021 -> 26/02/2021
MAR2021 -> 26/03/2021
APR2021 -> 30/04/2021
MAY2021 -> 28/05/2021
and so on till DEC2021
See this:
%let year=2021;
data want;
month = "0101&year."d;
format
month yymmd7.
l_day yymmdd10.
;
do while (month le "31dec&year."d);
l_day = intnx('month',month,0,"e");
l_day = l_day - mod(weekday(l_day)+1,7);
weekday = weekday(l_day);
output;
month = intnx('month',month,1);
end;
run;
What exactly do you have at the beginning? A dataset with strings like "Jan2021" or SAS-dates formatted to show only month and year?
With strings:
data have_want;
format date eom date9.;
var = "JAN2021";
date = input(cats('1', quote(trim(var)), 'd'), date9.);
eom = intnx('month', date, 0, 'e');
run;
If you already have a sas-date, just skip calling input.
See this:
%let year=2021;
data want;
month = "0101&year."d;
format
month yymmd7.
l_day yymmdd10.
;
do while (month le "31dec&year."d);
l_day = intnx('month',month,0,"e");
l_day = l_day - mod(weekday(l_day)+1,7);
weekday = weekday(l_day);
output;
month = intnx('month',month,1);
end;
run;
My fault, over-read that you want last Friday, thought last of the month.
Hi @Aexor,
The NWKDOM function is ideal for calculations like this.
data have;
year=2021;
run;
data want(drop=y:);
set have;
do _n_=1 to 12;
month=intnx('month',mdy(1,1,year),_n_-1);
last_fri=nwkdom(5,6,_n_,year);
output;
end;
format m: monyy7. l: ddmmyy10.;
run;
I should follow my own Maxims 😉
Maxim 9: There Is a Function for It.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.