Help using Base SAS procedures

convert date format

Reply
Regular Contributor
Posts: 222

convert date format

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;

Regular Contributor
Posts: 222

Re: convert date format

Posted in reply to Mike_Davis

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;

Super Contributor
Posts: 1,636

Re: convert date format

Posted in reply to Mike_Davis

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;

Super User
Posts: 5,503

Re: convert date format

Posted in reply to Mike_Davis

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.

Contributor
Posts: 73

Re: convert date format

Posted in reply to Astounding

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;

Regular Contributor
Posts: 151

Re: convert date format

Posted in reply to Mike_Davis

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;

Ask a Question
Discussion stats
  • 5 replies
  • 332 views
  • 0 likes
  • 5 in conversation