In my sample of data, I have firm, year, month and price variables. I would like to create a new variable with year-month-day in any sas format, where day is the last business day (or last day) of the given year month. Can anyone help me on it plz? Thanks
data tmp1;
input year firm$ month$ price;
datalines;
2004 AE0006 m1 8.2
2004 AE0006 m2 8.5
2004 AE0006 m3 8.2
2004 AE0006 m4 8.3
2004 AE0006 m5 8.4
2004 AE0006 m6 8.5
2004 AE0006 m7 8.6
2004 AE0006 m8 8.7
2004 AE0006 m9 8.8
2004 AE0006 m10 8.9
2004 AE0006 m11 9
2004 AE0006 m12 9.1
2005 AE0006 m1 9.2
2005 AE0006 m2 9.3
2005 AE0006 m3 9.4
2005 AE0006 m4 9.5
2005 AE0006 m5 9.6
2005 AE0006 m6 9.7
2005 AE0006 m7 9.8
2005 AE0006 m8 9.9
2005 AE0006 m9 10
2005 AE0006 m10 10.1
2005 AE0006 m11 10.2
2005 AE0006 m12 10.3
run;
Here is what I expect:
date price
31-Jan-2004 8.2
29-Feb-2004 8.5
31-Mar-2004 8.2
30-Apr-2004 8.3
31-May-2004 8.4
30-Jun-2004 8.5
31-Jul-2004 8.6
31-Aug-2004 8.7
30-Sep-2004 8.8
31-Oct-2004 8.9
30-Nov-2004 9
31-Dec-2004 9.1
31-Jan-2005 9.2
28-Feb-2005 9.3
31-Mar-2005 9.4
30-Apr-2005 9.5
31-May-2005 9.6
30-Jun-2005 9.7
31-Jul-2005 9.8
31-Aug-2005 9.9
30-Sep-2005 10
31-Oct-2005 10.1
30-Nov-2005 10.2
31-Dec-2005 10.3
Something like this?
data want;
set tmp1;
last_date=intnx('month',mdy(input(substr(month,2),2.),1,year),0,'E');
select(weekday(last_date));
when(1) last_working_day=last_date-2; /* Sunday */
when(7) last_working_day=last_date-1; /* Saturday */
otherwise last_working_day=last_date;
end;
format last_date last_working_day date9.;
run;
You wrote that you want the last business date - I put in a check for the weekday, and put the result in last_working_day. But you may have to check for some national holidays as well.
Something like this?
data want;
set tmp1;
last_date=intnx('month',mdy(input(substr(month,2),2.),1,year),0,'E');
select(weekday(last_date));
when(1) last_working_day=last_date-2; /* Sunday */
when(7) last_working_day=last_date-1; /* Saturday */
otherwise last_working_day=last_date;
end;
format last_date last_working_day date9.;
run;
You wrote that you want the last business date - I put in a check for the weekday, and put the result in last_working_day. But you may have to check for some national holidays as well.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.
Ready to level-up your skills? Choose your own adventure.