This tutorial explains multiple ways we can convert a character variable (string) to a date variable in SAS.
Suppose you encounter a problem in which you need to convert character variable to SAS date format. It happens most of the times when we upload raw data file in TXT, EXCEL or CSV format to SAS. The problem with dates in character format is you cannot apply any calculations on them.
The following SAS code creates a sample SAS dataset for demonstration purpose.
data example; input dateofbirth $20.; cards; 05/11/1980 07/05/1990 04/14/1981 ; run;
The INPUT function is used to convert character variable (string) to numeric. With MMDDYY10. format, we assign format of the date.
data out; set example; dateofbirth2 = input(strip(dateofbirth),MMDDYY10.); format dateofbirth2 MMDDYY10.; run;Important Note : Please make sure a new variable is created for conversion. If you use the same variable for conversion,the format of the variable would remain character.
Output : Convert Character Variable to Date |
As you can see our original dateofbirth variable is in Month-Date-Year format but stored as a character. If we need to convert it to Date-Month-Year format and stored as in SAS date format.
data out; set example; dateofbirth2 = input(strip(dateofbirth), MMDDYY10.); format dateofbirth2 DDMMYY10.; run;Make sure you put the original date format in INPUT function and put the desired date format in FORMAT statement. If you put the different date format in INPUT function, it may lead to missing value. For example, 04/14/1981 cannot be converted to DDMMYY10. format directly as SAS reads 14 as month which is not possible.
You can use the format date11. to convert character values in DD-MMM-YYYY format.
DATA temp; INPUT dt $11.; dt2 = input(strip(dt),date11.); FORMAT dt2 date11.; CARDS; 10/JUN/2024 ; PROC PRINT NOOBS; RUN;
Suppose you need to convert multiple character variables to SAS datevalue format. We can create SAS array to convert them.
data example2; input dateofbirth $10. hire $11.; cards; 1971-11-21 1991-12-21 1980-05-14 1999-10-20 ; run;Real SAS Date values are numeric so numeric array is created for them.
data out; set example2; array olddates $ dateofbirth hire; array newdates dt1 dt2; do i = 1 to dim(olddates); newdates(i) = input(strip(olddates(i)),yymmdd10.); end; drop i; format dt1-dt2 yymmdd10.; run;
Hi, this is Arvind, your site is really good, while checking SAS tutorial, i felt it really good because you mention there each and every small thing so nicely. so thanks for that.
ReplyDeleteThanks Arvind for your appreciation. Cheers!
DeleteHe'll I'm Narasimha, please can suggest me how to convert character date like 12-aug-2023 to numeric date format ( character date has the format 11.)
DeleteHi Deepanshu, This is Manjunatha. In the dataset1, the date variable is character$9. When I tried to convert it Eg: Newdate=input(strip(date),mmddyy10.); format Newdate mmddyy10. It is showing as missing values (.). Please help.thanks.
ReplyDeleteCould you please post an example of your date variable?
DeleteHi Deepanshu, I have the same problem than Manjunatha, I've a variable in format $CHAR23(2007-05-04 00:00:00.000) and I want obtain a new variable with ddmmyy10 format, I try to do the change by your way but that result a missing value.
ReplyDeleteCould you help me with this ? Thanks!
Check out this code -
Deletedata temp;
length x $23.;
input x$;
y = INPUT(substr(strip(x),1,10),yymmdd10.) ;
format y ddmmyy10.;
cards;
2007-05-0400:00:00.000
;
proc print;
run;
Hello Deepanshu,can you please let me know the format for the following date 10/Jun/2017
ReplyDeletedate11. format. I have added an example of this format in the article. Thanks!
Deletedidn't work
ReplyDeleteHi Deepanshu,
ReplyDeleteCan you please fix the image? Thank you so much
Long
data example;
ReplyDeleteinput dateofbirth $20.;
cards;
05/11/1980
07/05/1990
04/14/1981
;
run;
change month 14 to other i
i have character value of <18 and i want to change it to Numeric value can anyone help me.....
ReplyDeleteHi,
ReplyDeleteTO convert from character to numeric use input function. check the below code.
data temp;
input val $;
datalines;
20
21
;
run;
data tem;
set temp;
new= input(val,2.);
run;
proc contents data= tem;
run;
check the below link for more conversions of data.
https://blogs.sas.com/content/sgf/2015/05/01/converting-variable-types-do-i-use-put-or-input/
Suggestion for use of format of date11. for ddmmmyyy character date was very useful. Many thanks. Format date9. did now work there and only format of date11. worked.
ReplyDelete