BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
sasgorilla
Pyrite | Level 9

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Instead of looking at the weekday format, look at the weekday function.  It can easily be plugged into your existing code.

View solution in original post

4 REPLIES 4
Astounding
PROC Star

Instead of looking at the weekday format, look at the weekday function.  It can easily be plugged into your existing code.

sasgorilla
Pyrite | Level 9

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;

 

 

dxiao2017
Lapis Lazuli | Level 10

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;

Untitled5.png

Tom
Super User Tom
Super User

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;

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1340 views
  • 1 like
  • 4 in conversation