BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
confused_saser
Obsidian | Level 7

I don't think I understand exactly how this piece of code works:

 

 if (1 < weekday(date_accident)< 7) then 
       calc_date_accident = date_accident-1; 
    else 
       calc_date_accident = date_accident;

I understand what it is doing-I know that it is subtracting one day from the date_accident variable, but I don't think I understand what is happening in the "IF" statement, specifically the 1 and 7.

 

Can someone please explain what this code is actually saying?

 

thanks in advance

1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19

The expression is a shorthand for

1 < weekday(date_accident) and weekday(date_accident) < 7

you could also say

IF weekday(date_accident) NOT IN (1 7);

 

Not Saturday or Sunday.  See the WEEKDAY() function documentation.

View solution in original post

6 REPLIES 6
data_null__
Jade | Level 19

The expression is a shorthand for

1 < weekday(date_accident) and weekday(date_accident) < 7

you could also say

IF weekday(date_accident) NOT IN (1 7);

 

Not Saturday or Sunday.  See the WEEKDAY() function documentation.

confused_saser
Obsidian | Level 7

thanks @data_null__!

 

So the 1 and the 7 actually represents Saturday and Sunday?

 

Does that mean I can select any day of the week by using these numbers in conjuction with "weekday". So  for example if I wanted to select Wednesday I would choose the number 4?

data_null__
Jade | Level 19

@confused_saser wrote:

thanks @data_null__!

 

So the 1 and the 7 actually represents Saturday and Sunday?

 

Does that mean I can select any day of the week by using these numbers in conjuction with "weekday". So  for example if I wanted to select Wednesday I would choose the number 4?


 

I'm sorry, I assumed you had looked at the documention for the WEEKDAY function and knew what it was returning.  Yes today, Wednesday, is weekday 4.

confused_saser
Obsidian | Level 7

I should have looked it up but I actualyl dind't realize it was a function.

 

Super new to SAS! thank you 🙂

data_null__
Jade | Level 19

@confused_saser wrote:

I should have looked it up but I actualyl dind't realize it was a function.

 

Super new to SAS! thank you 🙂


 

I thought your confusion was with the expression 1 < value < 7, it was for me.  I had to write an example to make sure I knew what it was doing.

 

data acc;
   do date_accident = today() to today()+10;
      weekday = weekday(date_accident);
      x = 1 < weekday(date_accident)< 7;
      y = 1 < weekday(date_accident) and weekday(date_accident) < 7;
      z = weekday(date_accident) not in (1 7);
      if (1 < weekday(date_accident)< 7) then calc_date_accident = date_accident-1; 
      else calc_date_accident = date_accident;
      output;
      end;
   run;
proc print;
   run;

Capture.PNG

Peter_C
Rhodochrosite | Level 12
Thought you might (at cost of a little complexity) have reduced the code to

Calc_date_accident = date_accident - not ( weekday( date_accident ) in( 1, 7 ) ) ;

Since the results of SAS logical operations are either 0 or 1, the composite above achieves what appears to be wanted....

I'm just interested in what activity requires this adjustment for Monday to Friday but not for Saturday and Sunday....?

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
  • 6 replies
  • 1849 views
  • 3 likes
  • 3 in conversation