to estimate the begining date of the week and its ending date is pretty straingh forward.
But how do we estimate the start and end dates of the month, if the week start on Saturday.
ex : 02JAN2016 - 29 jan2016
data demo;
format date weekdatx. sat_start_of_week sat_end_of_week begining_month_date ending_month_date date9.;
do date='01jan2016'd to '31mar2016'd;
sat_start_of_week=intnx('week.7',date,0,'b');
sat_end_of_week=intnx('week.7',date,0,'e');
begining_month_date='';
ending_month_date='';
output;
end;
run;
You need to use a shift interval since you want the week to start on Saturday.
Example:
data _null_;
x='24Oct2019'd;
e=intnx("week.7",x,0,"B");
put e=;
format e mmddyy9.;
run;
I am already able to find the start and the end date of a week if the first day is a saturday.
My question was how do we estimate the start and the end of the month?
data demo;
format date weekdatx. sat_start_of_week sat_end_of_week begining_month_date ending_month_date date9.;
do date='01jan2016'd to '31mar2016'd;
sat_start_of_week=intnx('week.7',date,0,'b');
sat_end_of_week=intnx('week.7',date,0,'e');
begining_month_date='';
ending_month_date='';
output;
end;
run;
so for January 2016, this month start on January 02, 2016 and end on January 29,2016
Same thing for the month of february: January 30, 2016 and end feb 26, 2016
We can see those min and max from the week date values.
My question is how do we calculate those month start and end values ?
What are YOUR rules for begin and end of month?
I haven't run into anything that has "month" start or end that depends on the day of the week. So you implement your rule(s) after defining them.
The only common rules involve the date to count weeks from the start of year because the day of the week a year starts will vary from year to year for some purposes hence the U, V and W parameters available for WEEK functions. But the Year still starts on 1 January. The months still start on the first day of the month.
There DATDIF and YRDIF functions that are available for certain financial things that use a "basis", in this case a parameter of the function, such as '30/360' that calculates difference between dates using a 30 day\interval and arbitrary 360 day year. But that function is for between two dates not changing start or end of existing calendar calendar elements like "start of month"
There is not a function that will do that directly you will have to take your rules and code each one, for example:
data test;
do i=0 to 100;
y=i*0.05;
x=i+0.01;
output;
end;
run;
data test;
set test end=last;
x_obs=7.01;
y_obs=0.35;
if last then reflabel='.3';
run;
data demo;
format date weekdatx. sat_start_of_week sat_end_of_week begining_month_date ending_month_date date9.;
do date='01jan2016'd to '31mar2016'd;
sat_start_of_week=intnx('week.7',date,0,'b');
sat_end_of_week=intnx('week.7',date,0,'e');
begining_month_date=intnx('month',date,0,'b');
ending_month_date=intnx('month',date,0,'e');
wdb=weekday(begining_month_date);
wde=weekday(ending_month_date);
if wdb =6 then begin_month=begining_month_date+1;
if wdb =2 then begin_month=begining_month_date-2;
if wde eq 1 then ending_month=ending_month_date-2;
if wde eq 2 then ending_month=ending_month_date-3;
output;
end;
drop begining_month_date ending_month_date;
run;
proc print;
format begin_month ending_month date.;
run;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Early bird rate extended! Save $200 when you sign up by March 31.
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.