Help using Base SAS procedures

Problem with If-Then Statement

Occasional Learner
Posts: 1

Problem with If-Then Statement

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;



Super User
Super User
Posts: 7,417

Re: Problem with If-Then Statement

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
Posts: 10,538

Re: Problem with If-Then Statement

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?

Respected Advisor
Posts: 3,901

Re: Problem with If-Then Statement

[ Edited ]


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) );
Ask a Question
Discussion stats
  • 3 replies
  • 1 like
  • 4 in conversation