BookmarkSubscribeRSS Feed
Mike_Davis
Fluorite | Level 6

Hello everyone,

I have a dataset with a character variable whose name is date,it looks like this:  April 2000.

I want to convert this variable to a standard numerical SAS date variable  like 04/01/2000  or '01Apr2000'd.

please advise.

Thanks!

Mike

data have;

length date $15.;

date='April 2000';output;

date='March 2001';output;

run;

5 REPLIES 5
Mike_Davis
Fluorite | Level 6

Please give me some advise,

thanks!

Mike

data need(keep=date date2 );

set have;

month=scan(date,1);

year=scan(date,2);

temp1=cats('01',substr(month,1,3),year);

date2=input(temp1,date9.);

format date2 mmddyys10.;

run;

Linlin
Lapis Lazuli | Level 10

ata have;

length date $15.;

date='April 2000';output;

date='March 2001';output;

run;

data want;

set have;

newdate=input(cats('01',substr(date,1,3),scan(date,2)),date9.);

format newdate mmddyy10.;

proc print;run;

Astounding
PROC Star

Mike,

The posted solutions look fine.

This is a related efficiency trick.  If you want to go with the version that you posted that creates MONTH and YEAR, you can cut out a couple of functions by using this code:

length month $ 3;

month = date;

Since there is only room to store 3 characters, you get the first 3.

Good luck.

robby_beum
Quartz | Level 8

You could also use the MDY() function to convert stand alone values into a numeric date:

data want;

   format new_date MMDDYY10.;

   set have;

   /* Substr the month and year. Might have to use a PROC FORMAT

       to convert the character month name into a 1,2,3,4...*/

   mnth = substr(...);

   yr = substr(...);

   new_date = MDY(mnth, 1, year);

run;

Keith
Obsidian | Level 7

Another option is to create an informat which is then used to convert the data.  The following code creates informats for all months from Jan 2000 - Dec 2012, obviously you can easily increase this range in the loop.

proc format;

picture dt_pic low-high='%B %Y' (datatype=date);

run;

data dt_infmt;

retain fmtname 'dt_in' type 'J';

do i=0 to 155;

  label=intnx('month','01jan2000'd,i);

  start=strip(put(label,dt_pic15.));

  output;

end;

run;

proc format cntlin=dt_infmt;

run;

data want;

set have;

new_date=input(date,dt_in.);

format new_date date9.;

run;

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1995 views
  • 0 likes
  • 5 in conversation