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!
Please post the log as text, the data as working data step and the complete code of the step.
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.
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;
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!
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.
Ready to level-up your skills? Choose your own adventure.