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.
AndyTeng
Fluorite | Level 6

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.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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;

View solution in original post

7 REPLIES 7
Jagadishkatam
Amethyst | Level 16
data have;
input date:anydtdte.;
 week=intck('week',intnx('month',date,0),date)+1; 
format date date9.;
cards;
20180825
;
Thanks,
Jag
Astounding
PROC Star

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);

novinosrin
Tourmaline | Level 20

@Astounding Sir, elegance personified. Very neat!

AndyTeng
Fluorite | Level 6

 

 

 

 

Jagadishkatam
Amethyst | Level 16
Hi I tried the below and it worked with no issues

data have;
input date:anydtdte.;
weeknum = ceil(day(date) / 7);
format date date9.;
cards;
20021030
;
Thanks,
Jag
Astounding
PROC Star

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;

AndyTeng
Fluorite | Level 6
Hi, Astounding
It works!
Yes, I have to transform the date to a valid SAS date first.
Thank you very much.

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
  • 10163 views
  • 9 likes
  • 4 in conversation