📄 m34 - generating data with do loops - 32 of 34.mht
字号:
a series</A>; the DO loop will execute once for each item, with =
the index=20
variable equal to the value of each item. A series can consist of =
all=20
numeric values, all character values (enclosed in quotation =
marks), or all=20
variable names (without quotation marks).=20
<P><STRONG>Nesting DO Loops</STRONG><BR>DO loops can run <A=20
onmouseover=3D"window.status=3D'Display details on nesting DO =
loops.';"=20
onclick=3DsuggestBack() onmouseout=3D"window.status=3D'';"=20
href=3D"http://www.sas.com/apps/OLTRN/59080/m34/m34_13.htm">within =
DO=20
loops</A>, as long as you assign a unique index variable to each =
loop and=20
terminate each DO loop with its own END statement.=20
<P><STRONG>Iteratively Processing Data That Is Read from a Data=20
Set</STRONG><BR>You can use a DO loop to <A=20
onmouseover=3D"window.status=3D'Display details on reading data =
with DO loops.';"=20
onclick=3DsuggestBack() onmouseout=3D"window.status=3D'';"=20
href=3D"http://www.sas.com/apps/OLTRN/59080/m34/m34_16.htm">read a =
data=20
set</A> and compute the value of a new variable. DO loop start and =
stop=20
values, for example, can be read from a data set.=20
<P><STRONG>Conditionally Executing DO Loops</STRONG><BR>The <A=20
onmouseover=3D"window.status=3D'Display details on the DO UNTIL =
statement.'; "=20
onclick=3DsuggestBack() onmouseout=3D"window.status=3D''"=20
href=3D"http://www.sas.com/apps/OLTRN/59080/m34/m34_19.htm">DO =
UNTIL</A>=20
statement executes a DO loop until a condition is true. Because =
the=20
expression is not evaluated until the bottom of the loop, a DO =
UNTIL loop=20
will execute at least once. The <A=20
onmouseover=3D"window.status=3D'Display details on the DO WHILE =
statement.';"=20
onclick=3DsuggestBack() onmouseout=3D"window.status=3D''"=20
href=3D"http://www.sas.com/apps/OLTRN/59080/m34/m34_23.htm">DO =
WHILE</A>=20
statement is used to execute a DO loop while a condition is true. =
Because=20
the DO WHILE statement is evaluated at the top of the DO loop, if =
the=20
expression is false the first time it is evaluated, the DO loop =
never=20
executes.=20
<P><STRONG>Using Conditional Clauses with the Iterative DO=20
Statement</STRONG><BR>DO WHILE and DO UNTIL statements can be used =
within=20
iterative DO loops to combine <A=20
onmouseover=3D"window.status=3D'Display details on conditional DO =
loop clauses.'; "=20
onclick=3DsuggestBack() onmouseout=3D"window.status=3D''"=20
=
href=3D"http://www.sas.com/apps/OLTRN/59080/m34/m34_27.htm">conditional =
and=20
unconditional</A> execution.=20
<P><STRONG>Creating Samples</STRONG><BR>DO loops provide an easy =
way to <A=20
onmouseover=3D"window.status=3D'Display details on creating =
samples.';"=20
onclick=3DsuggestBack() onmouseout=3D"window.status=3D''"=20
href=3D"http://www.sas.com/apps/OLTRN/59080/m34/m34_30.htm">create =
samples</A> from other data sets. Enclose the SET statement in a =
DO loop,=20
using the start, stop, and increment values to select the =
observations.=20
Add the POINT=3D option to the SET statement, setting it equal to =
the index=20
variable of the DO loop. Then add a STOP statement to prevent DATA =
step=20
looping, and add an OUTPUT statement to write DATA step output.=20
</P></TD></TR></TBODY></TABLE><!-- InstanceEndEditable --><BR>
<TABLE cellSpacing=3D0 cellPadding=3D0 width=3D"85%" align=3Dcenter =
border=3D0>
<TBODY>
<TR>
<TD align=3Dleft>
<HR>
</TD></TR></TBODY></TABLE>
<P><!-- InstanceBeginEditable name=3D"syntax" -->
<CENTER>
<TABLE cellSpacing=3D0 cellPadding=3D0 width=3D"85%" border=3D0>
<TBODY>
<TR>
<TD><STRONG><A name=3DSyntax>Syntax</A></STRONG>=20
<P><EM>To go to the page where a statement or option was =
presented, select=20
a link.</EM> </P></TD></TR></TBODY></TABLE><BR></CENTER>
<P align=3Dcenter>
<TABLE cellPadding=3D2 align=3Dcenter nowrap>
<TBODY>
<TR>
<TD>
<DL>
<DT><FONT color=3D#003399><A=20
onmouseover=3D"window.status=3D'Display details on DO loop =
syntax.'; "=20
onclick=3DsuggestBack() onmouseout=3D"window.status=3D''"=20
=
href=3D"http://www.sas.com/apps/OLTRN/59080/m34/m34_3.htm"><STRONG>DO</ST=
RONG>=20
<EM>index-variable</EM>=3D<EM>start</EM> <STRONG>TO</STRONG> =
<EM>stop</EM>=20
<STRONG>BY</STRONG> =
<EM>increment</EM><STRONG>;</STRONG></A></FONT>=20
<DD><FONT color=3D#003399><EM>SAS statements</EM></FONT>=20
<DT><FONT color=3D#003399><A=20
onmouseover=3D"window.status=3D'Display details on DO loop =
syntax.'; "=20
onclick=3DsuggestBack() onmouseout=3D"window.status=3D''"=20
=
href=3D"http://www.sas.com/apps/OLTRN/59080/m34/m34_3.htm"><STRONG>END;</=
STRONG></A></FONT>=20
<BR><BR>
<DT><FONT color=3D#003399><A=20
onmouseover=3D"window.status=3D'Display details on the DO UNTIL =
statement.'; "=20
onclick=3DsuggestBack() onmouseout=3D"window.status=3D''"=20
=
href=3D"http://www.sas.com/apps/OLTRN/59080/m34/m34_19.htm"><STRONG>DO=20
UNTIL(</STRONG><EM>expression</EM><STRONG>);</STRONG></A></FONT> =
<BR><BR>
<DT><FONT color=3D#003399><A=20
onmouseover=3D"window.status=3D'Display details on the DO WHILE =
statement.'; "=20
onclick=3DsuggestBack() onmouseout=3D"window.status=3D''"=20
=
href=3D"http://www.sas.com/apps/OLTRN/59080/m34/m34_23.htm"><STRONG>DO=20
WHILE(</STRONG><EM>expression</EM><STRONG>);</STRONG></A></FONT> =
<BR><BR>
<DT><FONT color=3D#003399><A=20
onmouseover=3D"window.status=3D'Display details on conditional =
DO loop clauses.'; "=20
onclick=3DsuggestBack() onmouseout=3D"window.status=3D''"=20
=
href=3D"http://www.sas.com/apps/OLTRN/59080/m34/m34_27.htm"><STRONG>DO</S=
TRONG>=20
<EM>index-variable</EM>=3D<EM>start</EM> <STRONG>TO</STRONG> =
<EM>stop</EM>=20
<STRONG>BY</STRONG> <EM>increment</EM>=20
=
<STRONG>UNTIL(</STRONG><EM>expression</EM><STRONG>);</STRONG></A></FONT> =
<BR><BR>
<DT><NOBR><FONT color=3D#003399><A=20
onmouseover=3D"window.status=3D'Display details on conditional =
DO loop clauses.'; "=20
onclick=3DsuggestBack() onmouseout=3D"window.status=3D''"=20
=
href=3D"http://www.sas.com/apps/OLTRN/59080/m34/m34_27.htm"><STRONG>DO</S=
TRONG>=20
<EM>index-variable</EM>=3D<EM>start</EM> <STRONG>TO</STRONG> =
<EM>stop</EM>=20
<STRONG>BY</STRONG> <EM>increment</EM>=20
=
<STRONG>WHILE(</STRONG><EM>expression</EM><STRONG>);</STRONG></A></FONT><=
/NOBR>=20
</DT></DL></TD></TR></TBODY></TABLE>
<CENTER>
<TABLE cellSpacing=3D0 cellPadding=3D0 width=3D"85%" border=3D0>
<TBODY>
<TR>
<TD align=3Dleft>
<HR>
</TD></TR></TBODY></TABLE><BR></CENTER><!-- InstanceEndEditable -->
<P><!-- InstanceBeginEditable name=3D"sampleprogram" -->
<TABLE cellSpacing=3D0 cellPadding=3D0 width=3D"85%" border=3D0>
<TBODY>
<TR>
<TD align=3Dleft><STRONG><A name=3DSample>Sample =
Programs</A></STRONG>=20
<BLOCKQUOTE><STRONG>Simple DO Loop =
</STRONG></BLOCKQUOTE><PRE><FONT color=3D#003399> data =
work.earn;</FONT>
<FONT color=3D#003399> Value=3D2000;</FONT>
<FONT color=3D#003399> do year=3D1 to 20;</FONT>
<FONT color=3D#003399> Interest=3Dvalue*.075;</FONT>
<FONT color=3D#003399> value+interest;</FONT>
<FONT color=3D#003399> end;</FONT>
<FONT color=3D#003399> run;</FONT></PRE>
<BLOCKQUOTE>
<P><STRONG>Nested DO Loops </STRONG></P></BLOCKQUOTE><PRE><FONT =
color=3D#003399> data work.earn;</FONT>
<FONT color=3D#003399> do year=3D1 to 20;</FONT>
<FONT color=3D#003399> Capital+2000;</FONT>
<FONT color=3D#003399> do month=3D1 to 12;</FONT>
<FONT color=3D#003399> Interest=3Dcapital*(.075/12);</FONT>
<FONT color=3D#003399> capital+interest;</FONT>
<FONT color=3D#003399> end;</FONT>
<FONT color=3D#003399> end;</FONT>
<FONT color=3D#003399> run;</FONT></PRE>
<BLOCKQUOTE>
<P><STRONG>Conditional Clause</STRONG> =
</P></BLOCKQUOTE><PRE><FONT color=3D#003399> data =
work.invest(drop=3Di);</FONT>
<FONT color=3D#003399> do i=3D1 to 10 =
until(Capital>=3D50000);</FONT>
<FONT color=3D#003399> Year+1;</FONT>
<FONT color=3D#003399> capital+2000;</FONT>
<FONT color=3D#003399> capital+capital*.10;</FONT>
<FONT color=3D#003399> end;</FONT>
<FONT color=3D#003399> run;</FONT></PRE>
<BLOCKQUOTE>
<P><STRONG>Creating Samples</STRONG> </P></BLOCKQUOTE><PRE> =
<FONT color=3D#003399>data work.subset;</FONT>
<FONT color=3D#003399>do sample=3D10 to 5000 by 10;</FONT>
<FONT color=3D#003399>set factory.widgets =
point=3Dsample;</FONT>
<FONT color=3D#003399>output;</FONT>
<FONT color=3D#003399>end;</FONT>
<FONT color=3D#003399>stop;</FONT>
<FONT color=3D#003399>run;</FONT></PRE>
<HR>
</TD></TR></TBODY></TABLE><BR><!-- InstanceEndEditable -->
<P><!-- InstanceBeginEditable name=3D"pointstoremember" -->
<TABLE cellSpacing=3D0 cellPadding=3D0 width=3D"85%" border=3D0>
<TBODY>
<TR>
<TD align=3Dleft><STRONG><A name=3DPoints>Points to =
Remember</A></STRONG> <BR>
<UL>
<LI>If you do not specify a BY clause, the increment value for =
DO loops=20
is 1.<BR><BR>
<LI>In most cases, the index variable is needed only for =
processing the=20
DO loop and can be dropped from the data set.<BR><BR>
<LI>The index variable is always incremented by one value beyond =
the=20
stop value unless you terminate the DO loop in some other=20
manner.<BR><BR>
<LI>It's easier to manage nested DO loops if you indent the =
statements=20
in each loop.=20
</LI></UL></TD></TR></TBODY></TABLE><!-- InstanceEndEditable =
--></CENTER><!-- #BeginLibraryItem "/Library/nav_regular_bottom.lbi" -->
<TABLE cellSpacing=3D0 cellPadding=3D0 width=3D"100%">
<TBODY>
<TR>
<TD width=3D14><IMG height=3D7 alt=3D""=20
=
src=3D"http://www.sas.com/apps/elearning/images/en/navimages/linebox.gif"=
=20
width=3D14></TD>
<TD colSpan=3D2><IMG height=3D7 alt=3D""=20
=
src=3D"http://www.sas.com/apps/elearning/images/en/navimages/linegray.gif=
"=20
width=3D"100%"></TD></TR>
<TR>
<TD> </TD>
<TD vAlign=3Dtop><FONT face=3D"Verdana, Arial, Helvetica, Sans =
Serif"=20
color=3Dgray size=3D1><A class=3Dcopyrightlink=20
href=3D"javascript:%20openOther('../copyrite.htm')"><FONT=20
face=3D"Arial, Helvetica, sans-serif">Copyright</FONT></A> <FONT=20
face=3D"Arial, Helvetica, sans-serif">=A9 2003 SAS Institute Inc., =
Cary, NC,=20
USA. All rights reserved.</FONT></FONT> <FONT=20
face=3D"Arial, Helvetica, sans-serif"><BR><FONT color=3Dgray =
size=3D1><A=20
class=3Dcopyrightlink href=3D"http://www.sas.com/Copyright.html"=20
target=3D_blank>Terms of Use & Legal Information</A> | <A=20
class=3Dcopyrightlink href=3D"http://www.sas.com/Privacy.html"=20
target=3D_blank>Privacy Statement</A></FONT> </FONT></TD>
<TD width=3D60>
<DIV align=3Dright><A=20
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -