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

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 !

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

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

5 REPLIES 5
Shmuel
Garnet | Level 18
Change the informat to mmdyy10.:

Mark_App = input(Mark_App_s, mmdyy10.) ;
ballardw
Super User

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.

rmacarthur
Pyrite | Level 9

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.    

Shmuel
Garnet | Level 18

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;
rmacarthur
Pyrite | Level 9

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 !

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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