Hi,
I have a problem to create cross table for survey data contained "select all that applied" questions. The data is like below (Headache and Insomnia are two options in one "select all that applied" questions, so they may overlapped):
ID | Treatment | Headache | Insomnia |
1 | 1 | 0 | 0 |
2 | 0 | 1 | 0 |
3 | 1 | 1 | 0 |
4 | 1 | 1 | 0 |
5 | 1 | 1 | 0 |
6 | 0 | 0 | 0 |
7 | 1 | 1 | 1 |
8 | 1 | 1 | 0 |
9 | 0 | 1 | 0 |
10 | 1 | 1 | 0 |
11 | 1 | 1 | 1 |
12 | 1 | 1 | 0 |
13 | 0 | 1 | 0 |
14 | 0 | 0 | 0 |
15 | 1 | 0 | 0 |
16 | 1 | 1 | 0 |
17 | 1 | 0 | 1 |
18 | 1 | 1 | 0 |
19 | 1 | 1 | 0 |
20 | 1 | 1 | 0 |
The usual cross table is var by Headache or var by Insomnia separately with (0,1)x(0,1).
But I wonder how I can get a cross table crossing all options = 1 together, like the following table:
Treatment | Headache=1 [N (%)] | Insomnia=1 [N (%)] |
1 | xx (a%) | xx (b%) |
0 | xx (c%) | xx (d%) |
Total | n_1 (100%) | n_2 (100%) |
In this case, only a+c=100% make sense.
For the completeness, I also wonder how to get a "transposed" table like:
| Treatment |
|
|
| 1 [N (%)] | 0 [N (%)] | Total |
Headache=1 | xx (a%) | xx (b%) | n_1 (100%) |
Insomnia=1 | xx (c%) | xx (d%) | n_2 (100%) |
In this case, of course only a+b=100% make sense.
Hope someone could help. Thanks so much!
Have you tried PROC MEANS?
proc means data=have noprint;
class treatment;
ways 0 1;
output out = want sum= N mean = PCT;
run;
With 0/1 variables if you take the sum that's the equivalent of counting the number of 1s. If you take the mean, that's the same as calculating the percent.
The CLASS statement tells SAS to calculate it by treatment.
WAYS tells it to do the total and one way summaries.
WANT data set will have everything you need to create your table and then you'll need to figure out how to display it.
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.