BookmarkSubscribeRSS Feed
ursula
Pyrite | Level 9

Hi,

I have a 30 column dataset and want to total each column values.

ID C1 C2 C4 C4 C5
001 12   5   4
002 11 10 5 3 1
003 2   3   2
004   5 3 6  
005 4 5   2 6

 

what I want is:

ID C1 C2 C4 C4 C5
001 12   5   4
002 11 10 5 3 1
003 2   3   2
004   5 3 6  
005 4 5   2 6
total 29 20 16 11 13

 

I wonder how to code in SAS for total each column (I have 30 columns, C1 to C30).

 

Thanks in advance.

5 REPLIES 5
Cynthia_sas
SAS Super FREQ
Hi:
If you're using PROC PRINT, then you need the SUM statement. If you are using PROC REPORT, then you need an RBREAK statement. If you are using PROC TABULATE, then you need the special class variable ALL in the row dimension.

What code have you tried?
Cynthia
VDD
Ammonite | Level 13 VDD
Ammonite | Level 13
proc summary data=sashelp.class;
var age weight height;
output out=totals sum=;
run;

you can replace age weight height with your columns

 

Reeza
Super User

@ursula wrote:

Hi,

I have a 30 column dataset and want to total each column values.

ID C1 C2 C4 C4 C5
001 12   5   4
002 11 10 5 3 1
003 2   3   2
004   5 3 6  
005 4 5   2 6

 

what I want is:

ID C1 C2 C4 C4 C5
001 12   5   4
002 11 10 5 3 1
003 2   3   2
004   5 3 6  
005 4 5   2 6
total 29 20 16 11 13

 

I wonder how to code in SAS for total each column (I have 30 columns, C1 to C30).

 

Thanks in advance.


Do you want that as a result printed to an output destination such as HTML, Listing, Excel, Word, PowerPoint or PDF. 

Or are you trying to get that included into a data set - which is generally not recommended.

 

ursula
Pyrite | Level 9

Actually I want it run in data steps. but it seems like not recommended. 

ballardw
Super User

@ursula wrote:

Actually I want it run in data steps. but it seems like not recommended. 


The reason that including a Total or other summary value in the same variable is that if you don't use the data set for awhile and you forget the total is there, or some else uses it without knowledge may run code such as:

 

proc means data=<that data set> mean=;

   var c1 - c30;

run;

 

Which would include the TOTAL into the calculation for the means. Which would yield very bad results.

And even if you do know the total is there, you need to do modifications to the code to exclude it.

Generally such things are needed by PEOPLE, not computer programs. When a routine, such as regression, needs such a statistic it will calculate it as needed from the data (and again, if a total is there throw of the calculations).

Use a report procedure such as Proc Print, Report or Tabulate which will calculate totals without modifying your data, unless you know that a further computation in the process needs that total as the same variable.

 

I might create a summary value in a data set if I need to create an obnoxious complicated report layout, but I would also place that into a different variable to prevent accidental misuse.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 5 replies
  • 785 views
  • 5 likes
  • 5 in conversation