Hi,
I am trying to create two different date formats so I can convert them into the same format for further analysis.
data A;
format my_date date7. d_date mmddyy10. ;
input my_date date7. d_date mmddyy10. ;
datalines;
08JAN19 10/21/2005
26JUN20 12/12/2010
05DEC20 03/29/2009
04OCT13 11/11/2010
;
run;
The table below is what I wanted but the code above is not giving me the expected result. What am I doing wrong?
my_date | d_date |
08JAN19 | 12/12/2010 |
26JUN20 | 03/29/2009 |
05DEC20 | 11/11/2010 |
04OCT13 | 12/12/2010 |
Change your INPUT statement to:
input my_date date7. +1 d_date mmddyy10. ;
You say:
I am trying to create two different date formats so I can convert them into the same format for further analysis.
This really doesn't make sense. Dates can be analyzed in any format, variable 1 can be format 1 and variable 2 can be format 2, and they can even be unformatted, and the results will be the same.
Change your INPUT statement to:
input my_date date7. +1 d_date mmddyy10. ;
You say:
I am trying to create two different date formats so I can convert them into the same format for further analysis.
This really doesn't make sense. Dates can be analyzed in any format, variable 1 can be format 1 and variable 2 can be format 2, and they can even be unformatted, and the results will be the same.
You really should show LOG entries when code does not work as expected.
Please compare this to your code:
data A; format my_date date7. d_date mmddyy10. ; input my_date :date7. d_date :mmddyy10. ; datalines; 08JAN19 10/21/2005 26JUN20 12/12/2010 05DEC20 03/29/2009 04OCT13 11/11/2010 ; run;
When you specify an informat without the : modifier then a variable is read from the position of the input column that ends the last element read. Your code was attempting to read starting at the space after the 9 in 08JAN19 and was forced to read exactly 10 characters. So characters attempted to read as the first mmddyy value were " 10/21/200" 3-digit years are right out from the informat perspective. The : modifier says in effect "start reading where the next non-space value is".
Note that this code would also work:
data A; format my_date date7. d_date mmddyy10. ; input my_date date7. d_date mmddyy10. ; datalines; 08JAN1910/21/2005 26JUN2012/12/2010 05DEC2003/29/2009 04OCT1311/11/2010 ; run;
As the first 7 columns are read for My_date and then columns 8 through 17 are read for d_date.
That is not the output I get for that code.
18 data A; 119 format my_date date7. d_date mmddyy10. ; 120 input my_date date7. d_date mmddyy10. ; 121 datalines; NOTE: Invalid data for d_date in line 122 8-17. RULE: ----+----1----+----2----+----3----+----4----+----5----+----6-- 122 08JAN19 10/21/2005 my_date=08JAN19 d_date=. _ERROR_=1 _N_=1 NOTE: Invalid data for d_date in line 123 8-17. 123 26JUN20 12/12/2010 my_date=26JUN20 d_date=. _ERROR_=1 _N_=2 NOTE: Invalid data for d_date in line 124 8-17. 124 05DEC20 03/29/2009 my_date=05DEC20 d_date=. _ERROR_=1 _N_=3 NOTE: Invalid data for d_date in line 125 8-17. 125 04OCT13 11/11/2010 my_date=04OCT13 d_date=. _ERROR_=1 _N_=4 NOTE: The data set WORK.A has 4 observations and 2 variables. NOTE: DATA statement used (Total process time): real time 0.01 seconds cpu time 0.00 seconds 126 ;
Try reading the data in LIST mode instead of fixed format mode.
Like this:
data A;
input my_date :date. d_date :mmddyy. ;
format my_date d_date yymmdd10.;
datalines;
08JAN19 10/21/2005
26JUN20 12/12/2010
05DEC20 03/29/2009
04OCT13 11/11/2010
;
There is no good reason to attach different formats to dates in the same dataset. That will just confuse you. SAS does not care what format you use to display the dates. The format used to display a value does not change the value that is stored. Also avoid using either M-D-Y or D-M-Y order to display dates, either order you select will confuse half of your audience.
Obs my_date d_date 1 2019-01-08 2005-10-21 2 2020-06-26 2010-12-12 3 2020-12-05 2009-03-29 4 2013-10-04 2010-11-11
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.