I have a text field with dates formatted as:
25 December 2014
What informat is appropriate for that string? I have tried ANYDTDTEw. to no avail.
If no informat is available, what is my best recourse?
National Language Support informat NLDATE. might also work in 9.3 when you specify that the date is in British format. Try this :
options locale=en_GB;
data test;
myDateStr = "25 December 2014";
format myDate yymmdd10.;
myDate = input (myDateStr, nldate21.);
run;
options locale=en_US;
proc print data=test; run;
PG
Works for me :
data test;
input myDate & :anydtdte21.;
format myDate yymmdd10.;
datalines;
25 December 2014
;
proc print data=test; run;
Don't forget the ampersand so that the string being converted is not merely "25" but "25 December 2014"
PG
You are correct, I don't believe there is such an informat. Here is one way to convert your date:
proc format;
invalue mon
'December'=12
;
run;
data test;
old_date='25 December 2014';
new_date=mdy(input(scan(old_date,2),mon.),input(scan(old_date,1),best.),input(scan(old_date,3),best.));
format new_date date9.;
put new_date=;
run;
Of course, you need to beef up the proc format to cover all 12 month and all of the possible variants (caps, short name etc.).
Haikuo
Oops, Guess PG is correct!
Thank you. I tried the approach recommended by PGstats, but I get a null value. The format/mdy(input()) combo works.
Post your code and/or data to figure out what is going wrong. - PG
I literally cut and pasted your code from above and ran it. The data set work.test has 1 observation but myDate is null (myDate = .).
The log gives no errors or warnings. I'm running SAS 9.3 TS Level 1M2 on Windows 64 bit.
I'm running 9.4 on Windows 32bits, but according to SAS there were no changes to SAS informats in version 9.4.
Try this:
data test;
myDateStr = "25 December 2014";
format myDate yymmdd10.;
myDate = input (myDateStr, anydtdte21.);
run;
proc print data=test; run;
Do you still get a null value?
Yep, still null. thanks for the 9.3 work around hiakuobian
PG,
I have both 9.3M2 and 9.4, it only works on 9.4. I would consider this as an upgrade.
Haikuo
Thanks. That settles it. - PG
Try converting the spaces to hyphens. That should work on earlier versions of SAS.
cdate='25 December 2014';
date=input(tranwrd(trim(cdate),' ','-'),ANYDTDTE20.);
National Language Support informat NLDATE. might also work in 9.3 when you specify that the date is in British format. Try this :
options locale=en_GB;
data test;
myDateStr = "25 December 2014";
format myDate yymmdd10.;
myDate = input (myDateStr, nldate21.);
run;
options locale=en_US;
proc print data=test; run;
PG
Thanks all. There are now a few workable solutions.
I'm using an infile specification in a datastep, so would prefer to use a date informat rather than reading in the data as a string, and then using input() to convert to a date.
PG, the NLDATE informat does indeed work in the GB locale. Thanks!
data test;
myDateStr = "25 December 2014";
myDate = input(catt(scan(mydatestr,1),substr(scan(mydatestr,2),1,3),scan(mydatestr,3)), date9.);
format myDate WORDDATX.;
run;
proc print data=test;
run;
Another way :
data test; myDateStr = "25 December 2014"; format myDate yymmdd10.; myDate=input(prxchange('s/\s+(\w\w\w)\w+\s+/$1/o',-1,myDateStr),date11.); run;
Xia Keshan
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.