BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Bananas
Fluorite | Level 6

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:

CandyChipsFruitSandwiches
101.1128706
107.9146885
116.5138465
110.1132725
80.4100705
92.9116584
86.6110705

 

What I want:

CandyChipsFruitSandwichesTotal of everything eaten
101.1128706305.1
107.9146885 
116.5138465 
110.1132725 
80.4100705 
92.9116584 
86.6110705 
1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller

View solution in original post

7 REPLIES 7
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
Bananas
Fluorite | Level 6

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;

Bananas
Fluorite | Level 6

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.

PaigeMiller
Diamond | Level 26

@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.

--
Paige Miller
Bananas
Fluorite | Level 6

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. 

PaigeMiller
Diamond | Level 26

PROC PRINT or any dataviewer in SAS will show you the values in the new data set which I named WANT.

--
Paige Miller

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!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 7 replies
  • 1065 views
  • 1 like
  • 3 in conversation