When we do
programming, you want that a block of code is executed several number of times.
In general statements are executed sequentially like the first statement in a
function is executed first, followed by another statement and so on. In that
case we need to understand the concepts of Looping. In Looping there are
different keywords to define the iteration or loop statement. The most wee
known statement is the “for loop”
which is used by many language like C/C++, MATLA, R Java etc. There is similar
concepts in SAS like other language to define the loop. But in SAS looping is
done by DO Statement. And it is also
called DO Loop. There are four types of
DO Statement.
DO Statement
Do Statement classifies a group of statement which execute a
group of statement a certain number of times? The syntax for DO statement is
DO;
SAS Statement…;
END
An end statement represents the end of the loop for example:
DATA _null_;
do x = 1;
put x =;
end;
run;
Output
x = 1
|
The
output of this program is shown in Log File on SAS. Let take an another example
like if I remove the puts statements what you will get?
DATA _null_;
do x = 1;
put x =;
end;
run;
Output
x = 1
|
By default
you will get the value of x equals to 1. If I assign more than one value of x
like 1, 2,3 then you will see that It
print the all the values of x in output.
DATA _null_;
do x =
1,2,3;
put x =;
end;
run;
Output
x = 1
x = 2
x = 3
|
Now I want
to apply loop in string values how can I define it.
DATA _null_;
do x =
“AAA”,”BBBB”,”CCCC”;
put x =;
end;
run;
Output
x = AAA
x = BBBB
x = CCCC
|
Suppose we
want to print the values from 1 to 100. In that case it really hectic to
someone to assign all the values in a particular variable one by one. To solve
this issues we have another solution to set the range.
DATA _null_;
do x = 1 to
5;
put i =;
end;
run;
output
i = 1
i = 2
i = 3
i = 4
i = 5
|
Iterative DO Loop
The iterative DO Loop executes the statement between DO and
End repetively baes on the value of the index values. The syntax for Iterative
DO Loop statement is:
DO Index-variable = Start
TO Stop <By Increment>;
SAS Statement…;
END
By default,
each iteration of a DO statement increments the value of the counter by 1, but
you can also use the BY statement option to increment the counter by other
amounts. For example, each iteration the following DATA step increments the
values by 0.5. For example:
DATA _null_;
do i = 1 to
10 by 2;
put i =;
end;
run;
output
i = 1
i = 3
i = 5
i = 7
i = 9
|
DO While Loop
The loop
continues till the while condition becomes False. It almost works like the
iterative DO statement, except that you don’t need to specify an index-variable
or start and stop. The syntax of DO While Loop is:
DO WHILE (expression)
----SAS Statement----
END;
data _null_;
n=0;
do while(n<5);
put n=;
n+1;
end;
run;
output
n = 1
n = 2
n = 3
n = 4
|
In while loop the expression is
evaluated before the loop executes, and If the expression is false the first time
it is evaluated, then the loop will not execute at all.
No comments:
Post a Comment