frequency and counts

Accepted Solution Solved
Reply
Contributor
Posts: 27
Accepted Solution

frequency and counts

Hi

 

I have the following data:

 

id   test1  test2 test3 test5 test5

1     1        1       0       1       0

2     0        0       1       0       0

3     0        1       0       1       1 

4     1         1      0       0       1

 

I want to make a frequency table of the percentage of each test (test1-test5 )as a % from the total of all (tests that are 1) 1= is done 0 not done.

The table is like that

 

Tests    count    %    Total 

test1

test2 

test 3

test 4

test 5

Total 

 

when I do proc freq I can get them all, I tried to make a group but some observations contribute more than one time to each test. 

So each observation may have more than one test. so getting group didn't work. 

Can you guide me how to do that. 

 

Thank you so much, 

 

Lamia

 


Accepted Solutions
Solution
‎11-03-2017 01:42 PM
Trusted Advisor
Posts: 1,615

Re: frequency and counts

[ Edited ]

Is that what you want 

data have;
    input id   test1  test2 test3 test4 test5;
datalines;
1     1        1       0       1       0
2     0        0       1       0       0
3     0        1       0       1       1 
4     1        1       0       0       1
; run;

data want;
  set have end=eof;
        retain tot1-tot5 total;
        array testx test1-test5;
        array totx tot1-tot5;
        do i=1 to 5;
           totx(i) = sum(of totx(i) , testx(i));
total = sum(of total, testx(i)); /* alternative 1 */
end;
/* total = sum(of total, test1,test2,test3,test4,test5); /* alternative 2 */
if eof then do;
do i=1 to 5;
name = trim(vname(testx(i)));
count = totx(i);
percent = totx(i) / total;
output;
end;
end;
keep name count percent total;
run;

View solution in original post


All Replies
Super User
Posts: 20,226

Re: frequency and counts

Create one more variable per line that will add to the number you need?

Super User
Posts: 11,563

Re: frequency and counts

What would the output look like for the given example data?

Solution
‎11-03-2017 01:42 PM
Trusted Advisor
Posts: 1,615

Re: frequency and counts

[ Edited ]

Is that what you want 

data have;
    input id   test1  test2 test3 test4 test5;
datalines;
1     1        1       0       1       0
2     0        0       1       0       0
3     0        1       0       1       1 
4     1        1       0       0       1
; run;

data want;
  set have end=eof;
        retain tot1-tot5 total;
        array testx test1-test5;
        array totx tot1-tot5;
        do i=1 to 5;
           totx(i) = sum(of totx(i) , testx(i));
total = sum(of total, testx(i)); /* alternative 1 */
end;
/* total = sum(of total, test1,test2,test3,test4,test5); /* alternative 2 */
if eof then do;
do i=1 to 5;
name = trim(vname(testx(i)));
count = totx(i);
percent = totx(i) / total;
output;
end;
end;
keep name count percent total;
run;
Contributor
Posts: 27

Re: frequency and counts

Thank you so much Shmuel for your reply. I know I need to do array but I'm not that good at doing them. I will try the code tomorrow and let you know of the results.

 

 

Best Regards, 

Lamia

Trusted Advisor
Posts: 1,615

Re: frequency and counts

[ Edited ]

Few notes will help you understand the code:

 

the statement:

array testx test1-test5;

is equivalent to:

array testx {5} test1 test2 test3 test4 test5;

then testx(2) points to test2.

 

vname is a function that assigns the variable name of array member to a desired variable,

for example:

name = trim(vname(testx(i)));

when i=3 then vname(testx(3)) = 'test3'; - the name of the 3rd variable in the array.

 

 

 

Contributor
Posts: 27

Re: frequency and counts

Hello,

 

I tried the code, it works well except the total didn't calculate correctly so the percent didn't show as well. the counts were shown correctly. 

so in your code :

total=sum(of total, test1, test2,test3, test4, test5);

 

so is it sum (of totx instead of total or what )!!!, because the total showed only 1 for all !!!)

 

Thank you so much for your help, I appreciate it. 

 

Best,

Lamia

 

Trusted Advisor
Posts: 1,615

Re: frequency and counts

TOTAL is total of all tests (test1 to test5) of all the observation.

 

TOTX is a name of an array and you cannot use it for adding.

 

I run my test with both alternatives and got the same result, with total=10.

 

Check your code again. If you have any issue, please post the last code you run

with the results you got. 

Contributor
Posts: 27

Re: frequency and counts

Hi,

Thank you so much, I did the code and I got the results, correctly. 

Thank you so much for your help, I've couldn't done it without you. 

 

Best Regards, 

 

Lamia

☑ This topic is solved.

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

Discussion stats
  • 8 replies
  • 261 views
  • 1 like
  • 4 in conversation