SAS Procedures

Help using Base SAS procedures
BookmarkSubscribeRSS Feed
Jima
Calcite | Level 5

I am trying to do a running total of 'n' in a PROC REPORT. I can easily get 'n' and '%' like this:

CategoryItem%
A11004.76%
 22009.52%
 330014.29%
    
B140019.05%
 250023.81%
 360028.57%
    
  2100100.00%

 

But I want to get cumulative 'n' and '%' within each category like this:

CategoryItemCum NCum %
A110016.67%
 230050.00%
 3600100.00%
    
B140026.67%
 290060.00%
 31500100.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.

3 REPLIES 3
Cynthia_sas
SAS Super FREQ
Hi:
SAS does recognize the N statistic in a COMPUTE block, but you need to use it properly. Perhaps using a temporary variable will help. But to compute a percent and/or a cum percent, you're going to need to understand what is the denominator for the division.

I'm curious though why you don't use PROC FREQ to get the cum frequency and cum percent, create an output dataset from FREQ and then use PROC REPORT to arrange the output from FREQ.

You don't show any data or PROC REPORT code. Can you post a sample dataset or re-create the report using SASHELP.CLASS or SASHELP.SHOES??

Cynthia
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
Cynthia_sas
SAS Super FREQ

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:

cum_freq_report.png

 

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:

cum_of_group_report.png

 

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

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

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
  • 3 replies
  • 1226 views
  • 0 likes
  • 3 in conversation