4.2 C
London
Friday, December 20, 2024
HomeSASData Munging in SASSAS: How to Convert Numeric Variable to Date

SAS: How to Convert Numeric Variable to Date

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the following basic syntax to convert a numeric variable to a date variable in SAS:

date_var = input(put(numeric_var, 8.), MMDDYY10.);
format date_var MMDDYY10.;

The following example shows how to use this function in practice.

Related: How to Convert Numeric Variable to Character in SAS

Example: Convert Numeric Variable to Date in SAS

Suppose we have the following dataset in SAS that shows the total sales made by some store during various different days:

/*create dataset*/
data original_data;
    input day sales;
    datalines;
01012022 15
01022022 19
01052022 22
01142022 11
01152022 26
01212022 28
;
run;

/*view dataset*/
proc print data=original_data;

We can use proc contents to view the data type of each variable in the dataset:

/*display data type for each variable*/
proc contents data=original_data;

We can see that day and sales are both numeric variables.

We can use the following code to create a new dataset in which we convert the day variable from numeric to date:

/*create new dataset where 'day' is date*/
data new_data;
    set original_data;
    date_day = input(put(day, 8.), MMDDYY10.);
    format date_day MMDDYY10.;
    drop day;
run;

/*view new dataset*/
proc print data=new_data; 

Note: We used the drop function to drop the original day variable from the dataset.

We can see that the new variable we created, date_day, is in a date format.

Note that MMDDYY10. is only one possible date format that we could have used. You can find a complete list of SAS date formats here.

Additional Resources

The following tutorials explain how to perform other common tasks in SAS:

SAS: How to Convert Character Variable to Date
SAS: How to Convert Character Variable to Numeric
SAS: Convert Strings to Uppercase, Lowercase & Proper Case

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories