The SAS Output Delivery System and reporting techniques

Cumulative Totals in Proc Report

Reply
Occasional Contributor
Posts: 16

Cumulative Totals in Proc Report

I am trying to figure out the cumulative total for each week end (Saturday) and the total for all weeks within each month so that the cumulative total resets each month for mtd and adds for ytd. Below is my sample code and desired results. My code adds the break values which is not what I want…any help in understanding how to approach this  is appreciated. Thanks!

Mon Date  Day     x

1 1/1/13 Tue   10

1 1/2/13 Wed    5

1 1/3/13 Thur   10

1 1/4/13   Fri     8

1 1/5/13   Sat     6

1 ...

1 1/30/13      Wed     20

1 1/31/13      Thur       2

2 2/1/13   Fri       4

2 2/2/13   Sat      5

2 ...

2 2/28/13      Thur      7

proc report data=test;

column  mon day date x mtd_x ytd_x;

define mon/ group noprint;

define x/analysis sum;

define mtd_x/computed;

define ytd_x/computed;

break after month_num/summarize;

compute mtd_x; total+mtd_x.sum;

IF day = "Sat" then mtd_x=total;

if _break_="mon" then mtd_x=x.sum;

endcomp;

compute ytd_x;

total_1+ytd_x.sum;

IF day = "Sat" then ytd_x=total_1;

if _break_="mon" then ytd_x=total_1;

endcomp;

run;

Sample Result needed

Mon      Date       Day        x      mtd_x ytd_x

1      1/1/13      Tue           10

1      1/2/13      Wed           5

1      1/3/13      Thur           10

1      1/4/13      Fri               8

1      1/5/13      Sat              6      39       39

1 ...

1 1/30/13    Wed                20

1 1/31/13    Thur                 2      61       61

2 2/1/13      Fri                   4

2 2/2/13      Sat                  5      9       70

2 ...

2 2/28/13    Thur                7      16       77

SAS Super FREQ
Posts: 619

Re: Cumulative Totals in Proc Report

Hi

With proc report you can find out when a "group" begins, but there is no way to find out whether you are in the last line for a group. But you can use a DATA Step giving you this information.

See sample code below. currently the report does print more columns than needed, but it will in understanding what is happening.

data have;
  infile cards;
 
input
  mon :
8.
  date :
anydtdte.
  day : $
8.
  x :
8.
  ;
  week = substr( put( date, weeku.), 1, 8);
  format date date9.;
 
cards;
1 1/1/13 Tue 10
1 1/2/13 Wed 5
1 1/3/13 Thur 10
1 1/4/13 Fri 8
1 1/5/13 Sat 6
1 1/30/13 Wed 20
1 1/31/13 Thur 2
2 2/1/13 Fri 4
2 2/2/13 Sat 5
2 2/28/13 Thur 7
;

data want;
  set have;
  by mon week;
  mFirst = first.mon;
  mLast = last.mon;
  wFirst = first.week;
  wlast = last.week;
run;

proc report data=want nowd;
 
column mon week date day x mtd ytd mFirst mLast wFirst wLast _dummy;
  define mon / order;
 
define date / order;
 
define week / order;
 
define day / display;
 
define x / analysis sum;
 
define mtd / computed;
 
define ytd / computed;
 
define mFirst / display;
 
define mLast / display;
 
define wFirst / display;
 
define wLast / display;
 
define _dummy / computed;

 
compute _dummy / char length=64;
  _dummy = _break_;

 
if mFirst = 1 then do;
  h_mtd =
0;
 
end;

 
if missing(_break_) = 1 then do;
  h_mtd + x.sum;
  h_ytd + x.sum;
 
end;

 
if wLast = 1 then do;
  mtd = h_mtd;
  ytd = h_ytd;
 
end;
 
endcomp;
run;
Occasional Contributor
Posts: 16

Re: Cumulative Totals in Proc Report

your logic worked in a data step for me. Thanks for your help.

Post a Question
Discussion Stats
  • 2 replies
  • 501 views
  • 3 likes
  • 2 in conversation