BookmarkSubscribeRSS Feed
Calcite | Level 5

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;



Diamond | Level 26 RW9
Diamond | Level 26

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.

Super User

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

Opal | Level 21


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_tea_m_r =1;
  eat_soda_m_r =0;
  eat_soda_m_r =.;

data want;
  set have;
  array eat_vars {*} eat_:;
  SSB=  ( sum(of eat_vars[*])=dim(eat_vars) );



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. 

Register now!

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.

Get the $99 certification deal.jpg



Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 1 like
  • 4 in conversation