BookmarkSubscribeRSS Feed
raivester
Quartz | Level 8

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?

4 REPLIES 4
Astounding
PROC Star

Sure, just use:

new_date = input(orig_date, mmddyy10.);

SAS is bright enough to figure it out.

CarmineVerrell
SAS Employee

data test;
infile datalines;
input origdate $10.;

newdate=input(origdate,anydtdte10.);
datalines;
1/5/2010
12/24/2013
10/3/2012
;
run;

s_lassen
Meteorite | Level 14

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.

Tom
Super User Tom
Super User

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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

What is Bayesian Analysis?

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.

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
  • 4 replies
  • 659 views
  • 2 likes
  • 5 in conversation