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;
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;
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;
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.
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;
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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!
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.
Ready to level-up your skills? Choose your own adventure.