BookmarkSubscribeRSS Feed
Sean_OConnor
Obsidian | Level 7

Folks,

 

I'm having an issue with sum of function with SAS. For example the following code produces the correct output for some sample data.

 

DATA LIST;
 INPUT X Y Z;
DATALINES;
1 2 3
4 5 6
;
run;

data i;
set list;
u=sum(of x y z);run;

However, when I attempt to run it with my own data what happens is that the variables which are on line 1 are summed, but the total of the new variable is pushed onto line 2.

 

For instance what's happening is 

 

DATA LIST;
 INPUT X Y Z;
DATALINES;
1 2 3
4 5 6
;
run;

data i;
set list;
u=sum(of x y z);run;

x y z u

1 2 3 .

4 5 6 6

 .  .  . 15

 

Has anyone any idea why this is happening?

4 REPLIES 4
Kurt_Bremser
Super User

There's something else happening (the code you ran is not exactly what you posted).

Because this code:

DATA LIST;
 INPUT X Y Z;
DATALINES;
1 2 3
4 5 6
;
run;

data i;
set list;
u=sum(of x y z);
run;

proc print data=i noobs;
run;

gives me this result:

X    Y    Z     u

1    2    3     6
4    5    6    15
 

 

Sean_OConnor
Obsidian | Level 7
Sorry Kurt,

I appear to have found the issue. I have blanks (.) in my dataset rather than 0.

It appears blanks push the data down while zeros don't.
ballardw
Super User

Are you doing these calculations in the same step that you read an external file? It may be that the data you think are "blanks" are actually getting interpretted as end of line for the input.

 

It never hurts to show the exact code you ran from the log with any messages or notes.

Astounding
PROC Star

No, it won't work like that.  I suspect the program that you actually ran switched the order of two statements:

 

data i;

u = sum(of x y z);

set list;

run;

 

Even that will give  you only two observations (not three), but it will shift the values of U by one observation.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1475 views
  • 0 likes
  • 4 in conversation