BookmarkSubscribeRSS Feed
HeatherNewton
Quartz | Level 8

what does it mean when we specify values in sum?

I tried this and it does not add up, .. 

is there special meaning when we specify like this?

 

fruit=sum(apple='1',orange='2',lemon='3')

fruit is not 6 but show noting in output

2 REPLIES 2
SASKiwi
PROC Star

You can easily test how this works yourself:

28         data _null_;
29           apple = '1';
30           orange = '1';
31           fruit = sum(apple = '1', orange = '1');
32           put _all_;
33         run;

apple=1 orange=1 fruit=2 _ERROR_=0 _N_=1
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds

Summing conditions like apple = '1' turns them into Boolean true = 1 or false = 0 values that are then added. 

ballardw
Super User

@HeatherNewton wrote:

what does it mean when we specify values in sum?

I tried this and it does not add up, .. 

is there special meaning when we specify like this?

 

fruit=sum(apple='1',orange='2',lemon='3')

fruit is not 6 but show noting in output


SAS returns 1 for a true result of a logical comparison and 0 for false. So when apple='1' you get a 1 and 0 otherwise. Summing these comparisons is a way to COUNT how many are true.

This internal creation of a numbers will execute much faster than a bunch of If/then/else that might be another attempt to count such as:

Count=0;
if apple='1' then count=count+1;
if orange='2' then count=count+1;
if lemon='3' then count=count+1;

By "tried this" how many different values of apple, orange and lemon did you try?

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—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
  • 2 replies
  • 827 views
  • 0 likes
  • 3 in conversation