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-2024.png

 

Secure your spot at the must-attend AI and analytics event of 2024: SAS Innovate 2024! Get ready for a jam-packed agenda featuring workshops, super demos, breakout sessions, roundtables, inspiring keynotes and incredible networking events.

 

Register by March 1 to snag the Early Bird rate of just $695! Don't miss out on this exclusive offer. 

 

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.

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