turn on suggestions

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

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- SAS Procedures
- /
- Not Sure What Proc To Use

Topic Options

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-12-2016 12:21 PM

Hello all!

I'm rather rusty at SAS and was hoping for a bit of help. I have 5 binary variables and want to know how many observations reported "yes" for all 5 of them. Is there a way to do this? Thank you!

Accepted Solutions

Solution

01-12-2016
01:48 PM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-12-2016 12:24 PM

Proc Freq is a good start.

proc freq data=your_dataset_name;

table binary1*binary2*binary3*binary4*binary5/out=summary list;

run;

proc sort data=summary;

by descending binary1 descending binary2 descending binary3 descending binary4 descending binary5;

run;

proc print data=summary;

run;

If you want to limit the results to only 1's you can use a WHERE to filter it out, though it's nice to see all the combinations.

WHERE sum(binary1, binary2, .. , binary5) = 5;

proc freq data=your_dataset_name;

table binary1*binary2*binary3*binary4*binary5/out=summary list;

run;

proc sort data=summary;

by descending binary1 descending binary2 descending binary3 descending binary4 descending binary5;

run;

proc print data=summary;

run;

If you want to limit the results to only 1's you can use a WHERE to filter it out, though it's nice to see all the combinations.

WHERE sum(binary1, binary2, .. , binary5) = 5;

All Replies

Solution

01-12-2016
01:48 PM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-12-2016 12:24 PM

proc freq data=your_dataset_name;

table binary1*binary2*binary3*binary4*binary5/out=summary list;

run;

proc sort data=summary;

by descending binary1 descending binary2 descending binary3 descending binary4 descending binary5;

run;

proc print data=summary;

run;

If you want to limit the results to only 1's you can use a WHERE to filter it out, though it's nice to see all the combinations.

WHERE sum(binary1, binary2, .. , binary5) = 5;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-12-2016 12:27 PM

Well, if you have something like:

data have; var1=1; var2=0; var3=1; var4=1; output; var1=1; var2=1; var3=1; var4=1; output; run; data want; set have end=last; retain result 0; array var{4}; if sum(of var{*})=4 then result=sum(result,1); if last then output; run;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-12-2016 01:40 PM

There are indeed many different ways to achieve this. I agree with @Reeza that it's nice to see all combinations. Here is an alternative to the "table binary1*binary2*binary3*binary4*binary5" approach with PROC FREQ. (Sometimes I have encountered memory shortage issues when applying such a 5- or higher-dimensional cross-table request to a large dataset.)

```
data have; /* just to create dummy data */
array var a b c d e;
do j=1 to 100;
do i=1 to 5;
var[i]=(ranuni(314159)<0.5);
end;
output;
end;
drop i j;
run;
data combi;
set have;
length comb $5;
comb=cats(of a--e); /* creates the combinations like '01101' */
run;
proc freq data=combi;
tables comb;
run;
```

Or you could use PROC SQL without creating a new dataset (like COMBI above):

```
proc sql;
select a, b, c, d, e, count(*) as cnt
from have
group by a, b, c, d, e;
quit;
```

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-12-2016 01:50 PM

@Reeza @RW9 @FreelanceReinhard Thank you all for your replies!