• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / SAS / SAS weekday function – Get Day of Week from Date Variable

SAS weekday function – Get Day of Week from Date Variable

January 19, 2022 Leave a Comment

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.

Other Articles You'll Also Like:

  • 1.  SAS Percent Format – Formatting Number as Percent in SAS Dataset
  • 2.  SAS year function – Get Year from Date Variable
  • 3.  Multiple Condition If Statements in SAS Macro Language
  • 4.  SAS Uppercase – Make String Letters Uppercase with SAS upcase function
  • 5.  Get the Row Number in a SAS Data Step with _n_
  • 6.  Change Length of Character Variable in SAS Dataset
  • 7.  SAS where in – Subset Data by Multiple Values in Data Step
  • 8.  SAS data _null_ – Create a SAS Dataset with No Records and Variables
  • 9.  SAS left() Function – Left Align Character Variables in Data Step
  • 10.  SAS yymmdd10. Date Format

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy