## How to obtain month through week and year

Solved
Frequent Contributor
Posts: 110

# How to obtain month through week and year

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

Accepted Solutions
Solution
‎08-12-2015 09:24 AM
Super User
Posts: 9,599

## Re: How to obtain month through week and year

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.

All Replies
PROC Star
Posts: 1,457

## Re: How to obtain month through week and year

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.

Frequent Contributor
Posts: 110

## Re: How to obtain month through week and year

Thanks, Quentin.

It was basically two columns:

year week

2013   1

2013   2

...

2013  52

2014   1

2014   2

...

2014  52.

Frequent Contributor
Posts: 77

## Re: How to obtain month through week and year

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;

Solution
‎08-12-2015 09:24 AM
Super User
Posts: 9,599

## Re: How to obtain month through week and year

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.

PROC Star
Posts: 1,457

## Re: How to obtain month through week and year

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?

Frequent Contributor
Posts: 146

## Re: How to obtain month through week and year

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?

Super Contributor
Posts: 275

## Re: How to obtain month through week and year

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

;

Super User
Posts: 10,766

## Re: How to obtain month through week and year

Check informat  WEEKU. WEEKV. WEEKW.

🔒 This topic is solved and locked.