Proc freq where several variables equal to 1

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

Proc freq where several variables equal to 1

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
Super User
Posts: 19,023

Re: Proc freq where several variables equal to 1

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;

 

View solution in original post


All Replies
Solution
‎10-13-2016 07:05 PM
Super User
Posts: 19,023

Re: Proc freq where several variables equal to 1

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;

 

New Contributor
Posts: 4

Re: Proc freq where several variables equal to 1

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). 

Super User
Posts: 19,023

Re: Proc freq where several variables equal to 1

[ Edited ]

 

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

New Contributor
Posts: 4

Re: Proc freq where several variables equal to 1

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. 

Super User
Posts: 19,023

Re: Proc freq where several variables equal to 1

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.

 

New Contributor
Posts: 4

Re: Proc freq where several variables equal to 1

[ Edited ]

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.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 378 views
  • 0 likes
  • 2 in conversation