DATA Step, Macro, Functions and more

convert 2 vars (char type) to 2 vars (date type)

Accepted Solution Solved
Reply
Contributor
Posts: 39
Accepted Solution

convert 2 vars (char type) to 2 vars (date type)

I have 2 vars (char type):

 

date 1           date 2

26/07/2013   10/04/2014 07:50:11 p.m.
28/02/2013   12/09/2012 12:46:29

 

I need to convert to data type, how can I do it?


Accepted Solutions
Solution
‎03-02-2017 12:08 PM
Super User
Posts: 5,083

Re: convert 2 vars (char type) to 2 vars (date type)

Your problem description talks about dates, and not datetimes.  To get dates only, you could use:

 

date_1_converted = input(date1, ddmmyy10.);

date_2_converted = input(date2, ddmmyy10.);

format date_1_converted date_2_converted yymmdd10.;

 

There's no need to apply YYMMDD10 as the final format.  That's just an example of what is possible.

 

Also note, it's not 100% clear that DATE_2_CONVERTED should apply the DDMMYY10 informat to convert it.  That would be applicable when the character values actually include the day then the month then the year.  But it's also conceivable that the character values contain the month then the day then the year.  In that case, convert with:

 

date_2_converted = input(date2, mmddyy10.);

View solution in original post


All Replies
Super User
Posts: 17,829

Re: convert 2 vars (char type) to 2 vars (date type)

Use the INPUT function. 

 

 

NEW_VAR = INPUT( old_var_name, current_format);

 

Solution
‎03-02-2017 12:08 PM
Super User
Posts: 5,083

Re: convert 2 vars (char type) to 2 vars (date type)

Your problem description talks about dates, and not datetimes.  To get dates only, you could use:

 

date_1_converted = input(date1, ddmmyy10.);

date_2_converted = input(date2, ddmmyy10.);

format date_1_converted date_2_converted yymmdd10.;

 

There's no need to apply YYMMDD10 as the final format.  That's just an example of what is possible.

 

Also note, it's not 100% clear that DATE_2_CONVERTED should apply the DDMMYY10 informat to convert it.  That would be applicable when the character values actually include the day then the month then the year.  But it's also conceivable that the character values contain the month then the day then the year.  In that case, convert with:

 

date_2_converted = input(date2, mmddyy10.);

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 113 views
  • 2 likes
  • 3 in conversation