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 'balance' variable values and write these values to the output dataset. The DATA step runs 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 an output CAS table.
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 one DO loop within another to calculate the value of a one-year investment earning 7.5% annual interest, compounded monthly.
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, including single, multiple, expressions, and sequences with or without variables.
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 iterative DO TO syntax to repeatedly increment the values of 'x'. The loop also uses the OUTPUT statement to write each incremented 'x' value to the output dataset.
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 iterative DO TO-value BY-increment syntax to repeatedly increment the values of 'x' by '1'. The BY-increment value specifies that the index variable increments by '2' with each loop execution. The loop also uses the OUTPUT statement to write each incremented 'x' value 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 : The first DATA step in this example uses a DO WHILE loop to repetitively 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 iterating to decrement 'Balance' as long as the balance is greater than zero. The DATA step runs 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 table in a different library, 'mylib.loan', and then calculates the number of payments that must be made for the specified loan. It does this by repeatedly iterating and decrementing 'Balance' until the balance is zero.
Copied!
libname mylib;
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;
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.