Can someone explain this piece of code in plain language?

Accepted Solution Solved
Reply
Contributor
Posts: 37
Accepted Solution

Can someone explain this piece of code in plain language?

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


Accepted Solutions
Solution
‎01-25-2017 12:57 PM
Respected Advisor
Posts: 3,775

Re: Can someone explain this piece of code in plain language?

[ Edited ]

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


All Replies
Solution
‎01-25-2017 12:57 PM
Respected Advisor
Posts: 3,775

Re: Can someone explain this piece of code in plain language?

[ Edited ]

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.

Contributor
Posts: 37

Re: Can someone explain this piece of code in plain language?

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?

Respected Advisor
Posts: 3,775

Re: Can someone explain this piece of code in plain language?

[ Edited ]

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.

Contributor
Posts: 37

Re: Can someone explain this piece of code in plain language?

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

 

Super new to SAS! thank you Smiley Happy

Respected Advisor
Posts: 3,775

Re: Can someone explain this piece of code in plain language?


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 Smiley Happy


 

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

Valued Guide
Posts: 2,174

Re: Can someone explain this piece of code in plain language?

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....?
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 243 views
  • 3 likes
  • 3 in conversation