During the execution of the DATA step processing, the DATA step works like a loop, repetitively reading the data and creating observations one at a time. We call this type of loop the implicit loop. Sometimes we need to execute certain SAS® statements repeatedly. In this situation, we need to construct an explicit loop by using the DO, DO WHILE, or DO UNTIL statements. There is a wide range of applications for explicit loops, such as generating random samples, reading multiple external data files, and so forth. However, in some scenarios, creating an explicit loop can be very tricky, even for seasoned programmers. Constructing a successful loop is dependent upon grasping SAS® programming fundamentals, such as understanding that the SAS data set is created one observation at a time in the program data vector (PDV). In this paper, you will learn how to create loops with various applications and what happens in the PDV when creating the explicit loop.
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
Get the scoop on the loop how best to write a loop in the data step
1. Get the Scoop on the Loop How Best to Write a Loop in the DATA Step Arthur Li Department of Information Science City of Hope Comprehensive Cancer Center Duarte, CA
2.
3.
4.
5.
6.
7.
8.
9.
10. IMPLICIT LOOP data trial1 (drop=rannum); set patient; rannum = ranuni( 2 ); if rannum> 0.5 then group = 'D' ; else group = 'P' ; run ; Patient: PDV: COMPILATION: D = dropped K = kept M1240 4 F2340 3 F2390 2 M2390 1 ID K GROUP D RANNUM K ID D _ERROR_ D _N_
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25. EXPLICIT LOOP data trial2(drop = rannum); id = 'M2390' ; rannum = ranuni( 2 ); if rannum> 0.5 then group = 'D' ; else group = 'P' ; output ; id = 'F2390' ; rannum = ranuni( 2 ); if rannum> 0.5 then group = 'D' ; else group = 'P' ; output ; id = 'F2340' ; rannum = ranuni( 2 ); if rannum> 0.5 then group = 'D' ; else group = 'P' ; output ; id = 'M1240' ; rannum = ranuni( 2 ); if rannum> 0.5 then group = 'D' ; else group = 'P' ; output ; run ; Assigning IDs in the DATA step
26. EXPLICIT LOOP data trial2(drop = rannum); id = 'M2390' ; rannum = ranuni( 2 ); if rannum> 0.5 then group = 'D' ; else group = 'P' ; output ; id = 'F2390' ; rannum = ranuni( 2 ); if rannum> 0.5 then group = 'D' ; else group = 'P' ; output ; id = 'F2340' ; rannum = ranuni( 2 ); if rannum> 0.5 then group = 'D' ; else group = 'P' ; output ; id = 'M1240' ; rannum = ranuni( 2 ); if rannum> 0.5 then group = 'D' ; else group = 'P' ; output ; run ; 4 explicit OUTPUT statements
27.
28.
29.
30. ITERATIVE DO LOOP data trial2(drop = rannum); id = 'M2390' ; rannum = ranuni( 2 ); if rannum> 0.5 then group = 'D' ; else group = 'P' ; output ; id = 'F2390' ; ... id = 'F2340' ; ... id = 'M1240' ; rannum = ranuni( 2 ); if rannum> 0.5 then group = 'D' ; else group = 'P' ; output ; run ; DO INDEX-VARIABLE = VALUE1, VALUE2, …, VALUEN ; SAS STATEMENTS END; data trial2 (drop = rannum); do id = 'M2390' , 'F2390' , 'F2340' , 'M1240' ; rannum = ranuni( 2 ); if rannum> 0.5 then group = 'D' ; else group = 'P' ; output ; end ; run ;
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52. DO WHILE DO WHILE (EXPRESSION) ; SAS STATEMENTS END; data trial3 (drop = rannum); do id = 1 to 4 ; rannum = ranuni( 2 ); if rannum> 0.5 then group = 'D' ; else group = 'P' ; output ; end ; run ; data trial4 (drop=rannum); do while (id < 4 ); id + 1 ; rannum = ranuni( 2 ); if rannum> 0.5 then group = 'D' ; else group = 'P' ; output ; end ; run ; Iterative DO loop: DO WHILE loop:
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67. DO UNTIL DO UNTIL (EXPRESSION) ; SAS STATEMENTS END; data trial3 (drop = rannum); do id = 1 to 4 ; rannum = ranuni( 2 ); if rannum> 0.5 then group = 'D' ; else group = 'P' ; output ; end ; run ; data trial4 (drop=rannum); do while (id < 4 ); id + 1 ; rannum = ranuni( 2 ); if rannum> 0.5 then group = 'D' ; else group = 'P' ; output ; end ; run ; Iterative DO loop: DO WHILE loop: data trial5 (drop=rannum); do until (id >= 4 ); id + 1 ; rannum = ranuni( 2 ); if rannum > 0.5 then group = 'D' ; else group = 'P' ; output ; end ; run ; DO UNTIL loop: Will not continue if the EXPRESSION is false Will not continue for another iteration if the EXPRESSION is true