You can use the input() function in SAS to convert a character variable to a date variable format.
This function uses the following basic syntax:
date_var = input(character_var, MMDDYY10.); format date_var MMDDYY10.;
The following example shows how to use this function in practice.
Example: Convert Character Variable to Date in SAS
Suppose we have the following dataset in SAS that shows the total sales made by some store during six 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 see that day is a character variable, but it needs to be represented in a date format.
We can use the following code to create a new dataset in which we convert the day variable from a character to date format:
/*create new dataset where 'day' is in date format*/ data new_data; set original_data; new_day = input(day, MMDDYY10.); format new_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, new_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: