BookmarkSubscribeRSS Feed
Azuki707
Calcite | Level 5

I'm trying to calculate the sum of 3 columns. But when I use the summation, some of the records give me wrong result.

It's simple

Sum(0, colA, colB, colC)

ColA, ColB, ColC are blank. So the total should be 0. But I get 2.

Please help!

 

3 REPLIES 3
andreas_lds
Jade | Level 19

Please post the log as text, the data as working data step and the complete code of the step.

 

Amir
PROC Star

Hi,

 

As well as showing your code and any messages from the log using the Insert Code icon "</>" when posting, also confirm that the columns are numeric, as opposed to character, as you said they were blank, whereas the default representation (which can be changed) for a missing numeric value is ".".

 

Try filtering the data to show any non-missing and non-zero values, e.g.:

 

data want;
  set have;
  
  where colA or colB or colC;
run;

 

 

Thanks & kind regards,

Amir.

Rick_SAS
SAS Super FREQ

I don't understand why you say "ColA, ColB, ColC are blank." Do you mean missing values? A numeric variable cannot be "blank."

Here are two examples that demonstrate how the DATA step works for numeric variables (hopefully this is your situation) and character variables (probably a mistake that should be fixed). In both cases, the total is 0 when the variables are all missing.

 

/* the usual case: the variables are numeric */
data Have;
input colA colB colC;
sum = Sum(0, colA, colB, colC); /* variables are numeric */
datalines;
1 2 3
. 2 3
1 . 3
1 2 .
. . .
;

proc print data=Have; run;

/* How does the SUM function behave if given character 
   variables that can be converted to numbers? */
data Have2;
length colA colB colC $1;
infile DATALINES delimiter=',' DSD;
input colA colB colC $;    /* make variables character */
sum = Sum(0, colA, colB, colC);
datalines;
'1', '2', '3'
' ', '2', '3'
'1', ' ', '3'
'1', '2', ' '
' ', ' ', ' ' 
;

proc print data=Have2; run;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 3 replies
  • 713 views
  • 0 likes
  • 4 in conversation