BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Julie4435637
Obsidian | Level 7

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
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
And use DATE9 instead of date, most likely the default is date7, which can mess things up.

View solution in original post

9 REPLIES 9
Reeza
Super User
Yr4 is a character value.
Your ‘dates’ don’t need the quotation mark or d after it. Remove those and try INPUT again.

Reeza
Super User
And use DATE9 instead of date, most likely the default is date7, which can mess things up.
Julie4435637
Obsidian | Level 7

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
Reeza
Super User
You don’t need the quotation marks or d. That still holds. You only need those for hardcoded dates in your code.
Kurt_Bremser
Super User

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.

singhsahab
Lapis Lazuli | Level 10

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.

Nav_Sweeney
Calcite | Level 5

Deviates from the main question/topic. Not relevant

Reeza
Super User
That becomes 4 function calls, 3 x input + MDY + format vs 2, cat + input.
Nav_Sweeney
Calcite | Level 5

Absolutely and doesn't keep up with OPs needs in any case IMHO. Your initial response was neat and well pointed. 

sas-innovate-2024.png

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.

 

Register now!

How to Concatenate Values

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.

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
  • 9 replies
  • 1017 views
  • 1 like
  • 5 in conversation