繁体   English   中英

数组下标超出范围在 SAS 中的行错误

[英]Array subscript out of range at line Error in SAS

我正在尝试使用 arrays 创建一组新的变量。 但我收到此错误“错误:第 581 行第 23 列的数组下标超出范围。”

在我的程序中,我有一组宏变量 n1 到 n15 这是我的代码,我无法找出我的 arrays 是如何超出范围的,因为所有 arrays 都有 15 个元素

data allsae1; 
*length _a1 _a2 _a3 _a4 _a5 _a6 _a7 _a8 _a9 _a10 _a11 _a12 _a13 _a14 _a99 _b1 _b2 _b3 _b4 _b5 _b6 _b7 _b8 _b9 _b10 _b11 _b12 _b13 _b14 _b99 $10;
set  allsae; 

array _anum{15} a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a99;
array _bnum{15} b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14 b99;
array astat{15} _a1 _a2 _a3 _a4 _a5 _a6 _a7 _a8 _a9 _a10 _a11 _a12 _a13 _a14 _a99;
array bstat{15} _b1 _b2 _b3 _b4 _b5 _b6 _b7 _b8 _b9 _b10 _b11 _b12 _b13 _b14 _b99;

%macro stats;

%do i=1 %to 15;
    %if _anum[i] !=. %then %do;
    astat[i]=strip(put(_anum[i], best.))||" ("||strip(put(_anum[i]/(&&n&i) *100, 8.1))||"%)";
    %end;

    bstat[i] = strip(put(_bnum[i], best.));
%end;
%mend stats;
%stats;
run;

为什么这里有宏代码? 我看不到任何需要生成 SAS 代码的地方。 唯一的地方是对&&n&i的引用,但我看不到您在哪里定义了任何名为 N1、N2 等的宏变量。

字符串_anum[i]总是不等于字符串. 所以你总是生成 SAS 语句

astat[i]=strip(put(_anum[i], best.))||" ("||strip(put(_anum[i]/(<something>) *100, 8.1))||"%)";

但是您从未创建变量I ,因此astat_anum arrays 的索引将无效。

很可能您只需要一个普通的 DO 循环,根本不需要定义宏。 如果你真的有这 15 个宏变量并且它们包含数字字符串,你可能只使用 SYMGETN() function。

do i=1 to 15;
  if not missing(_anum[i]) then do;
    astat[i]=strip(put(_anum[i], best.))||" ("||strip(put(_anum[i]/(symgetn(cats('n',i))) *100, 8.1))||"%)";
  end;
  bstat[i] = strip(put(_bnum[i], best.));
end;

或者只是制作一个临时数组来拥有这 15 个值。

array _n[15] _temporary_ (&n1 &n2 &n3 &n4 .... &n15);

然后您使用I变量对其进行索引。

... _n[i] ...

您在这里需要 arrays 而不是宏。 您正在尝试使用宏变量,但我建议您将宏变量 N 分配给数组。 我还建议创建单个宏变量而不是 N,这样您就不必处理索引和宏循环。

使用这样的东西创建你的 N:

proc sql noprint;
select n into N_list_values separated by ", " from yourTable;
quit;

%put &n_list_values;

然后你可以在以后像这样在数组中使用它。

array _anum{15} a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a99;
array _bnum{15} b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14 b99;
array astat{15} _a1 _a2 _a3 _a4 _a5 _a6 _a7 _a8 _a9 _a10 _a11 _a12 _a13 _a14 _a99;
array bstat{15} _b1 _b2 _b3 _b4 _b5 _b6 _b7 _b8 _b9 _b10 _b11 _b12 _b13 _b14 _b99;

array _n(15) _temporary_ (&N_list_values);



do i=1 to 15;
    if _anum[i] !=. then do;
    astat[i]=strip(put(_anum[i], best.))||" ("||strip(put(_anum[i]/(_n(i)) *100, 8.1))||"%)";
    end;


bstat[i] = strip(put(_bnum[i], best.));
end;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM