Sum Only Certain Values in a SAS-EG Column?

Reply
New Contributor
Posts: 3

Sum Only Certain Values in a SAS-EG Column?

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)?

Super Contributor
Posts: 333

Re: Sum Only Certain Values in a SAS-EG Column?

Are you creating a dataset or report?

New Contributor
Posts: 3

Re: Sum Only Certain Values in a SAS-EG Column?

I'm creating a dataset.

Respected Advisor
Posts: 3,124

Re: Sum Only Certain Values in a SAS-EG Column?

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

Super Contributor
Posts: 333

Re: Sum Only Certain Values in a SAS-EG Column?

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;

Super Contributor
Posts: 333

Re: Sum Only Certain Values in a SAS-EG Column?

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.

Respected Advisor
Posts: 3,124

Re: Sum Only Certain Values in a SAS-EG Column?

,

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

Super Contributor
Posts: 333

Re: Sum Only Certain Values in a SAS-EG Column?

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

Ask a Question
Discussion stats
  • 7 replies
  • 1136 views
  • 0 likes
  • 3 in conversation