BookmarkSubscribeRSS Feed
mdesessa
Calcite | Level 5

When making a new computed column in SAS enterprise guide, is there a way to make the value of each cell in the column equal the sum of all the previous values in another column (basically a totalizer)?

7 REPLIES 7
esjackso
Quartz | Level 8

Are you creating a dataset or report?

mdesessa
Calcite | Level 5

I'm creating a dataset.

Haikuo
Onyx | Level 15

Not sure how you would do it in EG, but the does the following Base code meet your need?

data test;

do k=1 to 100;

   i=100*round(ranuni(0),0.01);

total_i+i;

   output;

end;

run;

Haikuo

esjackso
Quartz | Level 8

I dont remember a way to do it using the built in capabilities. I would convert the EG task into programming task (I believe it is a right click option for this - add as a code template - which will probably give you SQL code instead of data step coding if the case you might have to write the datastep yourself) . Then add the retained variable and sum to the code like Hai.Kuo mentioned. Here is another example:

data createddata;

     set inputdata;

     /*set the initial value*/

     totalizer = 0;

     /*add each rows total to the running total*/

     totalizer = totalizer + total;

     /*tell sas to retain the old value to use on the next observation*/

     retain totalizer;

run;

esjackso
Quartz | Level 8

If you are creating other things is EG that you dont know how to covert to a datastep - let the group know and we can help with that as well.

Haikuo
Onyx | Level 15

,

At the risk of being obnoxious, I have to say the following 3 statements:

/*set the initial value*/

  totalizer = 0;

  /*add each rows total to the running total*/

  totalizer = totalizer + total;

  /*tell sas to retain the old value to use on the next observation*/

  retain totalizer;

can be replaced by just one:

  totalizer + total;

I supposed you won't get much in term of performance, it is just lesser typing.

Haikuo

esjackso
Quartz | Level 8

Meant to mention they were the same but replied to fast. Thanks for pointing that out Hai.kuo.

I wasnt sure how much data step understanding there was so I broke down what the automatic sum statement does.

EJ

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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