SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
RAVI2000
Lapis Lazuli | Level 10

There are 2 variables A and B, and I create variable C using + operator, variable D using sum
function and E using sum statement. What will be result?

I know the difference between "+" and SUM function.

"+": If there are any missing values it returns missing result.

SUM function: It doesn't consider missing values and averages the remaining data.

Some information about SUM statement is, it has default functionality as RETAIN statement. 

I want to know how it is working. Can any one give me a scenario?

Thankyou!

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

@RAVI2000 wrote:
How can I explain the SUM statememt as?
It gives cumulative sum by retaining ?

It gives a cumulative sum by retaining. In the code below, it adds the value of X to the previous value of CUMULATIVE_SUM

 

data a;
    input x;
    cumulative_sum+x;
cards;
5
1
-7
12.4
;

 

A slight correction

 

SUM function: It doesn't consider missing values and averages the remaining data.

SUM function: It doesn't consider missing values and sums the remaining data.

--
Paige Miller

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

Here a typical use of the SUM statement to create a running count and sum per group:

data want;
set have;
by id;
if first.id
then do;
  count = 1;
  sum_amount = amount;
end;
else do;
  count + 1;
  sum_amount + amount;
end;
run;

As the SUM statement implies a retain, no separate RETAIN is needed.

RAVI2000
Lapis Lazuli | Level 10
How can I explain the SUM statememt as?
It gives cumulative sum by retaining ?
PaigeMiller
Diamond | Level 26

@RAVI2000 wrote:
How can I explain the SUM statememt as?
It gives cumulative sum by retaining ?

It gives a cumulative sum by retaining. In the code below, it adds the value of X to the previous value of CUMULATIVE_SUM

 

data a;
    input x;
    cumulative_sum+x;
cards;
5
1
-7
12.4
;

 

A slight correction

 

SUM function: It doesn't consider missing values and averages the remaining data.

SUM function: It doesn't consider missing values and sums the remaining data.

--
Paige Miller
RAVI2000
Lapis Lazuli | Level 10
Thank you @Kurt , for the clear explanation.

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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