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

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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