SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Aexor
Lapis Lazuli | Level 10

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
Kurt_Bremser
Super User

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;

View solution in original post

7 REPLIES 7
andreas_lds
Jade | Level 19

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.

Aexor
Lapis Lazuli | Level 10
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
Kurt_Bremser
Super User

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;
andreas_lds
Jade | Level 19

My fault, over-read that you want last Friday, thought last of the month.

 

Aexor
Lapis Lazuli | Level 10
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

FreelanceReinh
Jade | Level 19

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;

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 5016 views
  • 12 likes
  • 4 in conversation