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.
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.
The following tutorials explain how to perform other common tasks in SAS: