Calcite | Level 5

Error in simple Sum

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.

3 REPLIES 3

Re: Error in simple Sum

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

PROC Star

Re: Error in simple Sum

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.

SAS Super FREQ

Re: Error in simple Sum

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;``````
Discussion stats
• 3 replies
• 292 views
• 0 likes
• 4 in conversation