SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
curious_guy
Calcite | Level 5

Hi SAS Expert. 

 

I want to ask how to do cumulative sum on previous cell for each row.

Here is data from proc tabulate:

1.png

 

The output I want. 

2.png

So basically the value is the sum from previous cell.

 

Here is the code i use to to proc tabulate

 

proc tabulate data = daybalance;
table year, day*balance;
var balance;
class year day;
run;

 

Kindly help with this if you know thank you for your help it means a lot for me.

5 REPLIES 5
ballardw
Super User

Proc Tabulate will not do what you want. You could use Proc means/summary, to get year/day sums and a data step to generate cumulative totals and then Proc tabulate or Report to display the result.

 

Perhaps something like:

proc summary data=daybalance nway;
   class day year ;
   var balance;
   output out = daysummary (drop=_: ) sum=;
run;

data want;
   set daysummary;
   by day;
   retain cumbalance;
   if first.day then cumbalance=balance;
   else cumbalance + balance;
run;

And then use the Want set in proc tabulate to display values of Cumbalance created above.

 

Caution: If you include any ALL the result may not be what you expect depending on the dimension and statistic used with cumulative variables.

 

You could use an output set created from Proc Tabulate to feed into the data step but the structure of those sets may be a bit more complicated to work with.

For actual working code you would have to show an example of your starting data in the form of a data step.

 

curious_guy
Calcite | Level 5

i there thank you for your solutions. It works!

Tom
Super User Tom
Super User

Assuming that DAY is character then you could just do it yourself.

/*
proc tabulate data = daybalance;
table year, day*balance;
var balance;
class year day;
run;
*/

proc sql;
create table summary as 
select year
     , sum(case when day in ('Sunday') then balance else 0 end) as Sunday
     , sum(case when day in ('Sunday','Monday') then balance else 0 end) as Monday
     , sum(case when day in ('Sunday','Monday','Tuesday') then balance else 0 end) as Tuesday
     , sum(case when day in ('Sunday','Monday','Tuesday','Wednesday') then balance else 0 end) as Wednesday
     , sum(case when day in ('Sunday','Monday','Tuesday','Wednesday','Thursday') then balance else 0 end) as Thursday
from daybalance
group by year
;
quit;

proc print;
run;
curious_guy
Calcite | Level 5

Thank youu for your support

Ksharp
Super User
/*
You need multi-label format
*/


data have;
do year=2016 to 2017;
 do weekday=1 to 4;
   balance=1;
   output;
 end;
end;
run;

proc format;
value fmt(multilabel)
1='Monday'
1-2='Tuesday'
1-3='Wednesday'
1-4='Thursday'
;
run;


proc tabulate data=have format=32.0;
format weekday fmt.;
class year ;
class weekday/mlf preloadfmt  order=data;
var balance;
table year,weekday*balance;
run;

Ksharp_0-1667822053201.png

 

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 5 replies
  • 1116 views
  • 1 like
  • 4 in conversation