BookmarkSubscribeRSS Feed
Sean_OConnor
Fluorite | Level 6

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
Fluorite | Level 6
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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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