SAS : Converting Number Format to Date Format

This tutorial focuses on converting a number format variable to a date format variable.
Convert Number Format 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
Sample Data

The following program is used to create a sample data.
data temp;
input date;
cards;
20160514
19990505
20131104
20110724
;
run;
Solution
data temp2;
set temp;
newdate = input(put(date,8.),yymmdd8.);
format newdate date10.;
proc print noobs;
run;
Output
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. 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'.
Related Posts
About Author:

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 has worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and Human Resource.

7 Responses to "SAS : Converting Number Format to Date Format"
  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

Next → ← Prev
Love this Post? Spread the Word!
Share