🔒 This topic is solved and locked.
Need further help from the community? Please
sign in and ask a new question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Posted 07-29-2021 06:06 AM
(5015 views)
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
1 ACCEPTED SOLUTION
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
7 REPLIES 7
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I want to create something dynamic .
Input i have is only year like "2021"
I want to create coloums which will consist values
JAN2021 29/01/2021
FEB2021 26/02/2021
MAR2021 26/03/2021
APR2021 30/04/2021
MAY2021 28/05/2021
Input i have is only year like "2021"
I want to create coloums which will consist values
JAN2021 29/01/2021
FEB2021 26/02/2021
MAR2021 26/03/2021
APR2021 30/04/2021
MAY2021 28/05/2021
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
My fault, over-read that you want last Friday, thought last of the month.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Sure. I dont have data as such. the requirement is i have only year value that is "2021" and i want to code to get date values for last Friday of each month of year 2021.
I will try to replicate the same with data.not sure whether it explains the requirement clearly or not
Have data set
Year
2021
Want data set
want
JAN2021 29/01/2021
FEB2021 26/02/2021
MAR2021 26/03/2021
APR2021 30/04/2021
MAY2021 28/05/2021
this will go till DEC2021 and date value of last Friday of December
I will try to replicate the same with data.not sure whether it explains the requirement clearly or not
Have data set
Year
2021
Want data set
want
JAN2021 29/01/2021
FEB2021 26/02/2021
MAR2021 26/03/2021
APR2021 30/04/2021
MAY2021 28/05/2021
this will go till DEC2021 and date value of last Friday of December
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I should follow my own Maxims 😉
Maxim 9: There Is a Function for It.