Hi, everyone
If I have a data format "20180825" for the Date column, how do I find the week number of the month for that date (i.e. the fourth week of Aug in 2018)? I need your help for this question ASAP. Thanks a lot.
That can be fixed. You need to treat the date as a valid SAS date, not as an eight-digit number. Unfortunately, it gets a little complex, so I'll break it out as two separate statements:
data want;
set have;
true_sasdate = input(put(datevar, z8.), yymmdd8.);
weeknum = ceil( day(true_sasdate) / 7);
run;
data have;
input date:anydtdte.;
week=intck('week',intnx('month',date,0),date)+1;
format date date9.;
cards;
20180825
;
If you need the answer ASAP, you should provide the rest of the relevant information.
Is your date variable an eight-digit number, or is it an eight-digit character string, or is it already a SAS date?
What is your definition of a week? Are the first seven days of the month all in week 1? Even if the first of the month is Wednesday?
Depending on your answers, here is a formula that might come in handy:
weeknum = ceil(day(datevar) / 7);
@Astounding Sir, elegance personified. Very neat!
Hi, Astounding
Thank you for your reply.
Yes, I think it's a eight-digit number. I don't have definite definition for the week number of a month. It would be fine if the program can help to show the sequence of a week within a month. BTW, I got the following message by implementing your suggested code:
NOTE: Invalid argument to function DAY(20021030) at line 1998 column 33.
If you have any suggestion, please let me know. Thank you very much.
That can be fixed. You need to treat the date as a valid SAS date, not as an eight-digit number. Unfortunately, it gets a little complex, so I'll break it out as two separate statements:
data want;
set have;
true_sasdate = input(put(datevar, z8.), yymmdd8.);
weeknum = ceil( day(true_sasdate) / 7);
run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.