×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

SAS chaning char variables to date

SAS chaning char variables to date

SAS chaning char variables to date

(OP)
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.

RE: SAS chaning char variables to date

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close