Constructing DO Loops

DO Loop Execution

Here is how the DO loop executes in the DATA step. This example sums the interest that was earned each month for a one-year investment.
data work.earnings; 
  Amount=1000; 
  Rate=0.75/12; 
  do month=1 to 12; 
    Earned+(amount+earned)*rate; 
  end; 
run;
This DATA step does not read data from an external source. When submitted, it compiles and then executes only once to generate data. During compilation, the program data vector is created for the Work.Earnings data set.
Program Data Vector
When the DATA step executes, the values of Amount and Rate are assigned.
Program Data Vector
Next, the DO loop executes. During each execution of the DO loop, the value of Earned is calculated and is added to its previous value. Then the value of Month is incremented. On the 12th execution of the DO loop, the value of Month is incremented to 12 and the value of Earned is 1069.839.
Program Data Vector
After the 12th execution of the DO loop, the value of Month is incremented to 13. Because 13 exceeds the stop value of the iterative DO statement, the DO loop stops executing, and processing continues to the next DATA step statement. The end of the DATA step is reached, the values are written to the Work.Earnings data set, and in this example, the DATA step ends. Only one observation is written to the data set.
Figure 11.1 SAS Data Set Work.Earnings
SAS Data Set Work.Earnings
Notice that the index variable Month is also stored in the data set. In most cases, the index variable is needed only for processing the DO loop and can be dropped from the data set.

Using Explicit OUTPUT Statements

To create an observation for each iteration of the DO loop, place an OUTPUT statement inside the loop. By default, every DATA step contains an implicit OUTPUT statement at the end of the step. But placing an explicit OUTPUT statement in a DATA step overrides automatic output, causing SAS to add an observation to the data set only when the explicit OUTPUT statement is executed.
The previous example created one observation because it used automatic output at the end of the DATA step. In the following example, the OUTPUT statement overrides automatic output, so the DATA step writes 20 observations.
data work.earn; 
  Value=2000; 
  do Year=1 to 20; 
    Interest=value*.075; 
    value+interest; 
    output; 
  end; 
run;
proc print data=work.earn;
run;
Figure 11.2 HTML Output: OUTPUT Statement inside Each DO Loop (partial output)
The OUTPUT statement overrides automatic output, so the DATA step writes 20 observations.

Decrementing DO Loops

You can decrement a DO loop's index variable by specifying a negative value for the BY clause. For example, the specification in this iterative DO statement decreases the index variable by 1, resulting in values of 5, 4, 3, 2, and 1. The following brief examples show you the syntax.
DO index-variable=5 to 1 by -1; 
   ...more SAS statements... 
END;
When you use a negative BY clause value, the start value must always be greater than the stop value in order to decrease the index variable during each iteration.
DO index-variable=5 to 1 by -1; 
   ...more SAS statements... 
END;

Specifying a Series of Items

You can also specify how many times a DO loop executes by listing items in a series.
Syntax, DO loop with a variable list:
DO index-variable=value1, value2, value3... ;
...more SAS statements...
END;
values can be character or numeric.
When the DO loop executes, it executes once for each item in the series. The index variable equals the value of the current item. You must use commas to separate items in the series.
To list items in a series, you must specify one of the following, as shown in the syntax:
  • all numeric values.
    DO index-variable=2,5,9,13,27; 
       ...more SAS statements... 
    END;
  • all character values, which are enclosed in quotation marks.
    DO index-variable='MON','TUE','WED','THR','FRI'; 
       ...more SAS statements... 
    END;
  • all variable names. The index variable takes on the values of the specified variables.
    DO index-variable=win,place,show; 
       ...more SAS statements... 
    END;
Variable names must represent either all numeric or all character values. Do not enclose variable names in quotation marks.
Last updated: August 23, 2018
..................Content has been hidden....................

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