简体   繁体   English

第29行第18列的数组下标超出范围

[英]Array subscript out of range at line 29 column 18

I was trying to use ARRAY function to do some calculation for a sample data set. 我试图使用ARRAY函数对样本数据集进行一些计算。 The following is my code: 以下是我的代码:

DATA INCHES;
SET SASHELP.FISH;
ARRAY CONVERT[5] HEIGHT WIDTH LENGTH1 LENGTH2 LENGTH3;
DO I = 1 TO 159;
CONVERT[I]=ROUND(CONVERT[I]/2.54);
DROP I;
END;
RUN;

And there was an error in the log: 日志中有一个错误:

ERROR: Array subscript out of range at line 29 column 18. 错误:第29行第18列的数组下标超出范围。

Thank you for your time and help. 感谢您的时间和帮助。

Tom above is correct. 上面的汤姆是正确的。 Arrays allows you to perform calculations using the index, but they have the length. 数组允许您使用索引执行计算,但是它们具有长度。 command 命令

ARRAY CONVERT[5] HEIGHT WIDTH LENGTH1 LENGTH2 LENGTH3; 

Means convert[1] refers to Height ...[2] 表示convert [1]表示高度... [2] to width and so forth. 到宽度等等。 As you mention only 5 variables, you can't refer to [6] as it does not exist. 由于仅提及5个变量,因此无法引用[6],因为它不存在。

Why use arrays? 为什么要使用数组? Well, you can do shorthand notations like sigma=sum (of convert[*]) 好吧,您可以使用sigma = sum之类的速记符号(属于convert [*])

DATA INCHES;
    SET SASHELP.FISH;
       ARRAY CONVERT[5] HEIGHT WIDTH LENGTH1 LENGTH2 LENGTH3;
        DO I = 1 TO 5;
            Converted[I]=ROUND(CONVERT[I]/2.54);
            DROP I;
        END;
RUN;

For more on Arrays see: https://support.sas.com/resources/papers/97529_Using_Arrays_in_SAS_Programming.pdf 有关数组的更多信息,请参见: https : //support.sas.com/resources/papers/97529_Using_Arrays_in_SAS_Programming.pdf

Just to add to the previous comments, I notice that the dataset SASHELP.FISH has 159 records, so you seem to be confusing columns with rows. 仅添加到前面的评论中,我注意到数据集SASHELP.FISH具有159条记录,因此您似乎将列与行混淆了。

A datastep will automatically loop through every record, unless explicitly told otherwise, therefore you do not need to set up such a loop. 除非另有明确说明,否则一个数据步骤将自动遍历每条记录,因此您无需设置这种循环。

The array you created stores the specified column values for the row the datastep is currently processing, therefore a loop set up to read these values cannot exceed the number of array elements (as mentioned by @Tom and @Pinegulf). 您创建的数组将为数据步骤当前正在处理的行存储指定的列值,因此设置为读取这些值的循环不能超过数组元素的数量(如@Tom和@Pinegulf所述)。

If you want to be sure of looping through all the values in the array, then you can use do i = 1 to dim(convert) 如果要确保遍历数组中的所有值,则可以使用do i = 1 to dim(convert)

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

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