Can someone advise why im getting the array subscript out of range error
749 data Ischemic_stroke;
750 set CIHI.DAD1819Q2;
751 length STROKE_GROUP $ 40;
752
753 STROKE_ISC=0;
754 STROKE_CNT=0;
755 /* EXCLUDE UNDER 18 */
756 IF AGE<18 THEN STROKE_ISC=0;
757 /* EXCLUDE NON-ONTARIO FUNDED PATIENT */
758 IF PROVINCE_ISSUING_HCN ~='ON' OR RESPONSIBILITY_FOR_PAYMENT~='01' THEN STROKE_ISC=0;
759 /* EXCLUDE SURGICAL CASES */
760 IF MCC_PARTITION='I' THEN STROKE_ISC=0;
761
762 /* ARRAY - DIAGNOSIS CODE */
763
764 ARRAY DX_CODE [25] DIAG_CODE_01 - DIAG_CODE_25;
765 /* ARRAY - DIAGNOSIS TYPE */
766 ARRAY DX_TYPE [25] DIAG_TYPE_01 - DIAG_TYPE_25;
767
768
769 /* SEARCH FOR STROKE DIAGNOSIS CODES AND EXCLUDE TYPE 3 */
770
771 DO H=1 TO 25;
772
773 IF ((SUBSTR(DX_CODE[H],1,3) IN ('G45' 'I61' 'I63' 'I64') OR SUBSTR(DX_CODE[H],1,4) = 'H341'))and
773! SUBSTR(DX_CODE[H],1,4) NOT IN ('G454' 'I636') and DX_TYPE(H) NE '3' THEN DO
774 STROKE_ISC=1;
775
776 END;
777 END;
778
779 /* LIST OF 3-DIGIT STROKE DIAGNOSIS CODES - ISCHEMIC */
780 IF STROKE_ISC=1 and SUBSTR(DX_CODE[H],1,3) IN ('I63' 'I64' 'H34') THEN DO;
781 STROKE_CNT+1;
782 STROKE_GROUP = 'Stroke - Ischemic';
783 END;
784 DROP H ;
785
786 RUN;
ERROR: Array subscript out of range at line 780 column 28.
When your top DO loop is finished, the final value of H is 26. There's nothing in your logic to capture the value of H when a match is found, so the loop continues all the way to the end.
Your bottom DO loop then attempts to find the 26th element of the array, and there are only 25 values.
Also note, you can get rid of the SUBSTR function by adding the colon to your comparisons. For example, these two are identical:
if ((substr(DX_CODE[H], 1, 3) in ('G45' 'I61' 'I63' 'I64')
or substr(DX_CODE[H], 1, 4) = 'H341')) and ....
if (DX_CODE[H] in : ('G45' 'I61' 'I63' 'I64' 'H341')) and ....
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.