BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
lizzy28
Quartz | Level 8

Hi all,

This might be a simple question: I have week and year information, how can I get the corresponding month?

Thanks a lot!

Lizi

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

If you want a date, it being the first date in the given week then you can do something like:

data have;

  year='2013'; week=1; output;

  year='2013'; week=2; output;

  year='2013'; week=3; output;

run;

data want;

  set have;

  if week=1 then new_date=input("01JAN"||strip(year),date9.);

  else new_date=input("01JAN"||strip(year),date9.) + ((week-1) * 7);

  format new_date date9.;

run;

Rembering that a date variable is the number of days since 01Jan1960, so work out the year as 01JAN, gives you the number for that day, then add the week number less 1 (as we want start) multiplied by 7.

View solution in original post

8 REPLIES 8
Quentin
Super User

Can you give a few examples of values you have?  Are they like 201501 for first week of 2015?  201552 for fifty-second week of 2015?  Or some other format?  Also, might need to define what day is the start of the week, etc.

lizzy28
Quartz | Level 8

Thanks, Quentin.

It was basically two columns:

year week

2013   1

2013   2

...

2013  52

2014   1

2014   2

...

2014  52.

MadhuKorni
Quartz | Level 8

data Want;

input Year week;

Mnt_Name=put(input(cat(0101,Year),ddmmyy10.)+(Week-1)*7,monname3.);

Mnt_Num=Month(input(cat(0101,Year),ddmmyy10.)+(Week-1)*7);

cards;

2013 1

2013 2

2013 16

2013 52

2014 1

2014 2

2014 52

;

proc print;run;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

If you want a date, it being the first date in the given week then you can do something like:

data have;

  year='2013'; week=1; output;

  year='2013'; week=2; output;

  year='2013'; week=3; output;

run;

data want;

  set have;

  if week=1 then new_date=input("01JAN"||strip(year),date9.);

  else new_date=input("01JAN"||strip(year),date9.) + ((week-1) * 7);

  format new_date date9.;

run;

Rembering that a date variable is the number of days since 01Jan1960, so work out the year as 01JAN, gives you the number for that day, then add the week number less 1 (as we want start) multiplied by 7.

Quentin
Super User

What would 2015 week 1 mean?  Jan 1, 2015 was a Thursday.  So what is the corresponding month for week 1, January?

What would be the first week number of 2015 that would map to February?  What's the rule that says which month a year-week number should map to?

arodriguez
Lapis Lazuli | Level 10

Hi Liziwu,

You must specify what do you need, a desired output maybe.

What do you need, the month at the begining of the week, at the end, the week is in the month if there are 4 or more days on it?

slchen
Lapis Lazuli | Level 10

data Want;

input Year week;

month=month(intnx('week',mdy(1,1,year),week));;

cards;

2013 1

2013 2

2013 16

2013 52

2014 1

2014 2

2014 52

;

Ksharp
Super User

Check informat  WEEKU. WEEKV. WEEKW. 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 8 replies
  • 2308 views
  • 6 likes
  • 7 in conversation