All tables are dynamically created using loops and variable assignments within DATA steps.
1 Code Block
DATA STEP Data
Explanation : Creation of table 'simple1' with a simple loop from 1 to 4. Without an explicit OUTPUT statement inside the loop, writing to the table occurs implicitly at the end of the DATA step (a single final observation containing the last values of i and answer).
Copied!
/*
Demonstration: Simple Illustration of How DO Loops Work
*/
data simple1;
do i = 1 to 4;
answer = 1 + i;
end;
run;
proc print data=simple1 noobs;
run;
1
/*
2
Demonstration: Simple Illustration of How DO Loops Work
3
*/
4
DATA simple1;
5
DO i = 1 to 4;
6
answer = 1 + i;
7
END;
8
RUN;
9
10
PROC PRINTDATA=simple1 noobs;
11
RUN;
2 Code Block
DATA STEP Data
Explanation : Creation of 'simple2'. Adding the OUTPUT statement inside the loop forces an observation to be written at each iteration. The resulting table will therefore have 4 rows.
Copied!
/*
Demonstration: Using the Output Statement
*/
data simple2;
do i = 1 to 4;
answer = 1 + i;
output;
end;
run;
proc print data=simple2 noobs;
run;
1
/*
2
Demonstration: Using the Output Statement
3
*/
4
DATA simple2;
5
DO i = 1 to 4;
6
answer = 1 + i;
7
OUTPUT;
8
END;
9
RUN;
10
11
PROC PRINTDATA=simple2 noobs;
12
RUN;
3 Code Block
DATA STEP Data
Explanation : Similar to the previous example, but the iteration variable 'i' is dropped from the final table using the (drop=i) option.
Copied!
*Simple example #3;
data simple3 (drop=i);
do i = 1 to 4;
answer = 1 + i;
output;
end;
run;
proc print data=simple3 noobs;
run;
1
*Simple example #3;
2
DATA simple3 (drop=i);
3
DO i = 1 to 4;
4
answer = 1 + i;
5
OUTPUT;
6
END;
7
RUN;
8
9
PROC PRINTDATA=simple3 noobs;
10
RUN;
4 Code Block
DATA STEP Data
Explanation : Demonstration of a loop with an increment (BY) of 2. The loop iterates for i = 0, 2, 4, ..., 10.
Copied!
*Simple example #4;
data simple4 (drop=i);
do i = 0 to 10 by 2;
answer = 1 + i;
output;
end;
run;
proc print data=simple4 noobs;
run;
1
*Simple example #4;
2
DATA simple4 (drop=i);
3
DO i = 0 to 10BY2;
4
answer = 1 + i;
5
OUTPUT;
6
END;
7
RUN;
8
9
PROC PRINTDATA=simple4 noobs;
10
RUN;
5 Code Block
DATA STEP Data
Explanation : Using a decimal increment (0.25). The loop iterates more finely between 0 and 10.
Copied!
*Simple example #5;
data simple5 (drop=i);
do i = 0 to 10 by .25;
answer = 1 + i;
output;
end;
run;
proc print data=simple5 noobs;
run;
1
*Simple example #5;
2
DATA simple5 (drop=i);
3
DO i = 0 to 10BY .25;
4
answer = 1 + i;
5
OUTPUT;
6
END;
7
RUN;
8
9
PROC PRINTDATA=simple5 noobs;
10
RUN;
6 Code Block
DATA STEP Data
Explanation : Example of a decremental loop (countdown) using a negative step (BY -1), ranging from 10 to 1.
Copied!
*Simple example 6;
data simple6 (drop=i);
do i = 10 to 1 by -1;
answer = 1 + i;
output;
end;
run;
proc print data=simple6 noobs;
run;
1
*Simple example 6;
2
DATA simple6 (drop=i);
3
DO i = 10 to 1BY -1;
4
answer = 1 + i;
5
OUTPUT;
6
END;
7
RUN;
8
9
PROC PRINTDATA=simple6 noobs;
10
RUN;
7 Code Block
DATA STEP Data
Explanation : Iteration over an explicit list of non-sequential values (10, 12, 23, 147).
Copied!
*Simple example 7;
data simple7 (drop=i);
do i = 10, 12, 23, 147;
answer = 1 + i;
output;
end;
run;
proc print data=simple7 noobs;
run;
1
*Simple example 7;
2
DATA simple7 (drop=i);
3
DO i = 10, 12, 23, 147;
4
answer = 1 + i;
5
OUTPUT;
6
END;
7
RUN;
8
9
PROC PRINTDATA=simple7 noobs;
10
RUN;
8 Code Block
DATA STEP Data
Explanation : Illustration of nested loops. Note: the inner loop 'do j = i**2;' is not a classic iterative loop (no 'to'), it executes once by assigning the calculated value to j. The OUTPUT is triggered at each pass.
Copied!
*Nesting do loops;
data nested;
do i = 1 to 10;
do j = i**2;
output;
end;
end;
run;
proc print data=nested noobs;
run;
1
*Nesting do loops;
2
DATA nested;
3
DO i = 1 to 10;
4
DO j = i**2;
5
OUTPUT;
6
END;
7
END;
8
RUN;
9
10
PROC PRINTDATA=nested noobs;
11
RUN;
9 Code Block
DATA STEP Data
Explanation : Usage of DO WHILE. The condition is evaluated at the beginning of the loop. If 'answer' is missing initially, the condition might be false immediately or require initialization (here, SAS implicitly initializes to 0 for the sum instruction 'answer + 1', so the loop works).
Copied!
*Example of using do while;
data simple8;
do while (answer < 11);
answer + 1;
output;
end;
run;
proc print data=simple8 noobs;
run;
1
*Example of using do while;
2
DATA simple8;
3
DO while (answer < 11);
4
answer + 1;
5
OUTPUT;
6
END;
7
RUN;
8
9
PROC PRINTDATA=simple8 noobs;
10
RUN;
10 Code Block
DATA STEP Data
Explanation : Usage of DO UNTIL. The condition is evaluated at the end of the loop, ensuring that the code inside executes at least once.
Copied!
*Example of using do until;
data simple9;
do until (answer > 9);
answer + 1;
output;
end;
run;
proc print data=simple9 noobs;
run;
1
*Example of using do until;
2
DATA simple9;
3
DO until (answer > 9);
4
answer + 1;
5
OUTPUT;
6
END;
7
RUN;
8
9
PROC PRINTDATA=simple9 noobs;
10
RUN;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.