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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 2492 views
  • 0 likes
  • 3 in conversation