Hi,
I got the error shown in the title: ERROR: Array subscript out of range at line xxx column xx. There were many cases I found online asking for the similar question, but none of them came from my case.
Here is my code:
ARRAY codes(*) MODIFIER1
MODIFIER2
MODIFIER3
MODIFIER4
;
DO i = 1 TO DIM(codes);
IF MISSING(vsCptMod)
& ^htCptMod.find(KEY: codes[i])
THEN vsCptMod = i;
IF MISSING(vsTelMod)
& ^htTelMod.find(KEY: codes[i])
THEN vsTelMod = i;
END;
I couldn't find the code issue causing the error. Is there any solution available from the community?
Thanks a lot !
There are two find functions in your code.
in case the first find is the last member - then the next find starts from dim(codes).
I suggest to replace the coed with:
ARRAY codes(*) MODIFIER1 - MODIFIER4 ;
DO i = 1 TO DIM(codes);
IF MISSING(vsCptMod)
& ^htCptMod.find(KEY: codes[i])
THEN vsCptMod = i;
END;
DO i = 1 TO DIM(codes);
IF MISSING(vsTelMod)
& ^htTelMod.find(KEY: codes[i])
THEN vsTelMod = i;
END;
Since you didn't post the log we can't tell which line it is talking about. Or if there are multiple places that are indexing into an array on that line which one is causing the error.
@leehsin wrote:
Hi,
I got the error shown in the title: ERROR: Array subscript out of range at line xxx column xx.
The log will show you which line this is, and exactly where in the line (the column number) the error happens.
@leehsin :
The only logical reason it can happen given your code excerpt:
-- the loop index I is modified in the first IF statement inside the loop to a value outside the array bounds
-- when the second IF is executed, the codes[i] reference given to the KEY argument tag in FIND gets the runaway index value
Reasoning still further, the only instruction in the first IF that can modify index I is the FIND method call. And it can modify it only if:
-- hash variable I is defined to the data portion of the hash table
-- the FIND call is successful
To prevent this from happening in any case - even if I is in the data portion (I don't know why it should be but you must now better) - refrain from calling the FIND method where it serves no function except finding if the key-value is in the table. You only need to check if codes[i] is in the table, not retrieve anything from it. Yet if I is in the data portion and FIND is successful, the hash value of I will overwrite the value of its PDV host variable I - that is, your array index. Thus, call CHECK instead of FIND. This method, unlike FIND, is designed to only check (as its name implies) whether the key-value is in the table and does not overwrite any PDV host variables regardless of whether the CHECK call is successful (i.e. the key-value is in the table) or not.
Incidentally, calling FIND instead of CHECK is a typical mistake made by hash newbies. Always call CHECK (and never FIND) if all you want to do is to find if a key-value is in the table. In addition to being harmless to the PDV host variables, CHECK also executes faster than FIND since it causes no data movement from the hash table to the PDV.
Kind regards
Paul D.
Thanks for the nice words.
Bear in mind that in order to use the hash object conscientiously, one must understand that it's merely a table in memory endowed with a number of standard CRUD operations that can be performed against it using hash methods and operators. Thus, certain methods represent certain table operations, for example:
The list, of course, is not exhaustive. You can find a concise introduction into this kind of concept at:
https://support.sas.com/resources/papers/proceedings17/0821-2017.pdf
And this is the concept upon which the hash book @DonH and I have written is structured, which is why its title contains the phrase "Hash Table Operations". Here's a shameless plug:
Kind regards
Paul D.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.