SAS循环期间的回声

时间:2015-08-10 06:23:50

标签: sas

我是SAS新手,正在开发一个非常简单的for循环。为了更好地理解我的代码,我想为循环中的每次迭代输出计数器变量iproductlocation。如何调整代码才能执行此操作?每次我使用putecho时,我都会遇到语法错误。

data Error_log;
set Data_Set;

    do i=1 to nobs until (found);
        set Excel_Table point = i nobs = nobs;
        if (product = 01) and (Location = 'CA') then do;
            output Error_log;
            found = 1;
        end;
    end;

2 个答案:

答案 0 :(得分:3)

你已经从别人那里得到了很好的建议。我认为您对SAS DATA步骤循环的探索是一个好主意。肯定有两个set语句很有用的情况,并且使用DO循环显式迭代数据集很有用。谷歌关于“DoW循环”的评论文章。 PUT语句对跟踪循环很有用。请说明您是如何尝试使用它并收到错误的。

以下是与您类似的DATA步骤,并添加了PUT语句。请注意,我不是要评估代码的预期逻辑或代码。这只是玩循环的一个例子。 _n_是隐式DATA步循环的迭代次数的计数器。

data Error_Log;
  set sashelp.class;
  do i=1 to nobs until(found);
    set sashelp.shoes point=i nobs=nobs;
    put (_n_ i name product subsidiary)(=);
    if product="Boot" and subsidiary="Cairo" then do;     
      output Error_log;
      found=1;
    end;
  end;
run;

答案 1 :(得分:0)

两个快速点:正如提到的那样,它不清楚为什么你有这两个陈述。此外,您的found=1不会在output Error_log;之后被捕获。

关于如何更好地了解代码中发生的事情的问题,您可以使用数据集上的/debug选项:

data Error_log / debug ;
set Data_Set;

    do i=1 to nobs until (found);
        set Excel_Table point = i nobs = nobs;
        if (product = 01) and (Location = 'CA') then do;
            output Error_log;
            found = 1;
        end;
    end;
run; 

您可以阅读更多相关信息here

否则,如果不需要data_set,您只需返回找到的''来自Excel_Table的记录:

data error_log ;
  set Excel_Table(where=(product=01 and location='CA'));
  found=1 ;
run ;