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.
proc summary data=sashelp.class;
var age weight height;
output out=totals sum=;
run;
you can replace age weight height with your columns
@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.
Actually I want it run in data steps. but it seems like not recommended.
@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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.