## Count of any two of three variables having similar responses

Super Contributor
Posts: 338

# Count of any two of three variables having similar responses

Hi Colleagues,

Could anyone help me to write a SAS code for the following real life problem?

My simplified data set is like this.

Data food;
Input Household Q2 Q06 Q14;
Cards;

1 1 1 9
2 2 2 2
3 6 6 6
4 1 7 1
5 7 1 1
6 6 1 6
7 1 1 2
8 2 9 2
;
Run;

I need to take the count of households where a household gets two responses of 1 s for any of the two variables.

For example, answer for this data set is 4 because 4 HHs have two responses of 1 s across three variables.

Thanks

Neil
Super User
Posts: 13,574

## Re: Count of any two of three variables having similar responses

The following code fragments will create a dichotomous 0 or 1 for the condition, depending on whether you are concerned about exactly 2 or 2 or more are ones.

Proc means and sum on the variable of choice will give you the count.

Exactly two ones:

two_ones= (sum((q2=1),(q06=1),(q14=1)) =2);

Two or more ones:
two_or_more_ones= (sum((q2=1),(q06=1),(q14=1)) ge 2);

This will also handling missing values.
Super Contributor
Posts: 338

## Re: Count of any two of three variables having similar responses

Hi ballardw,

Your codes worked marvelously! They are great!

Thank you so munch.

Mirisage
Valued Guide
Posts: 2,191

## Re: Count of any two of three variables having similar responses

a sas9 feature (not sure if it is available earlier) which seems to be most appropriate is the count() function.
It works on strings, but the catx() function provides a flexible way of collecting variables into a string with delimiters ensuring the original columns can still be distinguished.
Demonstrated here in a ~"flexible" kind of way with a mixture of data-type columns[pre]106 %let var_list = age name height ;
107 data;
108 set sashelp.class ;
109
110 ** alter content to prove the counting works ;
111 if _n_ in( 1,5,9) then name='1' ;
112 if _n_ in( 3,5,8) then height=1 ;
113
114 ones_ct = count( '||' !! catx( '||', of &var_list ) !!'||', '|1|' ) ;
115 run ;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.DATA13 has 19 observations and 6 variables.
[/pre]

It places two pipe(|) marks around and between column values and searches for single pipes surrounding each 1. There might be a simpler way to maintain separation of the ones, but I found a problem with a single pipe when consecutive variable have value=1.

For Mirisage's data:
change the variable names assigned to the macro variable &var_list
replace sashelp.class with the appropriate dataset and
remove the testing lines numbered 110 to 112 in the log above.

PeterC
Discussion stats
• 3 replies
• 129 views
• 0 likes
• 3 in conversation