Examples use generated data (datalines) or SASHELP.
1 Code Block
DATA STEP Data
Explanation : This example uses an iterative DO statement to repeatedly decrement the values of the 'balance' variable and write these values to the output dataset. The DATA step executes on the SAS compute server.
Copied!
data loan;
balance=10000;
do payment_number=1 to 10;
balance=balance-1000;
output;
end;
run;
proc print data=loan;
run;
1
DATA loan;
2
balance=10000;
3
DO payment_number=1 to 10;
4
balance=balance-1000;
5
OUTPUT;
6
END;
7
RUN;
8
PROC PRINTDATA=loan;
9
RUN;
2 Code Block
DATA STEP Data
Explanation : This example uses the same iterative DO loop to execute the DATA step in CAS and create a CAS output table. Although the DATA step executes in CAS, it runs in a single thread because no input table is specified.
Copied!
cas casauto sessopts=(caslib='casuser');
libname mylib cas;
data mylib.loan;
balance=10000;
do payment_number=1 to 10;
balance=balance-1000;
output;
end;
run;
proc print data=mylib.loan;
run;
1
cas casauto sessopts=(caslib='casuser');
2
LIBNAME mylib cas;
3
4
DATA mylib.loan;
5
balance=10000;
6
DO payment_number=1 to 10;
7
balance=balance-1000;
8
OUTPUT;
9
END;
10
RUN;
11
PROC PRINTDATA=mylib.loan;
12
RUN;
3 Code Block
DATA STEP Data
Explanation : This example shows how to nest a DO loop within another to calculate the value of a one-year investment with an annual interest rate of 7.5%, compounded monthly. Nesting is useful when you need to repeat an internal action for each pass of the outer loop.
Copied!
data earn;
Capital=2000;
do Year=1 to 10;
do Month=1 to 12;
Interest=Capital*(.075/12);
Capital+Interest;
output;
end;
end;
run;
proc print data=earn; run;
1
DATA earn;
2
Capital=2000;
3
DO Year=1 to 10;
4
DO Month=1 to 12;
5
Interest=Capital*(.075/12);
6
Capital+Interest;
7
OUTPUT;
8
END;
9
END;
10
RUN;
11
PROC PRINTDATA=earn; RUN;
4 Code Block
DATA STEP Data
Explanation : This example uses DOLIST syntax to iterate over a list of values, expressions, or sequences. The DO loop index successively takes the values specified in the list, which are separated by commas.
Copied!
data do_list;
x=-5;
do i=5, /*a single value*/
5, 4, /*multiple values*/
x + 10, /*an expression*/
80 to 90 by 5, /*a sequence*/
60 to 40 by x; /*a sequence with a variable*/
output;
end;
run;
proc print data=do_list; run;
1
DATA do_list;
2
x=-5;
3
DO i=5, /*a single value*/
4
5, 4, /*multiple values*/
5
x + 10, /*an expression*/
6
80 to 90BY5, /*a sequence*/
7
60 to 40BY x; /*a sequence with a variable*/
8
OUTPUT;
9
END;
10
RUN;
11
PROC PRINTDATA=do_list; RUN;
5 Code Block
DATA STEP Data
Explanation : This example uses the iterative DO TO-<value> syntax to repeatedly increment the values of 'x'. The OUTPUT statement is used to write each incremented value of 'x' to the output dataset. The 'TO' value specifies the final value of the index variable.
Copied!
data do_to;
x=0;
do i=0 to 10;
x=x+1;
output;
end;
run;
proc print data=do_to;
run;
1
DATA do_to;
2
x=0;
3
DO i=0 to 10;
4
x=x+1;
5
OUTPUT;
6
END;
7
RUN;
8
PROC PRINTDATA=do_to;
9
RUN;
6 Code Block
DATA STEP Data
Explanation : This example uses the iterative DO TO-<value> BY-<increment> syntax to repeatedly increment the values of 'x'. The 'BY-<increment>' value specifies that the index variable is incremented by '2' with each execution of the loop. The OUTPUT statement writes each incremented value of 'x' to the output dataset.
Copied!
data do_to_by;
x=0;
do i=0 to 10 by 2;
x=x+1;
output;
end;
run;
proc print data=do_to_by;
run;
1
DATA do_to_by;
2
x=0;
3
DO i=0 to 10BY2;
4
x=x+1;
5
OUTPUT;
6
END;
7
RUN;
8
PROC PRINTDATA=do_to_by;
9
RUN;
7 Code Block
DATA STEP Data
Explanation : This first DATA step uses a DO loop to repeatedly execute a group of statements as long as a condition is true. The program calculates the number of payments required for a specified loan amount by repeatedly decrementing the 'Balance' as long as the balance is greater than zero. The DATA step executes on the SAS compute server.
Copied!
data loan;
balance=10000;
payment=0;
do while (balance>0);
balance=balance-1000;
payment=payment+1;
output;
end;
run;
proc print data=mylib.loan;
run;
1
DATA loan;
2
balance=10000;
3
payment=0;
4
DO while (balance>0);
5
balance=balance-1000;
6
payment=payment+1;
7
OUTPUT;
8
END;
9
RUN;
10
PROC PRINTDATA=mylib.loan;
11
RUN;
8 Code Block
DATA STEP Data
Explanation : This DATA step loads the same data into a new table in a different library, 'mylib.loan', and then calculates the number of payments required for the specified loan. It does this by repeatedly iterating and decrementing the 'Balance' values until the balance is zero. The DATA step executes in CAS but in a single thread because there is no input table.
Copied!
libname mylib cas;
data mylib.loan;
balance=10000;
payment=0;
do until (balance=0);
balance=balance-1000;
payment=payment+1;
output;
end;
run;
proc print data=mylib.loan;
run;
1
LIBNAME mylib cas;
2
3
DATA mylib.loan;
4
balance=10000;
5
payment=0;
6
DO until (balance=0);
7
balance=balance-1000;
8
payment=payment+1;
9
OUTPUT;
10
END;
11
RUN;
12
PROC PRINTDATA=mylib.loan;
13
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.