BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
lamiaH
Obsidian | Level 7

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

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

8 REPLIES 8
Reeza
Super User

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

ballardw
Super User

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

Shmuel
Garnet | Level 18

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;
lamiaH
Obsidian | Level 7

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

Shmuel
Garnet | Level 18

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.

 

 

 

lamiaH
Obsidian | Level 7

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

 

Shmuel
Garnet | Level 18

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. 

lamiaH
Obsidian | Level 7

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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