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
PROC Star

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

 

Amir
Ammonite | Level 13

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 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

Discussion stats
  • 3 replies
  • 202 views
  • 0 likes
  • 4 in conversation