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

Showing results for

- Home
- /
- Programming
- /
- SAS Procedures
- /
- Problem with If-Then Statement

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 07-28-2017 10:40 AM
(636 views)

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?

3 REPLIES 3

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

**If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. **

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.