Sentry Page Protection
Data Manipulation [18-18]
(+) Expression
Combining the use of the Retain statement and the SUM function allows you to compute the summation across observations.
The (+) Expression presents a shortcut that allows you to achieve the same results.
Again, the REVENUE data set contains the weekly revenue for 10 weeks.
Another way to compute the revenue summation is to use the (+) expression.
Example
Data Revenue2;
Set Revenue;
Total + Revenue;
Run;
The code above achieves the same result without having to use the Retain statement and the Sum function!
Let's take a look at the code one more time:
Total + Revenue;
The code above does the following:
- It creates the variable Total with no initial value (missing)
- The variable Total has a built-in retain capability. Its value is retained from one observation to the next
- The (+ Revenue) syntax adds the revenue to the variable Total on each observation.
As a result, the summation of revenue is computed.
This method presents a short cut in calculating the cumulative summation.
Exercise
Copy and run the INVENTORY data set from the yellow box below:
Copy and run the INVENTORY data set from the yellow box below:
The INVENTORY data set contains the inventory of a bookshelf from a furniture store.
Compute the total inventory across all of the stores using the (+) expression.
Need some help?
HINT:
No Retain statement or Sum function is needed when using the (+) expression.
SOLUTION:
Data Inventory2;
Set Inventory;
Total+Qty;
Run;
Fill out my online form.