turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- General Programming
- /
- Can someone explain this piece of code in plain la...

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-25-2017 12:37 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-25-2017 12:49 PM - last edited on 01-25-2017 01:02 PM by ChrisHemedinger

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.

All Replies

Solution

01-25-2017
12:57 PM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-25-2017 12:49 PM - last edited on 01-25-2017 01:02 PM by ChrisHemedinger

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-25-2017 12:52 PM

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?

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-25-2017 12:56 PM - edited 01-25-2017 12:56 PM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-25-2017 12:57 PM

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

Super new to SAS! thank you

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-25-2017 01:07 PM

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;
```

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-25-2017 05:22 PM

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....?

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....?