BookmarkSubscribeRSS Feed
Dynamike
Calcite | Level 5

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?

 

Thanks in advance!

Mike

 

5 REPLIES 5
Reeza
Super User

What is the type and format for this variable?

ballardw
Super User

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.

 

Dynamike
Calcite | Level 5

Hi ballardw!

 

Thank you for your help.

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!

 

Tom
Super User Tom
Super User

@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.

Dynamike
Calcite | Level 5

Ah sorry.

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

 

Have a nice day!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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