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
- /
- SAS Procedures
- /
- Problem with If-Then Statement

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

07-28-2017 10:40 AM

Using SAS Version 9.3

I would like to create a variable name SSB which if all of the variables (each of them are binary are 0 and 1) =0 (therefore using AND) then it should be =0 and if any of the variables have =1 then that would =1 (therefore using OR) See code below. This is not working however but I don't want the 0's to be OR's since that is not what I want to capture....

if eat_fruitjuice_m_r =0 AND

eat_100juice_m_r =0 AND

eat_juicedrink_m_r =0 AND

eat_soda_m_r =0 AND

eat_ssb_m_r =0 AND

eat_sugarwater_m_r=0 AND

eat_tea_m_r =0 AND

eat_vegjuice_m_r=0 then SSB=0;

else if eat_fruitjuice_m_r ne . and eat_fruitjuice_m_r=1 OR

eat_100juice_m_r ne . and eat_100juice_m_r=1 OR

eat_juicedrink_m_r ne . and eat_juicedrink_m_r =1 OR

eat_soda_m_r ne . and eat_soda_m =1 OR

eat_ssb_m_r ne . and eat_ssb_m_r=1 OR

eat_sugarwater_m_r ne . and eat_sugarwater_m_r=1 OR

eat_tea_m_r ne . and eat_tea_m_r =1 OR

eat_vegjuice_m_r ne . and eat_vegjuice_m_r=1 then SSB=1;

Suggestions?

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

Posted in reply to Sas20

07-28-2017 10:48 AM

You can simplfy it:

ssb=ifn(sum(eat_fruit_juice_m_r,eat_100juice_m_r,...) > 0,0,1);

First this sums() all your variables (I have only put in the first couple), as sum of 0,0,0 etc is 0. Then it uses the binary result ifn() function rather than if then else, just a bit shorted in these yes/no cases, so return 0 if condition true, 1 otherwise.

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

Posted in reply to Sas20

07-28-2017 11:04 AM

Some example data is always helpful such as a few inputs and what you are getting for output.

"Doesn't work" is awful vague.

Are there errors in the log?: Post the code and log in a code box opened with the {i} to maintain formatting of error messages.

No output? Post any log in a code box.

Unexpected output? Provide input data in the form of a dataset, the actual results and the expected results. Data should be in the form of a data step. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

I suspect that your issue may be in the code like this

if eat_fruitjuice_m_r ne . and eat_fruitjuice_m_r=1 OR

where you may intend:

if ( eat_fruitjuice_m_r ne . and eat_fruitjuice_m_r=1 ) OR

There may be some solutions with other functions but without explicit examples of the values the suggestions may not "work" for all cases.

Also is your intent to have SSB equal to 1 or 0 for all records or is there some where it would be missing such as one of your eat variables missing?

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

Posted in reply to Sas20

07-29-2017 12:41 AM - edited 07-29-2017 12:44 AM

Something like below should work. Just list in the array statement all the flag variables you want to use for your test.

```
data have;
eat_fruitjuice_m_r =1;
eat_100juice_m_r =1;
eat_juicedrink_m_r =1;
eat_soda_m_r =1;
eat_ssb_m_r =1;
eat_sugarwater_m_r=1;
eat_tea_m_r =1;
eat_vegjuice_m_r=1;
output;
eat_soda_m_r =0;
output;
eat_soda_m_r =.;
output;
run;
data want;
set have;
array eat_vars {*} eat_:;
SSB= ( sum(of eat_vars[*])=dim(eat_vars) );
run;
```