Creating and Modifying Variables

Accumulating Totals

To add the result of an expression to an accumulator variable, you can use a sum statement in your DATA step.
Syntax, sum statement:
variable+expression;
  • variable specifies the name of the accumulator variable. This variable must be numeric. The variable is automatically set to 0 before the first observation is read. The variable's value is retained from one DATA step execution to the next.
  • expression is any valid SAS expression.
Note: If the expression produces a missing value, the sum statement ignores it. The sum statement is one of the few SAS statements that do not begin with a keyword.
The sum statement adds the result of the expression that is on the right side of the plus sign (+) to the numeric variable that is on the left side of the plus sign. The value of the accumulator variable is initialized to 0 instead of missing before the first iteration of the DATA step. Subsequently, the variable’s value is retained from one iteration to the next, as if it had appeared in a RETAIN statement.

Example: Sum Statement

To find the total number of elapsed seconds in treadmill stress tests, you need a variable (in this example, SumSec) whose value begins at 0 and increases by the amount of the total seconds in each observation. To calculate the total number of elapsed seconds in treadmill stress tests, you use the sum statement shown below:
data clinic.stress; 
   infile tests; 
   input ID $ 1-4 Name $ 6-25 RestHR 27-29 MaxHR 31-33 
            RecHR 35-37 TimeMin 39-40 TimeSec 42-43 
            Tolerance $ 45; 
   TotalTime=(timemin*60)+timesec; 
   SumSec+totaltime; 
run;
The value of the variable on the left side of the plus sign (here, SumSec) begins at 0 and increases by the value of TotalTime with each observation.
SumSec
=
TotalTime
+
Previous total
0
758 
=
758
+
0
1363 
=
605
+
758
2036
=
673
+
1363
2618
=
582
+
2036
3324
=
706
+
2618

Initializing Sum Variables

In the previous example, the sum variable SumSec was initialized to 0 by default before the first observation was read. But what if you want to initialize SumSec to a different number, such as the total seconds from previous treadmill stress tests?
You can use the RETAIN statement to assign an initial value other than the default value of 0 to a variable whose value is assigned by a sum statement.
The RETAIN statement serves several purposes:
  • It assigns an initial value to a retained variable.
  • It prevents variables from being initialized each time the DATA step executes.
Syntax, RETAIN statement for initializing sum variables:
RETAIN variable <initial-value>;
  • variable is a variable whose values you want to retain.
  • initial-value specifies an initial value (numeric or character) for the preceding variable.
Note: The following statements are true about the RETAIN statement:
  • It is a compile-time-only statement that creates variables if they do not already exist.
  • It initializes the retained variable to missing before the first execution of the DATA step if you do not supply an initial value.
  • It has no effect on variables that are read with SET, MERGE, or UPDATE statements.

Example: RETAIN Statement

Suppose you want to add 5400 seconds (the accumulated total seconds from a previous treadmill stress test) to the variable SumSec in the Clinic.Stress data set when you create the data set. To initialize SumSec with the value 5400, you use the RETAIN statement shown below.
data clinic.stress; 
   infile tests; 
   input ID $ 1-4 Name $ 6-25 RestHR 27-29 MaxHR 31-33 
         RecHR 35-37 TimeMin 39-40 TimeSec 42-43 
         Tolerance $ 45; 
   TotalTime=(timemin*60)+timesec; 
   retain SumSec 5400;     
   sumsec+totaltime; 
run;
Now the value of SumSec begins at 5400 and increases by the value of TotalTime with each observation.
SumSec
=
TotalTime
+
Previous Total
5400
6158
=
758
+
5400
6763
=
605
+
6158
7436
=
673
+
6763
8018
=
582
+
7436
8724
=
706
+
8018
Last updated: January 10, 2018
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.164.34