## Convert numeric week of the year to date

Hi,

i have the following problem:

My data set consists of a variable,which consists of a numeric expression 'year plus week of the year' e.g. 201124 or 201334.

How can I convert this expression into a date variable?

Mike

5 REPLIES 5

## Re: Convert numeric week of the year to date

What is the type and format for this variable?

## Re: Convert numeric week of the year to date

There are three SAS supplied informats that will do this if the input string looks like yyyyWww where the W is exactly that, the letter W and the ww are the week number. The informats are WeekU WeekV and WeekW which have different details of first day of the week and where the first week of the year starts.

```data example;
x = '2013W34';
d1 = input(x,weeku7.);
d2 = input(x,weekv7.);
d3 = input(x,weekw7.);
put d1= date9. d2= date9. d3= date9.;
run;```

So if you can get the value into that format you have three choices but you should read the details on the informats to see which matches your "week" definition best.

## Re: Convert numeric week of the year to date

Hi ballardw!

I worked it out like this (i think it is not the easiest way, but it works (almost)):

Data Test;
set imp.SHT;
year=Substr(left(x),1,4);
week=Substr(left(x),5,2);
run;

Proc SQL;
Create Table Test2 as
Select distinct *, input(year,\$4.) || "W" || input(week,\$2.) as x2
from test
group by Pseudonym;
Quit;

data Test3;
Set Test2;
d1 = input(x2,weeku7.);
d2 = input(x2,weekv7.);
d3 = input(x2,weekw7.);
put d1= date9. d2= date9. d3= date9.;
run;

Now i can see in the log file:

e.g. for 2011W39

d1=25SEP2011 d2=26SEP2011 d3=26SEP2011 (which should be the right expression)

But in the data set it is still shown as

d1=18895 d2=18896 d3=18896

What could be the reason for this?

Right expression in the log file, but still numeric expression in the data set?

Thanks a lot!

## Re: Convert numeric week of the year to date

@Dynamike wrote:

Now i can see in the log file:

e.g. for 2011W39

d1=25SEP2011 d2=26SEP2011 d3=26SEP2011 (which should be the right expression)

But in the data set it is still shown as

d1=18895 d2=18896 d3=18896

What could be the reason for this?

Right expression in the log file, but still numeric expression in the data set?

Thanks a lot!

If you don't attach a format to a number then SAS will use BEST12 format display it most situations.

If you want your date values to print using DATE9 format by default then add a FORMAT statement to your data step to permanently attach the format.

## Re: Convert numeric week of the year to date

Ah sorry.

It works with "format d1=date9." instead of "put d1=date9."

Have a nice day!

Discussion stats
• 5 replies
• 3433 views
• 0 likes
• 4 in conversation