BookmarkSubscribeRSS Feed
Sas20
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;

 

Suggestions? 

3 REPLIES 3
RW9
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.

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

Patrick
Opal | Level 21

@Sas20

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

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

View all other training opportunities.

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