Hello, I have 3 variables that put together should be a date. The year keeps coming up as 2002 although the value 2018 was used in the statement to read in the value. I have pasted my code and some data below. What am I missing?
data rpt2;
set rpt1(obs=15 keep=dom mth yr);
yr4=(ifc(index(yr, ":") > 0, "2018", yr));
mod_dt_char=cats("'",dom, mth, yr4,"'d"); /* value before turning into date */
mod_dt=input(cats("'",dom, mth, yr4,"'d"),date.); /* convert to date */
format mod_dt mmddyy10.;
run;
mth | dom | yr | yr4 | mod_dt_char | mod_dt |
Feb | 23 | 2018 | 2018 | 23Feb2018'd | 02/23/2002 |
Feb | 23 | 2018 | 2018 | 23Feb2018'd | 02/23/2002 |
Feb | 14 | 2018 | 2018 | 14Feb2018'd | 02/14/2002 |
Feb | 23 | 2018 | 2018 | 23Feb2018'd | 02/23/2002 |
Feb | 23 | 2018 | 2018 | 23Feb2018'd | 02/23/2002 |
Feb | 14 | 2018 | 2018 | 14Feb2018'd | 02/14/2002 |
Feb | 23 | 2018 | 2018 | 23Feb2018'd | 02/23/2002 |
Feb | 23 | 2018 | 2018 | 23Feb2018'd | 02/23/2002 |
Feb | 14 | 2018 | 2018 | 14Feb2018'd | 02/14/2002 |
Feb | 23 | 2018 | 2018 | 23Feb2018'd | 02/23/2002 |
Apr | 11 | 2016 | 2016 | 11Apr2016'd | 04/11/2002 |
Apr | 11 | 2016 | 2016 | 11Apr2016'd | 04/11/2002 |
Apr | 8 | 2016 | 2016 | 8Apr2016'd | 04/08/1920 |
Apr | 8 | 2016 | 2016 | 8Apr2016'd | 04/08/1920 |
Apr | 11 | 2016 | 2016 | 11Apr2016'd | 04/11/2002 |
Thank you it worked - the date10. informat was the right one. I had never had problem before using date. without the length specified.
data rpt3;
set rpt2(keep=mod_dt_char);
mod_dt=input(mod_dt_char, date10.);
format mod_dt mmddyy10.;
run;
mod_dt_char | mod_dt |
'23Feb2018'd | 02/23/2018 |
'23Feb2018'd | 02/23/2018 |
'14Feb2018'd | 02/14/2018 |
'23Feb2018'd | 02/23/2018 |
'23Feb2018'd | 02/23/2018 |
'14Feb2018'd | 02/14/2018 |
'23Feb2018'd | 02/23/2018 |
'23Feb2018'd | 02/23/2018 |
'14Feb2018'd | 02/14/2018 |
'23Feb2018'd | 02/23/2018 |
'11Apr2016'd | 04/11/2016 |
'11Apr2016'd | 04/11/2016 |
'8Apr2016'd | 04/08/2016 |
'8Apr2016'd | 04/08/2016 |
'11Apr2016'd | 04/11/2016 |
The default length for the date. informat is 7.
Since the first 7 characters of your string are
'23feb2
you get a year of 2002. Remove the quotes, and use the date9. informat.
With regards to the default length of the informat, see Maxim 31.
Hi Julie,
instead of using cats function , you can directly use MDY date function. Below is the sample code
data test;
year='2018';
month='Feb';
day='19';
;
run;
proc format ;
value $datef
'Feb'=02
'Apr'=04
;
run;
data want;
set test;
new_date=mdy(input(month,$datef.),input(day,2.),input(year,4.));
format new_date ddmmyy10.;
run;
I created format as per your value available in month. You've to add remaining month.
Thanks.
Deviates from the main question/topic. Not relevant
Absolutely and doesn't keep up with OPs needs in any case IMHO. Your initial response was neat and well pointed.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.