简体   繁体   English

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

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

I am trying to create new set of variables using arrays.我正在尝试使用 arrays 创建一组新的变量。 but i am getting this error " ERROR: Array subscript out of range at line 581 column 23."但我收到此错误“错误:第 581 行第 23 列的数组下标超出范围。”

in my program i have set of macro variables n1 to n15 Here is my code i can;t find out how does my arrays goes out of range since all arrays have 15 elements在我的程序中,我有一组宏变量 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;

Why do you have macro code here?为什么这里有宏代码? I don't see any place where you need to generate SAS code.我看不到任何需要生成 SAS 代码的地方。 The only place is the reference to &&n&i but I don't see where you have defined any macro variables named N1, N2, etc.唯一的地方是对&&n&i的引用,但我看不到您在哪里定义了任何名为 N1、N2 等的宏变量。

The string _anum[i] is always not equal to the string .字符串_anum[i]总是不等于字符串. so you always generate the SAS statement所以你总是生成 SAS 语句

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

But you never created the variable I so the index into astat and _anum arrays will be invalid.但是您从未创建变量I ,因此astat_anum arrays 的索引将无效。

Most likely you just want a normal DO loop and don't need to define a macro at all.很可能您只需要一个普通的 DO 循环,根本不需要定义宏。 If you really have those 15 macro variables and they contain numeric strings you might just use the SYMGETN() function.如果你真的有这 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;

Or just make a temporary array to have those 15 values.或者只是制作一个临时数组来拥有这 15 个值。

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

which you then index into with the I variable.然后您使用I变量对其进行索引。

... _n[i] ...

You need arrays not macros here.您在这里需要 arrays 而不是宏。 You're trying to use your macro variables but I would instead suggest you assign your macro variables N to an array.您正在尝试使用宏变量,但我建议您将宏变量 N 分配给数组。 I would also recommend creating a single macro variable not N so you don't have to work about indexes and macro loops.我还建议创建单个宏变量而不是 N,这样您就不必处理索引和宏循环。

Create your N using something like this:使用这样的东西创建你的 N:

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

%put &n_list_values;

Then you can use it later on like this in the array.然后你可以在以后像这样在数组中使用它。

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