SAS : Convert Character Variable to Date

Live Online Training : SAS Programming with 50+ Case Studies

- Explain Programming Concepts in Simple English
- Live Projects
- Scenario Based Questions
- Job Placement Assistance
- Get 20% off till July 14, 2017

This tutorial explains multiple ways we can convert character variable to SAS date.
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.
Create a Sample Data
data example;
input dateofbirth $20.;
Convert Character Variable to SAS Date

The INPUT function is used to convert character variable to numeric. With MMDDYY10. format, we assign format of the date.
data out;
set example;
dateofbirth2 = input(strip(dateofbirth),MMDDYY10.);
format dateofbirth2 MMDDYY10.;
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

If Convert to Different Date Format

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.;

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.

How to convert character dates of DD-MMM-YYYY or DD/MMM/YYY format?

You can use the format date11. to convert character values in DD-MMM-YYYY format.
DATA temp;
     INPUT @6 dt $11.;
     dt2 = input(strip(dt),date11.);
     FORMAT dt2 date11.;

Convert Multiple Character Variables to Date

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.;
1971-11-21 1991-12-21
1980-05-14 1999-10-20
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.);
drop i;
format dt1-dt2 yymmdd10.;
Array : Convert Character Variable to SAS Date

SAS Tutorials : 100 Free SAS Tutorials

About Author:

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has close to 7 years of experience in data science and predictive modeling. During his tenure, he has worked with global clients in various domains like retail and commercial banking, Telecom, HR and Automotive.

While I love having friends who agree, I only learn from those who don't.

Let's Get Connected: Email | LinkedIn

Get Free Email Updates :
*Please confirm your email address by clicking on the link sent to your Email*

Related Posts:

8 Responses to "SAS : Convert Character Variable to Date"

  1. 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.

    1. Thanks Arvind for your appreciation. Cheers!

  2. Hi 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.

    1. Could you please post an example of your date variable?

  3. Hi 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.
    Could you help me with this ? Thanks!

    1. Check out this code -
      data temp;
      length x $23.;
      input x$;
      y = INPUT(substr(strip(x),1,10),yymmdd10.) ;
      format y ddmmyy10.;
      proc print;

  4. Hello Deepanshu,can you please let me know the format for the following date 10/Jun/2017

    1. date11. format. I have added an example of this format in the article. Thanks!


Next → ← Prev