To get the day of the week of a date variable in a SAS data step, the easiest way is to use the SAS weekday() function.
data data_with_weekday; set data_with_dates; day = weekday(d); run;
When working with data, many times we are working with dates and need to make adjustments to our data depending on which day of the week something occurred.
We can find the day of the week of a date in SAS very easily. With the SAS weekday() function, we can get the day of the week of a date variable.
The SAS weekday() function returns a number between 1 and 7, inclusively, where 1 is Sunday, 2 is Monday, etc., and 7 is Saturday.
Let’s say we have the following SAS dataset.
data data_with_dates; input d date9.; format d date9.; datalines; 31DEC2021 24OCT2020 12DEC2019 07JUN2019 17FEB2021 12JAN2021 03MAR2020 ; run;
We can get the day of the week from a date variable with the SAS weekday() function. Below is SAS code with a data step getting the day of the week from the variable “d”.
data data_with_weekday; set data_with_dates; wd = weekday(d); run;
The resulting dataset with the week day of the date variable is as follows.
d wd 1 31DEC2021 6 2 24OCT2020 7 3 12DEC2019 5 4 07JUN2019 6 5 17FEB2021 4 6 12JAN2021 3 7 03MAR2020 3
Getting the Day of Week Name in SAS from Date Variable
When working with dates, sometimes having the day number can be useful, but it’s possible we want to work with the day name instead.
Getting the name of a day of the week in SAS from a date variable takes a little more work, but it’s easy to do.
There are two ways we can get the name of a week day in SAS. One with PROC Format, and the other with an if/else statement. Depending on style, either will be useful.
Let’s say with the PROC format way of getting the name from a week day number. Below is the SAS code with the PROC Format giving us the week day names.
proc format; value dayname 1 = 'Sunday' 2 = 'Monday' 3 = 'Tuesday' 4 = 'Wednesday' 5 = 'Thursday' 6 = 'Friday' 7 = 'Saturday' other = 'N/A' ; run;
This new ‘dayname’ format will give us the day of the week name from a day of the week number. Using it in a data step, we can get the week day names from our date variable.
data data_with_weekday; format dayname dayname.; set data_with_dates; wd = weekday(d); dayname = wd; run;
The resulting data set with month names is as shown below:
dayname d wd 1 Friday 31DEC2021 6 2 Saturday 24OCT2020 7 3 Thursday 12DEC2019 5 4 Friday 07JUN2019 6 5 Wednesday 17FEB2021 4 6 Tuesday 12JAN2021 3 7 Tuesday 03MAR2020 3
You can also get the day of the week names using if/else if/else statements in a SAS data step. Below is the code to get the name of a day from a date in SAS.
data data_with_weekday; length dayname $ 12.; set data_with_dates; wd = month(d); if wd = 1 then dayname = "Sunday"; else if wd = 2 then dayname = "Monday"; else if wd = 3 then dayname = "Tuesday"; else if wd = 4 then dayname = "Wednesday"; else if wd = 5 then dayname = "Thursday"; else if wd = 6 then dayname = "Friday"; else if wd = 7 then dayname = "Saturday"; else dayname = "N/A"; run;
Personally, I like the PROC format way as it is much cleaner and easier to re-use.
One other way to get the name of the week day is to use the built-in SAS WEEKDATEw. format. While this will work, I like the custom PROC format that I have for you above.
Hopefully this article has been helpful for you to understand how to get the day of the week from a date variable in SAS.