I have a date variable in character format, length 10, format and informat $10. Examples of the values are 1/5/2010, 12/24/2013, 10/3/2012 (note that month and day do NOT have leading zeros). I would like to convert this variable to date format, but can't seem to find an informat that will work with this. I'm guessing I need to do something like the following.
new_date = input(orig_date, [informat]);
Any ideas?
Sure, just use:
new_date = input(orig_date, mmddyy10.);
SAS is bright enough to figure it out.
data test;
infile datalines;
input origdate $10.;
newdate=input(origdate,anydtdte10.);
datalines;
1/5/2010
12/24/2013
10/3/2012
;
run;
I would be careful about the ANYDTDTE informat. The output depends on the DATESTYLE option:
1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
72
73 options datestyle=dmy;
74 data _null_;
75 infile datalines;
76 input origdate $10.;
77 newdate=input(origdate,anydtdte10.);
78 put newdate date9.;
79 datalines;
01MAY2010
24DEC2013
10MAR2012
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
83 ;
84 run;
85
86 options datestyle=mdy;
87 data _null_;
88 infile datalines;
89 input origdate $10.;
90 newdate=input(origdate,anydtdte10.);
91 put newdate date9.;
92 datalines;
05JAN2010
24DEC2013
03OCT2012
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
So your program gets different results depending on the environment you are running in, as the default for DATETIME depends on the LOCALE option.
Assuming the other two examples are like the second one then you want to treat the values as MDY. So use the MMDDYY informat. Note that the INPUT() function does not care if you use a width on the informat that is longer than the length of string you are reading.
new_date = input(orig_date, mmddyy10.);
format new_date yymmdd10.;
Note I would not use either MDY or DMY order to display the dates to avoid confusing half of your audience. So either use DATE or YYMMDD as the format to attach to the new variable.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.