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;
mmddyy.
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.*/
Thank you!
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?
Does your dataset contain dates like 01/24/20, 03/12/20 ending with 20?
Here's one way to get 1920.
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
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?
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?
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.
Yes
Try this,
data have;
dt="01/24/13";
dt_new=input(dt,mmddyy8.);
format dt_new date9.;
run;
proc print data=have;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.