I'm new to sas. I'm trying to do most of it myself but I'm having a little trouble. I want to total up four categories but for each individual value.
This was my code for totaling columns. I don't know how to total rows and I have like 5000 rows. Is it something similar to this?
proc print data=lab.data;
sum candy;
run;
What I have:
Candy | Chips | Fruit | Sandwiches |
101.1 | 128 | 70 | 6 |
107.9 | 146 | 88 | 5 |
116.5 | 138 | 46 | 5 |
110.1 | 132 | 72 | 5 |
80.4 | 100 | 70 | 5 |
92.9 | 116 | 58 | 4 |
86.6 | 110 | 70 | 5 |
What I want:
Candy | Chips | Fruit | Sandwiches | Total of everything eaten |
101.1 | 128 | 70 | 6 | 305.1 |
107.9 | 146 | 88 | 5 | |
116.5 | 138 | 46 | 5 | |
110.1 | 132 | 72 | 5 | |
80.4 | 100 | 70 | 5 | |
92.9 | 116 | 58 | 4 | |
86.6 | 110 | 70 | 5 |
You have to do this in a DATA step, using the SUM function.
data want;
set have;
total=sum(of candy--sandwiches);
run;
or
data want;
set have;
total=sum(candy,chips,fruit,sandwiches);
run;
Also, since you are new, it might help you in the future to use the proper terminology ... candy and chips and fruit and sandwiches are VARIABLES and not categories.
You have to do this in a DATA step, using the SUM function.
data want;
set have;
total=sum(of candy--sandwiches);
run;
or
data want;
set have;
total=sum(candy,chips,fruit,sandwiches);
run;
Also, since you are new, it might help you in the future to use the proper terminology ... candy and chips and fruit and sandwiches are VARIABLES and not categories.
Hi,
would I still use the code below to see the new table. When i do this, it gives me the totals of the columns, not the rows.
proc print data= work.transform;
sum candy chips fruit sandwiches;
run;
Please post the code that creates WORK.TRANSFORM.
Hi,
I appreciate anyone who is taking the time to help me. I'm trying to learn online.
That's just the name of my data table. I had to multiply each variable by a certain number so it was:
DATA transform;
set lab.food;
chips= chips2 *4;
sandwiches= sandwiches2 *4;
fruit= fruit2 *9;
run;
Then i used the below code to show the new table.
proc print data=work.transform;
run;
That's where I got work.transform from.
@Bananas wrote:
Hi,
would I still use the code below to see the new table. When i do this, it gives me the totals of the columns, not the rows.
proc print data= work.transform;
sum candy chips fruit sandwiches;
run;
I gave you code that works to sum across the rows. PROC PRINT, as you can see, only sums columns.
Hi Paige,
thank you. Sorry to be a bother, I'm just having a hard time actually seeing the totals of the rows individually. The code you gave me did work as the log said. I will figure out how to see it in the output. Thanks for your help again.
PROC PRINT or any dataviewer in SAS will show you the values in the new data set which I named WANT.
Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.