How to Convert Numeric to Date Variables in SAS

Deepanshu Bhalla 11 Comments

This tutorial explains how to convert a numeric variable to a date variable in SAS, along with examples.

SAS: Convert Number to Date Format

Suppose you have a numeric variable that contains dates. You are asked to convert it to SAS date format. It seems to be a easy task but it sometimes becomes a daunting task when you don't know how SAS treats dates. The input data is shown below -

Raw Date Values in SAS
Sample Data

The following program is used to create a sample data.

data temp;
input date;
cards;
20160514
19990505
20131104
20110724
;
run;
Solution

To convert a numeric variable into a date variable in SAS, you can use the following syntax.

data temp2;
set temp;
newdate = input(put(date,8.),yymmdd8.);
format newdate date10.;
proc print noobs;
run;
Output: Converting Numeric to Date Format
Explanation
  1. PUT Function is used to convert the numeric variable to character format.
  2. INPUT Function is used to convert the character variable to sas date format
  3. yymmdd8 informat refers to years followed by month and days having width of total 8
  4. FORMAT function is used to display the SAS date values in a particular SAS date format. If we would not use format function, SAS would display the date in SAS datevalues format. For example, 20588 is a sas datevalue and it is equivalent to '14MAY2016'.
ANYDTDTE : One Informat for All

ANYDTDTE is the relatively latest informat developed by SAS Institute, which alone can handle various date, time or datetime forms. For example date can be like March 17, 2024, 17/03/2024, 03/17/2024, 17MAR2024 17:30:02, 17:30

data temp2;
set temp;
newdate = input(put(date,8.),ANYDTDTE8.);
format newdate date10.;
proc print noobs;
run;
Related Posts
Spread the Word!
Share
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

Post Comment 11 Responses to "How to Convert Numeric to Date Variables in SAS"
  1. one question, why i cant change the format yymmdd8. by the format date9. in the input function?
    what type of formats i can take or select?
    the put(date,8.) variable have not a format date, why i can not take other format date?

    ReplyDelete
  2. why is date10. format giving the output as 14MAY2016, it is the format date9.

    ReplyDelete
  3. How to convert character date "10/13/2019" to date9. Format

    ReplyDelete
    Replies
    1. use
      dt = put(input(date,mmddyy10.),date9.);
      format dt date9.;

      Delete
  4. Can you please explain this using data and set statement. My one date is in $5. And I want to change it to date9. How to do that. Name of the original variable is date_1 and new variable I want to create is date_2

    ReplyDelete
  5. Hi,



    Here is some data, where I need to fetch yymmdd10. format data (e.g. 2020-06-08).

    which includes all Jan-Dec or January to December data with (- or / ) seperates.


    data xyz;
    date1= "08-June-2020";output;
    date1= "31-Oct-2020";output;
    date1= "13/August/2020"; output;
    date1= "02/Jan/2020" ;output;

    run;



    Thank you in advance for your help and take care.



    Regards

    Priya

    ReplyDelete
  6. Thank you for your help in this topic :)
    In your example i use ANYDTDTE. informat for reading data.
    My way has pitfalls? Or why did you choose to convert data?
    I am new and want to know all secrets :)


    data temp;
    input date: anydtdte.;
    format date yymmdd10.;
    cards;
    20160514
    19990505
    20131104
    20110724
    ;
    run;

    ReplyDelete
    Replies
    1. ANYDTDTEw. informat is more flexible (and latest one) than older informat datew. so your way is better than mine :-) We convert number format to date to make it more readable as input value is date not a numeric value.

      Delete
  7. Hi if date is in 2019/un/un this format.how to get 2019 asoutput

    ReplyDelete
    Replies
    1. HI, I think we can't read data in sas with missing of month and date values. Please let me know if we can read data like this "2019/un/un" in sas.

      But if we have mmddyy then we can use year() function to pull the year value.

      Delete
Next → ← Prev