Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SAS chaning char variables to date

Status
Not open for further replies.

lulumohca

Programmer
May 8, 2008
8
US
Hi everyone, was hoping someone can help me with this in SAS:

I have data in SAS like this:

Obs Date Item1 Item2 Item3
1 _00853 1 3 5
2 _00901 124 24 2324
3 _00902 24 12 232
...
...
...
x _00952 135 235 345

How would I make the variables in Date column to SAS date variables? these columns stand for weeks (_00853 is the last week of 2008, _00901 is the first week of 2009)?

The best would be able to make date column have 02JAN2009 (week ending date)then next row to have 09JAN2009:

Obs Date Item1 Item2 Item3
1 02JAN2009 1 3 5
2 09JAN2009 124 24 2324
...

I have tried changing the formats or creating an array? But I am having no luck.

Thank you.
 
Hi Lulumohca,
this was a fun little brain teaser.
I've got it I think pretty much all the way there, but I'm not sure of exactly what date you want returned.
Tyr running the code below and have a play with the results to tweak it to fit your requirements.
I've made an assumption concerning the year part of the original date field and decided that if it is over 500 it's part of the 1000's, and otherwise it's part of the 2000's. You may want to tweak this depending on what the date is used for.
Once you've got the process returning the date that you want for all scenarios, you can always incorporate the various parts of the working out into 1 single line of code.
Code:
data _null_;
  date = '_00853';
  week = input(substr(date,5,2),2.);
  yr1 = input(substr(date,2,3),3.);

  if yr1 < 500 then year = 2000 + yr1;
  else year = 1000 + yr1;

  first = mdy(1,1,year);

  format new_Date ddmmyy10.;
  new_Date = intnx('WEEK',first,week) -1;

  put date=;
  put week=;
  put yr1=;
  put year=;
  put new_date=;
run;

I hope that this helps you.
Chris.

Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top