# How many SAS do loops you know?

Viewing 3 posts - 1 through 3 (of 3 total)
• Author
Posts
• #49300

Four types:-
1.basic iterative DO statement in SAS has the syntax DO value = start TO stop
do i = 1 to 4;
answer + 3;
end;

do j = 2 to 12 by 2;

do i = 20 to 1 by -1;
output;
end;

DO i = 1, 2, 3, 4, 5;

DO j = ‘a’, ‘b’, ‘c’, ‘d’;

eg;
DATA abc;
DO i = 1 to 6;
DO j = 1 to 9;
output;
END;
END;
RUN;

PROC PRINT data = abc;
TITLE ‘it is done!’;
RUN;

2.DO WHILE-

DO statement with a WHILE clause to iterate while a condition is true. the expression, in this case is evaluated at the top of the loop.
The condition is checked before each iteration, which implies that you should intialize the stopping condition prior to the loop.

3.Do until-

DO UNTIL loop, SAS executes the DO loop until the expression you’ve specified is true. Here’s the general form of a DO UNTIL loop:

DO UNTIL (expression);
action statements;
END;
where expression is any valid SAS expression enclosed in parentheses. The key thing to remember is that the expression is not evaluated until the bottom of the loop.

Therefore, a DO UNTIL loop always executes at least once. As soon as the expression is determined to be true, the DO loop does not execute again.

#49302

4.Do over loops with array

data test;
set temp;
array nvars _numeric_;
do over nvars;
if nvars > 3 then nvars = .;
end;
run;

courtesy- Listendata

#49304

do i = 1 to 5 by 0.5 while(y < 20); y = i**2; /* values are 1, 2.25, 4, ..., 16, 20.5 */ output; end;

Viewing 3 posts - 1 through 3 (of 3 total)
• You must be logged in to reply to this topic.