BookmarkSubscribeRSS Feed
BrahmanandaRao
Lapis Lazuli | Level 10
data MyData; 
  input Var1 Var2 Var3;
  datalines;
  10 20 30
  15 25 35
  5  15 10
;
run;

how to columns sum in data step method 

4 REPLIES 4
LinusH
Tourmaline | Level 20

It's not advisable to create columns totals in a data step, unless you wish to create a report layout directly (not for storage).

There are variety of SAS PROCs that does this for you, including PRINT.

Data never sleeps
ballardw
Super User

Some reasons to NOT do such.

First, to have a chance of debugging issues that will come up you need to have one additional variable for every one that want to sum and keep the values with RETAIN. Then when the end of the data set is reached you have to move that value into the correct column. Not impossible, just cumbersome.

 

People tend to use + from habit. In this situation you would need to decide if the + is correct as if you ever have a missing value then you will have missing values to the end of the data set. So use of the SUM function with the retained variable is pretty much required.

 

If you later use that data set for pretty much anything except Proc Print the added values will distort any statistics because that summed value will be included. This can be critically important if anyone other than you uses that data set as the are very unlikely to realize that you have internal summary data included.

Kurt_Bremser
Super User

In a dataset, such a "totals" observation is not needed (and in fact causes a dataset to be unusable for further analysis), and for reports, you use reporting procedures.

 

To calculate a sum while processing a dataset for any other use, use RETAINed variables.

Tom
Super User Tom
Super User

Make a new variable and sum them.  If you use SUM statements to do the summation (var + expression;) then the variables are automatically retained. 

data sums;
  set mydata end=eof;
  sum1+var1;
  sum2+var2;
  sum3+var3;
  if eof ;
  drop var1-var3;
run;

If you have more than about 3 or 5 variables then it probably is less typing to define arrays.

data sums;
  set mydata end=eof;
  array raw var1-var20 ;
  array sums sum1-sum20;
  do over sums;
    sums+raw;
  end;
  if eof ;
  drop var1-var20;
run;

Example:

data MyData; 
  input Var1 Var2 Var3;
datalines;
10 20 30
15 25 35
5  15 10
;

data sums;
  set mydata end=eof;
  array raw var1-var3 ;
  array sums sum1-sum3;
  do over sums;
    sums+raw;
  end;
  if eof ;
  drop var1-var3;
run;

Results

Obs    sum1    sum2    sum3

 1      30      60      75

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 4 replies
  • 400 views
  • 0 likes
  • 5 in conversation