BookmarkSubscribeRSS Feed
jcis7
Pyrite | Level 9

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;

12 REPLIES 12
ballardw
Super User

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.*/      


jcis7
Pyrite | Level 9

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?

stat_sas
Ammonite | Level 13

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

data_null__
Jade | Level 19

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
stat_sas
Ammonite | Level 13

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


ballardw
Super User

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?

jcis7
Pyrite | Level 9

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?

jcis7
Pyrite | Level 9

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.

stat_sas
Ammonite | Level 13

Try this,

data have;

dt="01/24/13";

dt_new=input(dt,mmddyy8.);

format dt_new date9.;

run;

proc print data=have;

run;

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 Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 12 replies
  • 3272 views
  • 0 likes
  • 4 in conversation