I'm a beginner to SAS. I'm trying to have nested counts presented cleanly as follows.
This is code to generate toy data, as well as 3 attempts I tried (from hours of googling how to do this) that give the correct info, but none are as clean as the above. Imagine how awful they'd look when I need more than 2 layers of nesting. and multiplying together the total number of categories per variables is large, but the actual number of their combinations in the data is small.
data toy;
input A $ B $;
datalines;
A1 B1
A1 B2
A1 B2
A2 B1
A2 B1
A2 B1
A2 B2
A2 B2
A2 B2
A2 B2
;
proc tabulate data=toy;
class A B;
table A*(B all) all;
run;
proc summary data=toy print;
class A B;
run;
proc freq data=toy;
tables A*B / nopercent norow nocol;
run;
Hi,
My tendency would be to use PROC REPORT. I think report #3 in my example (using your test data) is closer to what you want. You can generate the PROC REPORT output without any column headers at all; however, I rarely find a need to do that and using column headers keeps the report columns less confusing.
Cynthia
Hi,
My tendency would be to use PROC REPORT. I think report #3 in my example (using your test data) is closer to what you want. You can generate the PROC REPORT output without any column headers at all; however, I rarely find a need to do that and using column headers keeps the report columns less confusing.
Cynthia
thx Cynthia. What I ended up doing was based on your #2 proposal. I actually had 3 layers of nesting in my actual data (so imagine a variable C that we also want to nest under A and B in the toy data). This code seemed to do the job I hope it will generalize to future situations
proc report data=toy; column A B C n; define A / group; define B / group; define C / group; define n / 'Count'; break before A / summarize; break before B / summarize; run;
data toy;
input A $ B $;
datalines;
A1 B1
A1 B2
A1 B2
A2 B1
A2 B1
A2 B1
A2 B2
A2 B2
A2 B2
A2 B2
;
proc freq data=toy noprint;
tables A*B /out=have list nopercent norow nocol;
run;
proc report data=have nowd ;
columns a b count;
define a/group noprint;
define b/group 'level';
define count/analysis 'count';
compute before a;
b=a;
endcomp;
compute b;
if missing(_BREAK_) then call define(_col_,'style','style={indent=0.25in}');
endcomp;
break before a/summarize;
run;
I know this has been answered, but wouldn't a PROC MEANS (or SUMMARY) give all of the levels needed for this? It would then just be a sorting task to get something to print out. I have to admit the PROC REPORT approach more quickly yields a more esthetically pleasing output, but if the dataset is really large, with a lot of nesting, and you need to use the results for additional work, it might be worth taking a look.
SteveDenham
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.