BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
KAZ
Obsidian | Level 7 KAZ
Obsidian | Level 7

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? 

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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

14 REPLIES 14
PGStats
Opal | Level 21

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
haikuobian
Fluorite | Level 6

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! 

KAZ
Obsidian | Level 7 KAZ
Obsidian | Level 7

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

PGStats
Opal | Level 21

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

PG
KAZ
Obsidian | Level 7 KAZ
Obsidian | Level 7

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.

PGStats
Opal | Level 21

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
KAZ
Obsidian | Level 7 KAZ
Obsidian | Level 7

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

haikuobian
Fluorite | Level 6

PG,

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

Haikuo

PGStats
Opal | Level 21

Thanks. That settles it. - PG

PG
Tom
Super User Tom
Super User

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

cdate='25 December 2014';

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

PGStats
Opal | Level 21

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
KAZ
Obsidian | Level 7 KAZ
Obsidian | Level 7

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!

stat_sas
Ammonite | Level 13

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;

Ksharp
Super User

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to connect to databases in SAS Viya

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.

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