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 print data=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:
SAS: How to Use the WHERE Operator in PROC SQL
SAS: How to Use the IN Operator in PROC SQL
SAS: How to Use UPDATE Within PROC SQL
SAS: How to Use CONTAINS in PROC SQL