---
title: "简单循环遍历表 SAS"
author: "ethen"
date: "2010-10-28T08:48:05Z"
tags: []
categories: ["数据库"]
slug: "sas"
draft: false
lastmod: "2010-10-28T08:48:44Z"
id: 796114
url: "https://ethen.iteye.com/blog/796114"
---

本程序的作用在于遍历整个表，并对表中的数据进行逐行处理。

```
%let id1=%sysfunc(open(DataTable,i));                 //打开一个表，并用id1作为游标
%let _Item_id=%sysfunc(varnum(&id1,Item_id));  //取出item_id列的位置
%let _decimal=%sysfunc(varnum(&id1,decimal));  //取出decimal列的位置	
%do %while(%sysfunc(fetch(&id1)) eq 0);             //当遍历到最后一行后，sysfunc(fetch(&id1))会返回0
    %let  Item_id=%sysfunc(getvarc(&id1,&_Item_id));  //取出第n行数据中的Item_id字段的数据，n与id1相关
    %let  decimal=%sysfunc(getvarn(&id1,&_decimal));  //取出第n行数据中的decimal字段的数据，n与id1相关
		
    %put &Item_id  &decimal;  //打印测试
			
    data tmp3_1;
    set tmp3_1;
    if item_id="&Item_id" then Refer_n=round(Refer_n,&decimal);  //用取出的Item_id和decimal数据作为参数
    run;
				 	
%end;  //结束do-while循环
%let rc=%sysfunc(close(&id1)); //关闭游标	
```

 类似上述循环在平常的SAS编程中会多次用到，特此标记。