Table of Contents
The process of formatting date values in PROC SQL using the SAS language involves using the appropriate functions and formats to convert date values into a desired format. This allows for easier manipulation and analysis of date data in SQL queries. The SAS language offers a variety of date formats and functions, such as the DATEPART function and the YYMMDD format, to efficiently handle date values in PROC SQL. By properly formatting date values, users can ensure accurate and consistent results in their SQL queries.
SAS: Format Date Values in PROC SQL
You can use the FORMAT statement within PROC SQL in SAS to format date values in a specific way.
The following example shows how to use the FORMAT statement in practice.
Example: Format Date Values in PROC SQL
Suppose we have the following dataset in SAS that contains information about the start date of promotions at some retail store and the total sales made:
/*create dataset*/
data my_data;
format start_date date9.;
input start_date :date9. sales;
datalines;
01JAN2023 22
01FEB2023 16
14MAR2023 11
01MAY2023 32
13MAY2023 15
18AUG2023 11
20OCT2023 36
;
run;
/*view dataset*/
proc printdata=my_data;

We can use PROC SQL with the FORMAT statement to select all rows from this dataset and display the values in the start_date column in a MM/DD/YY format:
/*select all rows and format start_date column using mmddyy8.*/
proc sql;
select start_date format=mmddyy8., sales
from my_data;
quit;
Notice that the values in the start_date column are now displayed in a MM/DD/YY format.
You can also use the FORMAT statement to format new variables created using PROC SQL.
For example, the following code shows how to create a new variable called end_date by adding 7 days to start_date and formatting it as date9. instead:
/*create new end_date column with specific format*/
proc sql;
select start_date format=mmddyy8., start_date+7 as end_date format=date9., sales
from my_data;
quit;
Notice that we were able to specify the format for both the start_date and end_date variables by using the FORMAT statement multiple times.
The following tutorials explain how to perform other common tasks in SAS:
Cite this article
stats writer (2024). How can I format date values in PROC SQL using the SAS language?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-format-date-values-in-proc-sql-using-the-sas-language/
stats writer. "How can I format date values in PROC SQL using the SAS language?." PSYCHOLOGICAL SCALES, 23 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-format-date-values-in-proc-sql-using-the-sas-language/.
stats writer. "How can I format date values in PROC SQL using the SAS language?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-format-date-values-in-proc-sql-using-the-sas-language/.
stats writer (2024) 'How can I format date values in PROC SQL using the SAS language?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-format-date-values-in-proc-sql-using-the-sas-language/.
[1] stats writer, "How can I format date values in PROC SQL using the SAS language?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I format date values in PROC SQL using the SAS language?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
