Help using Base SAS procedures

Array subscript out of range

Accepted Solution Solved
Reply
Super Contributor
Posts: 1,040
Accepted Solution

Array subscript out of range


Hi team,

i have the following code and the log

could you help me where i am going wrong in the code???

Thanks

1310           data dummy;
1311          set dummy;
1312          array diag(35) dx1-dx35;
1313          do i= 1 to 35;
1314          if put(diag{i},$cancer.)=1 then do cancer=1;
1315          end;
1316          if cancer=. then cancer=0;
1317          if put(diag{i},$Diabetes.)=1 then do Diabetes=1;
1318          end;
1319          if Diabetes=. then Diabetes=0;
1320          end;
1321          if put(diag{i},$CAD.)=1 then do CAD=1;
1322          end;
1323          if CAD=. then CAD=0;
1324          if put(diag{i},$CHF.)=1 then do CHF=1;
1325          end;
1326          if CHF=. then CHF=0;
1327          drop i;
1328          run;

NOTE: Character values have been converted to numeric values at the places given by:
      (Line)Smiley SadColumn).
      1314:12   1317:12   1321:12   1324:12
ERROR: Array subscript out of range at line 1321 column 16.


Accepted Solutions
Solution
‎09-12-2013 05:18 PM
Super User
Posts: 5,090

Re: Array subscript out of range

Sorry, you're right.  It's just the extra "end" on line 1320.  That has to be moved to later in the DATA step to avoid ending the loop (starting on 1313) prematurely.

View solution in original post


All Replies
Super Contributor
Posts: 1,636

Re: Array subscript out of range

put function creates character variable.

put(diag{i},$cancer.)=1

should be

put(diag{i},$cancer.)="1"

Super Contributor
Posts: 1,040

Re: Array subscript out of range

hi Linlin,

thanks for the quick response.

Even after i changed it to have quotes the same error message appears

Thanks

Super User
Posts: 5,090

Re: Array subscript out of range

There might be a few ways to simplify your code.  But the quick answer to get rid of the error is to look at line 1319.  It's missing the word "do".  Every other time, you have said the equivalent of "do Diabetes=0".  But here the word "do" is omitted, which leads to a series of events:

(1) Line 1320 ends the DO loop that begins on line 1313.

(2) On Line 1321, i is 36, which is out of range for the array of 35 elements.

If you add "do" on line 1319, you will need to add the "end" to match with line 1313.

Better style would reorder the logic (abbreviated here):

cancer=0;

diabetes=0;

do i=1 to 35;

   if put(dx{i}, $CANCER.)="1" then cancer=1;

   if put(dx{i}, $DIABETES.)="1" then diabetes=1;

end;

Good luck.

Super Contributor
Posts: 1,040

Re: Array subscript out of range

Hi Astounding,

equivalent of    "do Diabetes=0".??????????

I checked again and i dint use do on others!!!!!!!!!!!

Could you verify one more time please?

Thanks

Solution
‎09-12-2013 05:18 PM
Super User
Posts: 5,090

Re: Array subscript out of range

Sorry, you're right.  It's just the extra "end" on line 1320.  That has to be moved to later in the DATA step to avoid ending the loop (starting on 1313) prematurely.

Super Contributor
Posts: 1,040

Re: Array subscript out of range

Hi,

Thanks so much. I removed the "END"  on line 1320 and placed an additional "end" just prior to the "drop i statement

Is that the right approach?????

thanks

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 11771 views
  • 6 likes
  • 3 in conversation