SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

Help with date informats

Accepted Solution Solved
Reply
Contributor KAZ
Contributor
Posts: 25
Accepted Solution

Help with date informats

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? 


Accepted Solutions
Solution
‎07-29-2014 09:43 PM
Respected Advisor
Posts: 4,920

Re: Help with date informats

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

PG

View solution in original post


All Replies
Respected Advisor
Posts: 4,920

Re: Help with date informats

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

PG
Occasional Contributor
Posts: 17

Re: Help with date informats

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! 

Contributor KAZ
Contributor
Posts: 25

Re: Help with date informats

Posted in reply to haikuobian

Thank you.  I tried the approach recommended by PGstats, but I get a null value.  The format/mdy(input()) combo works.

Respected Advisor
Posts: 4,920

Re: Help with date informats

Post your code and/or data to figure out what is going wrong. - PG

PG
Contributor KAZ
Contributor
Posts: 25

Re: Help with date informats

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.

Respected Advisor
Posts: 4,920

Re: Help with date informats

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?

PG
Contributor KAZ
Contributor
Posts: 25

Re: Help with date informats

Yep, still null.  thanks for the 9.3 work around hiakuobian

Occasional Contributor
Posts: 17

Re: Help with date informats

PG,

I have both 9.3M2 and 9.4, it only works on 9.4. I would consider this as an upgrade.

Haikuo

Respected Advisor
Posts: 4,920

Re: Help with date informats

Posted in reply to haikuobian

Thanks. That settles it. - PG

PG
Super User
Super User
Posts: 7,042

Re: Help with date informats

Try converting the spaces to hyphens.  That should work on earlier versions of SAS.

cdate='25 December 2014';

date=input(tranwrd(trim(cdate),' ','-'),ANYDTDTE20.);

Solution
‎07-29-2014 09:43 PM
Respected Advisor
Posts: 4,920

Re: Help with date informats

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

PG
Contributor KAZ
Contributor
Posts: 25

Re: Help with date informats

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!

Trusted Advisor
Posts: 1,228

Re: Help with date informats

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;

Super User
Posts: 10,028

Re: Help with date informats

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

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 14 replies
  • 740 views
  • 3 likes
  • 6 in conversation