How can I convert a character variable to a date using the SAS programming language? 2

How can I convert a character variable to a date using the SAS programming language?

The process of converting a character variable to a date using the SAS programming language involves using the INPUT function to read the character variable and convert it to a numeric value. This numeric value can then be formatted using the appropriate date format to display the date in a desired format. This conversion can be useful for performing various data analysis and manipulation tasks in SAS.

SAS: Convert Character Variable to Date


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 printdata=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 printdata=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 .

Additional Resources

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

Cite this article

stats writer (2024). How can I convert a character variable to a date using the SAS programming language?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-convert-a-character-variable-to-a-date-using-the-sas-programming-language/

stats writer. "How can I convert a character variable to a date using the SAS programming language?." PSYCHOLOGICAL SCALES, 1 Jul. 2024, https://scales.arabpsychology.com/stats/how-can-i-convert-a-character-variable-to-a-date-using-the-sas-programming-language/.

stats writer. "How can I convert a character variable to a date using the SAS programming language?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-convert-a-character-variable-to-a-date-using-the-sas-programming-language/.

stats writer (2024) 'How can I convert a character variable to a date using the SAS programming language?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-convert-a-character-variable-to-a-date-using-the-sas-programming-language/.

[1] stats writer, "How can I convert a character variable to a date using the SAS programming language?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.

stats writer. How can I convert a character variable to a date using the SAS programming language?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top