公卫百科  > 所属分类  >  SAS   
[0] 评论[0] 编辑

SAS Do Loops

Do-loops are used to repeat a sequence of operations. Here is an example.

Each row of data in the file iris.dat contains 12 measurements: measurements of four characteristics for each of the 3 species of iris. The 4 measurements are to be read into the 4 variables m1-m4 and an additional variable species will contain a number designating the type of iris from which the measurements were taken, say, 1 for iris setosa, 2 for versicolor, 3 for virginica. 公卫论坛


data iris;
infile `iris.dat';
do species=1 to 3;
input m1-m4 @;
output;
end;
run;


The do loop reads in turn the 4 measurements for each type in a given row of the data set and defines the variable species appropriately. 公卫人

Note that the do loop MUST BE paired with an end statement to mark the end of the loop.

The output statement is used to write the variables to the data set being created. If the output statement above were omitted, only the last group of measurments would appear in the SAS data set. 公卫人

The counter for a do-loop can take values specified by a comma delimited list. Examples:


do sex='m','f';
...
end;
do parity=1,3,5;
...
end;


A counterless do-loop can be used to have more complicated processing after an if-branch. Example:
公卫百科



data old;
input age sex income;
if age<30 then do;
if income<20000 then cat=1;
if income>20000 and income <30000 then cat=2;
end;
if age>= 30 then do;
....


Do-loops and if-then statements can be nested.

公卫人



The continue statement can be used to branch to the bottom of the inner most enclosing do loop.

The return statement can be used to branch to the bottom of the data step program.

The stop command can be used to end datastep processing. 公卫家园

附件列表


0

词条内容仅供参考,如果您需要解决具体问题
(尤其在法律、医学等领域),建议您咨询相关领域专业人士。

如果您认为本词条还有待完善,请 编辑

上一篇 SAS Drop Statement    下一篇 SAS Display Manager Statement

标签

暂无标签

同义词

暂无同义词