Create date variable from week, month, year

Solved
Occasional Contributor
Posts: 14

Create date variable from week, month, year

I have week, month, and year variables and need to create a date variable from these.  Suggestions on how to do this?

Accepted Solutions
Solution
‎06-13-2014 02:48 PM
Super User
Posts: 23,992

Re: Create date variable from week, month, year

Aligns to the beginning of the week;

date=mdy(1,1,year)+(week-1)*7;

OR

intnx('week', mdy(1,1, year), week, 'middle');

And look at the alignment options for intnx function, ie beginning end, middle.

SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition

All Replies
Super User
Posts: 23,992

Re: Create date variable from week, month, year

How does your week look, it is 25 indicating 25th week of the year or 2 indicating the 2nd week of a month?

Assuming its between 1 and 5

something like the following may work:

date=mdy(month, 7*(week-1)+1   , year);

Occasional Contributor
Posts: 14

Re: Create date variable from week, month, year

It is numbered 0 through 52, so 25 indicates the 25th week of the year.

Super User
Posts: 13,923

Re: Create date variable from week, month, year

Would you like the date to reflect any specific day of the week?

Occasional Contributor
Posts: 14

Re: Create date variable from week, month, year

Thursdays preferably, but if it is too difficult that way it doesn't particularly matter that much.

Super User
Posts: 23,992

Re: Create date variable from week, month, year

Weeks can cross months so having a specific day may not be possible

Occasional Contributor
Posts: 14

Re: Create date variable from week, month, year

Even just an approximate day would be ok.

e.g.

wk 1 = 1/1/14

wk 2 = 1/8/14

wk 3 = 1/15/14

etc.

Solution
‎06-13-2014 02:48 PM
Super User
Posts: 23,992

Re: Create date variable from week, month, year

Aligns to the beginning of the week;

date=mdy(1,1,year)+(week-1)*7;

OR

intnx('week', mdy(1,1, year), week, 'middle');

And look at the alignment options for intnx function, ie beginning end, middle.

SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition

🔒 This topic is solved and locked.