I am trying to do a running total of 'n' in a PROC REPORT. I can easily get 'n' and '%' like this:
Category | Item | N | % |
A | 1 | 100 | 4.76% |
2 | 200 | 9.52% | |
3 | 300 | 14.29% | |
B | 1 | 400 | 19.05% |
2 | 500 | 23.81% | |
3 | 600 | 28.57% | |
2100 | 100.00% |
But I want to get cumulative 'n' and '%' within each category like this:
Category | Item | Cum N | Cum % |
A | 1 | 100 | 16.67% |
2 | 300 | 50.00% | |
3 | 600 | 100.00% | |
B | 1 | 400 | 26.67% |
2 | 900 | 60.00% | |
3 | 1500 | 100.00% |
I have found several papers/references on cumulative totals and percents, but they all use one of the dataset variables. SAS doesn't recognize the 'n' statistic in a compute block. I have tried doing counts in a compute block - 'cnt+1;' - but didn't work. I could do the counts in a data step, but I would rather do it in the procedure. Any suggestions?
Thanks...
Jim A.
Agreeing with @Cynthia_sas, it would be very easy to compute the cumulative counts before you run PROC REPORT in a PROC or data step, and then run PROC REPORT on this.
Hi:
Well, either use the PROC FREQ numbers or use a DATA step or get them from TABULATE.
But I did not mean to imply that you can't do cumulative N or cumulative percent with PROC REPORT. It is totally possible. See below:
For percent of the group, as the original question showed, a slightly more complex compute block is needed. There's a LONG discussion of percents and percents of groups starting with Example 4 on page 22 of this paper: https://support.sas.com/resources/papers/proceedings17/SAS0431-2017.pdf .
For SASHELP.SHOES, it is possible to do cum count and cumpercent of group using a slightly different logic, as shown below:
You could always calculate the numbers in a DATA step program, but that frequently means two passes through the data, which is OK if it makes maintenance easier. But PROC REPORT can do the CUM count and CUM percent too.
Hope this helps,
Cynthia
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
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.