DATA Step, Macro, Functions and more

date format needed to convert 01/24/13 character date to sas date

Reply
Regular Contributor
Posts: 192

date format needed to convert 01/24/13 character date to sas date

I have character dates in this format:

for Jan 24, 2013:  01/24/13 so it's mm/dd/yy, etc....

I need to convert them to date9. format but this isn't working and I cannot find a comprehensive list of sas date formats though I searched sas documentation and google.

I'd like to use an array since I have to convert more than one date.

Any help you can give is much appreciated.

data dates;


set old;


array vaccines polio1 polio2 polio3 polio4 polio5 dtp1 dtp2 dtp3 dtp4 dtp5 mmr1 mmr2;


do over vaccines;

vaccines= input (vaccines, date9.);



end;

run;

Respected Advisor
Posts: 3,777

Re: date format needed to convert 01/24/13 character date to sas date

mmddyy.

Super User
Posts: 10,516

Re: date format needed to convert 01/24/13 character date to sas date

2 problems, you are trying to change variable from character to numeric, won't happen. You'll need to create a new variable to hold the numeric.

Second wrong informat.

trtL

array vaccines polio1 polio2 polio3 polio4 polio5 dtp1 dtp2 dtp3 dtp4 dtp5 mmr1 mmr2;

array nvac     npolio1 npolio2 npolio3 npolio4 npolio5 ndtp1 ndtp2 ndtp3 ndtp4 ndtp5 nmmr1 nmmr2;

do _i_ = 1 to dim(vaccines);

   nvac[_i_] = input(vaccines[_i_],mmddyy8.);

end;

format npolio1 npolio2 npolio3 npolio4 npolio5 ndtp1 ndtp2 ndtp3 ndtp4 ndtp5 nmmr1 nmmr2 date9.; /* sas dates are number offset from 1jan1960 so you use a Format to display them as you would like.*/      


Regular Contributor
Posts: 192

Re: date format needed to convert 01/24/13 character date to sas date

Thank you!

Regular Contributor
Posts: 192

Re: date format needed to convert 01/24/13 character date to sas date

When SAS formats the date using date9., the years are in the 1920s although the majority  of them are actually 2008.  The month and day are correct.   What am I doing wrong?

Trusted Advisor
Posts: 1,204

Re: date format needed to convert 01/24/13 character date to sas date

Does your dataset contain dates like 01/24/20, 03/12/20 ending with 20?

Respected Advisor
Posts: 3,777

Re: date format needed to convert 01/24/13 character date to sas date

Here's one way to get 1920.

data _null_;
   d=
'01/01/2014';
   y=input(d,
mmddyy8.);
   x=input(d,mmddyy10.);
   format y x date11.;
  
put _all_;
  
run;

d=
01/01/2014 y=01-JAN-1920 x=01-JAN-2014 _ERROR_=0 _N_=1
Trusted Advisor
Posts: 1,204

Re: date format needed to convert 01/24/13 character date to sas date

Thanks data_null_ for your input. As in original post date years' format is based on two digits. Informat mmddyy8. will produce 1920 for years ending with 20, because of yearcutoff range 1920 to 2019.

Naeem


Super User
Posts: 10,516

Re: date format needed to convert 01/24/13 character date to sas date

And

options yearcutoff = <4 digit year> ;

will change the default year to consider 2 digit years.

Of course since OP shouldn't have any valid dates in 2020 that should be acceptable. The question would be are any of the dates like 01/24/12 supposed to be from 1912. If so there will be a much greater challenge.

And didn't anyone learn, oh about 15 years ago, that 2 digit years are time bombs waiting to happen?

Regular Contributor
Posts: 192

Re: date format needed to convert 01/24/13 character date to sas date

Yes, the original MS Excel spreadsheet I am importing from was from someone that didn't follow directions (i.e., they were supposed to give a date formatted field, not a text field with a 2-digit month, day, and year).  All years are 07 through 14 (i.e, 2007 through 2014). I inserted options yearcutoff=1950 after reading SAS(R) 9.2 Language Reference: Concepts, Second Edition   but I ended up with all formatted sas years in 2020.  What am I missing?

Regular Contributor
Posts: 192

Re: date format needed to convert 01/24/13 character date to sas date

I actually need to convert the dates from  two-digit years (i.e, 07) to a sas date which is equivalent to  2007, then format.

Regular Contributor
Posts: 192

Re: date format needed to convert 01/24/13 character date to sas date

Yes

Trusted Advisor
Posts: 1,204

Re: date format needed to convert 01/24/13 character date to sas date

Try this,

data have;

dt="01/24/13";

dt_new=input(dt,mmddyy8.);

format dt_new date9.;

run;

proc print data=have;

run;

Ask a Question
Discussion stats
  • 12 replies
  • 1328 views
  • 0 likes
  • 4 in conversation