![]() ![]() ![]() The following statements extend the DATA step example and iterate as long as the value of y is less than 20. The condition is checked before each iteration, which implies that you should intialize the stopping condition prior to the loop. You can use the DO statement with a WHILE clause to iterate while a condition is true. There are two ways to do this: you can use the WHILE clause to iterate as long as a certain condition holds, or you can use the UNTIL clause to iterate until a certain condition holds. On occasion, you might want to stop iterating if a certain condition occurs. Variations on the DO loop: DO WHILE and DO UNTIL As mentioned in the previous section, you can also use the BY option to increment the counter by non-unit values and by negative values. In the body of the loop, z is the sum of powers of the elements of x.ĭuring the ith iteration, the elements of x are raised to the ith power. An END statement marks the end of the loop, as shown in the following example: The basic iterative DO statement in SAS has the syntax DO value = start TO stop. Older languages, such as FORTRAN and SAS, call the iteration statement a "do loop," but it is exactly the same concept. The most well-known statement is the "for loop," which is used by C/C++, MATLAB, R, and other languages. Different languages use different keywords to define the iteration statement. Loops are fundamental to programming because they enable you to repeat a computation for various values of parameters. I'll describe looping in the SAS DATA step and compare it with looping in the SAS/IML language. Today is the first anniversary of this blog, which is named The DO Loop, so it seems appropriate to blog about DO loops in SAS. ![]() Whether you need to iterate over parameters in an algorithm or indices in an array, a loop is often one of the first programming constructs that a beginning programmer learns. Looping is essential to statistical programming. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |