I have a data set of hospital patient IDs, and the data shows which of 2 pizza brands (Brand A or B) each unique individual like (they can like one, both, or neither). It looks something like this:
ID A B 21 Yes No 22 No No 23 Yes Yes 24 No Yes 25 Yes Yes 26 Yes No 27 No Yes . . . . ;
Now, based on that data, I want to know how many patients liked A, B, only A, only B, both, neither one, either one.
What's an effecient approach to this? I was thinking of doing a table where the columns are for A and B, then the rows are Yes or No. Then for each entry, it will have the count of yes/no for A and yes/no for B, but that doesn't seem to be very helpful in showing a report that gives me what I want.
Any ideas on what I can code to produce a report with those values?
Combine the variables A and B into a single variable. Then run PROC FREQ.
data have1;
set have;
ab=cats(A,'|',B);
run;
proc freq data=have1;
tables ab;
run;
data pizza_likes;
input ID $ A $ B $;
datalines;
21 Yes No
22 No No
23 Yes Yes
24 No Yes
25 Yes Yes
26 Yes No
27 No Yes
;
run;
proc freq data=pizza_likes;
tables A*B / nocol nopercent norow;
run;
proc sql;
select
sum(A='Yes') as Likes_A,
sum(B='Yes') as Likes_B,
sum(A='Yes' and B='No') as Likes_Only_A,
sum(A='No' and B='Yes') as Likes_Only_B,
sum(A='Yes' and B='Yes') as Likes_Both,
sum(A='No' and B='No') as Likes_Neither,
sum(A='Yes' or B='Yes') as Likes_Either
from pizza_likes;
quit;
My approach is similar to @PaigeMiller only uses an option on tables statement that doesn't require creating a new variable:
proc freq data=have; tables a*b / missing list ; run;
I include Missing so if you have any missing values they appear in the body of the report table.
@unwashedhelimix wrote:
I have a data set of hospital patient IDs, and the data shows which of 2 pizza brands (Brand A or B) each unique individual like (they can like one, both, or neither). It looks something like this:
ID A B 21 Yes No 22 No No 23 Yes Yes 24 No Yes 25 Yes Yes 26 Yes No 27 No Yes . . . . ;
Now, based on that data, I want to know how many patients liked A, B, only A, only B, both, neither one, either one.
What's an effecient approach to this? I was thinking of doing a table where the columns are for A and B, then the rows are Yes or No. Then for each entry, it will have the count of yes/no for A and yes/no for B, but that doesn't seem to be very helpful in showing a report that gives me what I want.
Any ideas on what I can code to produce a report with those values?
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.