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
- /
- General Programming
- /
- Proc freq where several variables equal to 1

Topic Options

- 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
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

10-13-2016 12:56 AM

Try to get the total number of "1"s for several binary variables at the same table. But it did not work. if I use "tables a b c d/noprint out=output;", then only d is listed in output. And I am not sure how to insert where statement to just count the "1"s. Please help. Thanks.

data test;

input a b c d;

datalines;

1 0 1 0

0 0 1 1

1 1 0 0

1 1 1 1

0 0 1 1

1 1 1 0

1 0 1 1

0 0 1 1

0 1 0 0

;

run;

proc freq data=test;

tables a b c d/noprint out=output;

run;

Accepted Solutions

Solution

10-13-2016
07:05 PM

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

Posted in reply to douguo79

10-13-2016 01:04 AM

That's how it works, yes, the documentation states this.

If you want it in a dataset use the ODS table instead.

I have some sample code that places them into a semi-decent formatted table. See the link below.

https://gist.github.com/statgeek/e0903d269d4a71316a4e

And if you really want to be lazy, use PROC MEANS and SUM the variables, since the values are only 1/0, the SUM is the same as the number of 1's.

```
proc means data=test sum stackods maxdec=0;
var a b c d;
ods output summary=Total;
run;
proc print data=total noobs label;
run;
```

GitHub is where people build software. More than 27 million people use GitHub to discover, fork, and contribute to over 80 million projects.

All Replies

Solution

10-13-2016
07:05 PM

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

Posted in reply to douguo79

10-13-2016 01:04 AM

That's how it works, yes, the documentation states this.

If you want it in a dataset use the ODS table instead.

I have some sample code that places them into a semi-decent formatted table. See the link below.

https://gist.github.com/statgeek/e0903d269d4a71316a4e

And if you really want to be lazy, use PROC MEANS and SUM the variables, since the values are only 1/0, the SUM is the same as the number of 1's.

```
proc means data=test sum stackods maxdec=0;
var a b c d;
ods output summary=Total;
run;
proc print data=total noobs label;
run;
```

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

Posted in reply to Reeza

10-13-2016 01:16 AM

Thank you for your reply. However, I really want to have the output as a dataset. Thus several of them can merge together by year (the data is from different years with the same variables).

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

Posted in reply to douguo79

10-13-2016 01:22 AM - edited 10-13-2016 01:23 AM

Re-read the answer. Run the code if you need to.

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

Posted in reply to Reeza

10-13-2016 01:34 AM

Thank you very much. The code works for count the "1"s. But what about "0"s? I am wondering if there is a method working for both.

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

Posted in reply to douguo79

10-13-2016 01:42 AM

Then use the solution in the github link, it's more similar to PROC FREQ.

Or, assuming you only have 0/1's then some basic math.

Number of observations = Number of 1's + Number of 0's.

PROC MEANS gives you 2/3, you can calculate the third in a data step.

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

Posted in reply to Reeza

10-13-2016 01:48 AM - edited 10-13-2016 07:05 PM

Thank you very much. I continued to work on it today. Unless I use macro statement, your method is the best one. Let me know if you have any new ideas. I really appreciate it.