SAS循环期间的回波

SAS循环期间的回波,sas,Sas,我是SAS的新手,正在开发一个非常简单的for循环。为了更好地理解我的代码,我想为循环中的每个迭代输出计数器变量I、product和location。我如何调整我的代码来实现这一点?每次使用put或echo时,都会出现语法错误 data Error_log; set Data_Set; do i=1 to nobs until (found); set Excel_Table point = i nobs = nobs; if (product = 01

我是SAS的新手,正在开发一个非常简单的for循环。为了更好地理解我的代码,我想为循环中的每个迭代输出计数器变量
I
product
location
。我如何调整我的代码来实现这一点?每次使用
put
echo
时,都会出现语法错误

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;

两个要点:正如stat提到的,不清楚为什么会有这两个set语句。此外,您的
found=1
不会被捕获,因为它位于
输出错误日志之后

关于如何更好地了解代码中发生了什么的问题,您可以在数据集上使用
/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; 
你可以阅读更多关于它的内容

否则,如果不需要
数据集
,您可以从
Excel\u表
返回“找到的”记录:

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

你已经从别人那里得到了很好的建议。我认为您探索SAS数据步循环是一个好主意。在某些情况下,拥有两个set语句是非常有用的,而使用DO循环显式迭代数据集是非常有用的。谷歌在“DoW loop”上发表论文供评论。PUT语句对于跟踪循环很有用。请说明你是如何尝试使用它的,并得到一个错误

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

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;

可能您的PUT语句在定义变量之前引用了该变量,并导致SAS以与在其中一个输入数据集中定义变量的方式不匹配的方式定义该变量。这里有一个例子

data _null_;
   put name= ;
   set sashelp.class ;
run;

ERROR: Variable name has been defined as both character and numeric.
防止这种情况的一种方法是添加一行,其中包含一个永远无法执行的SET语句。这样SAS将检查引用的数据集并适当地定义变量

data Error_log;
  if 1=0 then set Data_Set Excel_Table ;
  length i found 8;
  put 'BEFORE first SET statement: ' (_n_ nobs i found product location) (=);
  set Data_Set;
  put 'AFTER first SET statement: ' (_n_ nobs i found product location) (=);
  do i=1 to nobs until (found);
    put 'BEFORE second SET statement: ' (_n_ nobs i found product location) (=);
    set Excel_Table point = i nobs = nobs;
    put 'AFTER second SET statement: ' (_n_ nobs i found product location) (=);
    if (product = 01) and (Location = 'CA') then do;
      output Error_log;
      found = 1;
    end;
    put 'AFTER IF statement: ' (_n_ nobs i found product location) (=);
  end;
  put 'END of DATA step: ' (_n_ nobs i found product location) (=);
run;

语法错误是什么?另外,请相信选项应该在括号中(point=nobs=)。还认为point=选项还需要一个stop语句,而您似乎没有这样的语句。我还建议您将变量名nobs改为nobs=My_nobs。为什么要使用双集语句?您能否为我们提供数据集和excel表格的样本数据集,并详细说明您希望您的程序执行的操作?如果数据集是无用的,就不要包含它。此外,循环无法启动,它引用的变量在第二步语句之前没有值,其中nobs=nobs不是有效选项。另外,为什么要循环到整个数据集?这正是没有任何循环的datastep所做的…@Jonas point=nobs=options不应该在括号中。它们是SET语句的选项,而不是dataset选项。虽然STOP语句通常与point选项一起使用,但它不是必需的。如前所述,当第一个SET语句读取DATA_SET中的文件结尾标记时,数据步骤将自动停止。@stat使用两个类似的SET语句是从第二个表读取特定行的常用方法,同时仍在第一个表上迭代。SAS新手通常不使用它,但它仍然是一种非常有效的方法。请注意,触发DATA step调试器的/debug选项在Enterprise Guide或其他没有交互式会话的环境中不可用。如果您使用的是Display Manager SAS,这是一个很好的建议。