As the subject says, I'm trying to figure out how to create a weekend/weekday indicator variable based on date.
I have found the weekday. format where Sunday =1, Monday =2, etc. and am trying to use a statement in a datastep such as:
If (weekday format of my date) in (1 7) then weekend = 1;
ELSE IF (weekday format of my date) in (2:6) then weekend = 0;
I'm having trouble figuring out how to indicate (weekday format of my date).
Thank you for your help!
Instead of looking at the weekday format, look at the weekday function. It can easily be plugged into your existing code.
Instead of looking at the weekday format, look at the weekday function. It can easily be plugged into your existing code.
Thank you! The function did the trick.
IF weekday(date_of_Injury) in (1 7) then weekend=1;
ELSE IF weekday(date_of_Injury) in (2:6) then weekend=0;
Hi sasgorilla, I found some of your posts rather interesting. I tested your code, weekday() is the right function to use, however, there is a tiny syntax error: you missed a comma in the if statement. I corrected it and my version of code and output are as follows (I also created a small dataset to test the code).
data test;
input subjid date_of_injury date9.;
format date_of_injury date9.;
datalines;
1 01dec2024
2 02dec2024
3 07dec2024
4 08dec2024
5 09dec2024
6 10dec2024
7 14dec2024
8 15dec2024
9 16dec2024
;
run;
data test1;
set test;
weekday=weekday(date_of_injury);
if weekday(date_of_injury) in (1,7) then weekend=1;
else weekend=0;
run;
proc print data=test1;run;
Comma is not part of the syntax of the IF statement.
Perhaps you are of the mistaken believe you have to use commas only as the delimiter in the list of values of the right hand argument of the IN operator? That is NOT true in SAS syntax. You can use comma or space as a delimiter in the list.
Also notice the use of the : to indicate a RANGE of integer values in the right hand argument of the IN operator.
So this code would also work fine:
if weekday(date_of_injury) in (2,3 4:5 6) then weekend=0;
else weekend=1;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.