Alternative to using substr to convert char date var

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 131
Accepted Solution

Alternative to using substr to convert char date var

I'm wondering the best way to convert a character date var. to numeric if the character dates are written differently (ie 2/1/2013, 2/01/2013, 12/1/2013, etc.)

My initial date variable was character, so, i converted it to numeric using the following code but now I see this did not work because the character dates are written differently:

%let cutoff_date="28Feb2013"d;

data c2;

set c1;

/*ra date*/

    ra_date_n= mdy (substr(ra_date,1,2), substr(ra_date,4,2),substr(ra_date,7,4));

    label ra_date_n="Random Assignment Date";

if .< ra_date_n <=&cutoff_date.;

run;

Would you have suggestions; i'd prefer not to change the original excel file.

Thank you very much in advance.


Accepted Solutions
Solution
‎01-13-2015 02:58 PM
Super User
Posts: 19,791

Re: Alternative to using substr to convert char date var

Posted in reply to Maisha_Huq

Try the input function with mmddyy10. format:

data have;

input  date_char $10.;

cards;

2/1/2013

2/01/2013

12/1/2013

;

   run;

data want;

set have;

format sas_date date9.;

sas_date=input(date_char, mmddyy10.);

run;

proc print data=want;run;

View solution in original post


All Replies
Trusted Advisor
Posts: 3,212

Re: Alternative to using substr to convert char date var

Posted in reply to Maisha_Huq

Use the data informats SAS(R) 9.4 Formats and Informats: Reference with a input function SAS(R) 9.4 Functions and CALL Routines: Reference, Third Edition

Attention it is the input FUNCTION meant for conversions using formats not to be confused with the input statement.'

SAS(R) 9.4 Formats and Informats: Reference this one is supporting  dd / mm / yy

---->-- ja karman --<-----
Solution
‎01-13-2015 02:58 PM
Super User
Posts: 19,791

Re: Alternative to using substr to convert char date var

Posted in reply to Maisha_Huq

Try the input function with mmddyy10. format:

data have;

input  date_char $10.;

cards;

2/1/2013

2/01/2013

12/1/2013

;

   run;

data want;

set have;

format sas_date date9.;

sas_date=input(date_char, mmddyy10.);

run;

proc print data=want;run;

🔒 This topic is solved and locked.

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

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