Desktop productivity for business analysts and programmers

Issue with Sum of function

Reply
Contributor
Posts: 41

Issue with Sum of function

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?

Super User
Posts: 6,962

Re: Issue with Sum of function

[ Edited ]

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
 

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 41

Re: Issue with Sum of function

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.
Super User
Posts: 10,520

Re: Issue with Sum of function

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.

Super User
Posts: 5,085

Re: Issue with Sum of function

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.

Ask a Question
Discussion stats
  • 4 replies
  • 91 views
  • 0 likes
  • 4 in conversation