I made this widget at MyFlashFetish.com.

Saturday, April 9, 2011

Do...Loop Statement

Repeats a block of statements while a Boolean condition is True or until the condition becomes True.

Use a Do...Loop structure when you want to repeat a set of statements an indefinite number of times, until a condition is satisfied. If you want to repeat the statements a set number of times, the For...Next Statement is usually a better choice.
You can use either While or Until to specify condition, but not both.
You can test condition only one time, at either the start or the end of the loop. If you test condition at the start of the loop (in the Do statement), the loop might not run even one time. If you test at the end of the loop (in the Loop statement), the loop always runs at least one time.
The condition usually results from a comparison of two values, but it can be any expression that evaluates to a Boolean Data Type (Visual Basic) value (True orFalse). This includes values of other data types, such as numeric types, that have been converted to Boolean.
You can nest Do loops by putting one loop within another. You can also nest different kinds of control structures within each other. For more information, seeNested Control Structures (Visual Basic).

DO WHILE

Executes statements repetitively while a condition is true

Valid:
in a DATA step
Category:
Control
Type:
Executable


SYNTAX

DO WHILE (expression);
…..more SAS statements…
END;

ARGUMENTS.

(expression)
is any SAS expression, enclosed in parentheses. You must specify at least one expression.

DETAILS.
The expression is evaluated at the top of the loop before the statements in the DO loop are executed. If the expression is true, the DO loop iterates. If the expression is false the first time it is evaluated, the DO loop does not iterate even once.
COMPARISONS.
There are three other forms of the DO statement:


  • The DO statement, the simplest form of DO-group processing, designates a group of statements to be executed as a unit, usually as a part of IF-THEN/ELSE statements.
  • The iterative DO statement executes statements between DO and END statements repetitively based on the value of an index variable.
  • The DO UNTIL statement executes statements in a DO loop repetitively until a condition is true, checking the condition after each iteration of the DO loop. The DO WHILE statement evaluates the condition at the top of the loop; the DO UNTIL statement evaluates the condition at the bottom of the loop.
  • Note:   If the expression is false, the statements in a DO WHILE loop do not execute. However, because the DO UNTIL expression is evaluated at the bottom of the loop, the statements in the DO UNTIL loop always execute at least once.  


EXAMPLES.
These statements repeat the loop while N is less than 5. The expression N<5 is evaluated at the top of the loop. There are five iterations in all (0, 1, 2, 3, 4).
n=0;
   do while(n<5);
      put n=;
      n+1;
   end;


DO UNTIL

 Executes statements in a DO loop repetitively until a condition is true
Valid:   in a DATA step
Category:         Control
Type:    Executable


DO UNTIL (expression);
...more SAS statements...
END;


ARGUMENTS.
(expression)
is any SAS expression, enclosed in parentheses. You must specify at least one expression.
Details
The expression is evaluated at the bottom of the loop after the statements in the DO loop have been executed. If the expression is true, the DO loop does not iterate again.

Note:   The DO loop always iterates at least once. 
Comparisons
There are three other forms of the DO statement:

The DO statement, the simplest form of DO-group processing, designates a group of statements to be executed as a unit, usually as a part of IF-THEN/ELSE statements.
The iterative DO statement executes statements between DO and END statements repetitively based on the value of an index variable.

The DO WHILE statement executes statements in a DO loop repetitively while a condition is true, checking the condition before each iteration of the DO loop. The DO UNTIL statement evaluates the condition at the bottom of the loop; the DO WHILE statement evaluates the condition at the top of the loop.

Note:   The statements in a DO UNTIL loop always execute at least one time, whereas the statements in a DO WHILE loop do not iterate even once if the condition is false. 

Examples
These statements repeat the loop until N is greater than or equal to 5. The expression N>=5 is evaluated at the bottom of the loop. There are five iterations in all (0, 1, 2, 3, 4).

n=0;
   do until(n>=5);
      put n=;
      n+1;
   end;


 DO WHILE VERSUS DO UNTIL.

The do-while loop is similar to the while loop, except that the test condition occurs at the end of the loop.  Having the test condition at the end, guarantees that the body of the loop always executes at least one time.
The do-while loop is an exit-condition loop.  This means that the body of the loop is always executed first.  Then, the test condition is evaluated.  If the test condition is true, the program executes the body of the loop again.  If the test condition is false, the loop terminates and program execution continues with the statement following the while….

Do...Loop enables the script to continue to perform certain actions until a specific condition occurs. Do While...Loop enables your script to continue to perform these actions as long as the specified condition remains true. Once the specified condition is no longer true, Do While...Loop exits. In contrast, Do Until...Loop has the opposite effect the script continues to perform the action until a certain condition is met….




0 comments:

Post a Comment