BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
cluelesssas
Calcite | Level 5

I'm a beginner to SAS. I'm trying to have nested counts presented cleanly as follows.

cluelesssas_0-1743033823619.png

 

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;
1 ACCEPTED SOLUTION

Accepted Solutions
Cynthia_sas
Diamond | Level 26

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

Cynthia_sas_0-1743037408944.png

 

View solution in original post

6 REPLIES 6
Cynthia_sas
Diamond | Level 26

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

Cynthia_sas_0-1743037408944.png

 

cluelesssas
Calcite | Level 5

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;
Cynthia_sas
Diamond | Level 26
Hi:
That is a simpler form of PROC REPORT since it doesn't have any compute blocks or style overrides. If that works for you, then that is going to be easier to code. I suggest you look on www.lexjansen.com for PROC REPORT papers if you are new to using PROC REPORT.
Cynthia
Ksharp
Super User
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;

Ksharp_1-1743038560241.png

 

SteveDenham
Jade | Level 19

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

Ksharp
Super User
Yes.
But OP need pretty good layout like some margin before some levels.
That is unable to be done by PROC MEANS.
Since OP just only want a REPORT not a dataset, I think PROC REPORT is the only choice.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

What is Bayesian Analysis?

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1675 views
  • 2 likes
  • 4 in conversation