- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data have;
input date:anydtdte.;
week=intck('week',intnx('month',date,0),date)+1;
format date date9.;
cards;
20180825
;
Jag
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@Astounding Sir, elegance personified. Very neat!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data have;
input date:anydtdte.;
weeknum = ceil(day(date) / 7);
format date date9.;
cards;
20021030
;
Jag
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
It works!
Yes, I have to transform the date to a valid SAS date first.
Thank you very much.