B--rian
B--rian

Reputation: 5890

Converting MMM. DD, YY string to date9. in SAS?

Is there a way without regular expressions to convert e.g. myString = "Dec. 31, 18" into a integer suitable for date9.?

I found a PDF from lexjansen.com which lists many SAS date formats: Collection of SAS date formats To my understanding, I could do something like myDate = input("21Dec2018",date9.), but there is no SAS-format which matches my input string, so I am forced to perform some fancy regular expression first in order to convert myString to date9.. Is that correct?

A suitable regular expression would IMHO be s/([0-9]{3}). ([0-9]{1,2}), ([0-9]{2})/\2\120\3/, which collects the 3-letter-month in \1, the one-or-two-digit-day in \2, and the two-digit-year in \3 and rearranges it accordingly. My issue is now how to implement this regex in SAS with the correct escaping?

References

Upvotes: 0

Views: 1997

Answers (2)

Tom
Tom

Reputation: 51621

Regular expression are rarely worth the effort for such simple conversion. Just use SCAN().

data test;
  input datestr $20. ;
  date=input(cats(scan(datestr,2,'. ,')
                 ,scan(datestr,1,'. ,')
                 ,scan(datestr,2,','))
            ,date9.)
  ;
  format date date9.;
  put (_all_) (=);
cards;
Jan. 1,2020
;

Results:

datestr=Jan. 1,2020 date=01JAN2020

Upvotes: 1

data _null_
data _null_

Reputation: 9109

I think the technique described in the first example from this paper will be useful.

https://support.sas.com/resources/papers/proceedings12/245-2012.pdf

Upvotes: 2

Related Questions