DATA Step, Macro, Functions and more

Convert character to date

Accepted Solution Solved
Reply
Contributor
Posts: 21
Accepted Solution

Convert character to date

Am not able to convert character text to a SAS date, apologies for the simple request:

Data to be converted are "Mark_App_s" character of length 12.

 

Date sample 

 

Mark_App_s

 11/20/2001 

 09/05/2017 

 06/06/2017 

 10/31/2017 

 

Code sample is :

Data App_2 ;
set App_1 ;
Mark_App = input(Mark_App_s, yymmdd10.) ;
format Mark_App mmddyy10.;
run ;

 

Thanks !


Accepted Solutions
Solution
3 weeks ago
Trusted Advisor
Posts: 1,831

Re: Convert character to date

Posted in reply to rmacarthur

You can't change type of a variable and there is no a date type.

 

In your code Mark_app will be a numeric type. The format defines that it is a date and how to display it.

 

The input variable - Mark_app_s - is a char type variable.

 

The only way to "change" variable type is by recreating it:

data want;
  set have (rename=(numvar=char));
        numvar = input(char, <informat>);
run;

View solution in original post


All Replies
Trusted Advisor
Posts: 1,831

Re: Convert character to date

Posted in reply to rmacarthur
Change the informat to mmdyy10.:

Mark_App = input(Mark_App_s, mmdyy10.) ;
Super User
Posts: 13,338

Re: Convert character to date

Posted in reply to rmacarthur

The yymmdd10. informat would be used to read data that starts with the year such as 2001/11/20.

 

You may sometimes have to use something like  input(strip(variable),<informat>) if the character variable has leading spaces so that the informat starts reading where the data actually begins.

Contributor
Posts: 21

Re: Convert character to date

Thanks, I was using the correct INFOMAT, (mmddyy10.), very sorry I just typed it wrong (above), and I have stripped the leading and trailing characters, and have also set the length of the character variable "Mark_App_s " to $12. 

So here is a data sample:

 

Mark_App_s 
 11/20/2001     
 09/05/2017     
 08/30/2017     
 06/06/2017     
 10/31/2017     
 01/23/2004     

 

and here is the code

 

Data App_2 ;
set    App_1 ;
Mark_App = input(Mark_App_s, mmddyy10.) ;
format Mark_App mmddyy10.;
run ;

 

And this code does not change the character variable to a date variable.    

Solution
3 weeks ago
Trusted Advisor
Posts: 1,831

Re: Convert character to date

Posted in reply to rmacarthur

You can't change type of a variable and there is no a date type.

 

In your code Mark_app will be a numeric type. The format defines that it is a date and how to display it.

 

The input variable - Mark_app_s - is a char type variable.

 

The only way to "change" variable type is by recreating it:

data want;
  set have (rename=(numvar=char));
        numvar = input(char, <informat>);
run;
Contributor
Posts: 21

Re: Convert character to date

Note about the solution, the difficulty was caused by hidden characters in the variable "Mark_App_s ", which was imported from an online database.  Prior to implementing the above solution, I had to parse the variable using a SUBSTRING into MM, DD, YYYY components, Then tried converting each to a numeric variable.  If it would not convert using an INPUT function, that indicated there were hidden characters, and I continued to use the SUBSTRING function until the numeric conversion method worked.  Only then could the 

 

Mark_App_d  =     mdy(Month_2,Day,Year)       

 

function be used to convert the character variables (Month_2,Day,Year) to a SAS Date.

 

Thanks again for your assistance !

 

☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 199 views
  • 0 likes
  • 3 in conversation