Hello. I have question about building an array.
The arrays are included in the code below. The error follows. I am not sure what the mistake is. I know the repeated use of &ADMISSION is poor coding practice for purposes of de-bugging. Please advise. Thank you.
24 data &ADMISSION; 25 set &ADMISSION; 26 array DIAG(28) DIAG26 DIAG27 DIAG28 DIAG29 DIAG30 DIAG31 DIAG32 DIAG33 27 DIAG34 DIAG35 DIAG36 DIAG37 DIAG38 DIAG39 DIAG40 DIAG41 28 DIAG42 DIAG43 DIAG44 DIAG45 DIAG46 DIAG47 DIAG48 DIAG49 29 DIAG50 DIAG51 DIAG52 DIAG53; 30 array EVRSCD(12) EVRSCD01 EVRSCD02 EVRSCD03 EVRSCD04 EVRSCD05 EVRSCD06 EVRSCD07 31 EVRSCD08 EVRSCD09 EVRSCD10 EVRSCD11 EVRSCD12; 32 array PVSNCD_KF(25) PVSNCD01 PVSNCD02 PVSNCD03 PVSNCD04 PVSNCD05 PVSNCD06 PVSNCD07 33 PVSNCD08 PVSNCD09 PVSNCD10 PVSNCD11 PVSNCD12 PVSNCD13 34 PVSNCD14 PVSNCD15 PVSNCD16 PVSNCD17 PVSNCD18 PVSNCD19 35 PVSNCD20 PVSNCD21 PVSNCD22 PVSNCD23 PVSNCD24 PVSNCD25; 36 do i = 26 to 53; 37 DIAG(i) = .; 38 end; 39 do j = 01 to 12; 40 EVRSCD(j) = .; 41 end; 42 do k = 01 to 25; 43 PVSNCD_KF(k) = .; 44 end; 45 run;
ERROR: Array subscript out of range at line 37 column 4.
You are referencing array elements that do not exist. Define the array as follows instead.
array DIAG(26:53)
As of now, your array elements are numerated from 1 to 28. So when you try to reference the 29'th element of the array, SAS produces an error, because it does not exist.
You are referencing array elements that do not exist. Define the array as follows instead.
array DIAG(26:53)
As of now, your array elements are numerated from 1 to 28. So when you try to reference the 29'th element of the array, SAS produces an error, because it does not exist.
When correcting and re-running the revised code, I get the following error repeated multiple times:
ERROR: All variables in array list must be the same type, i.e., all numeric or character.
I think this is because the values of DIAG01 - DIAG 25 are alphanumeric. I changed the code in the OP to spaces:
do i = 26 to 53; DIAG(i) = ' '; end; do j = 01 to 12; EVRSCD(j) = ' '; end; do k = 01 to 25; PVSNCD_KF(k) = ' '; end; do l = 01 to 12; EDGSCD(l) = ' '; end;
However, I still receive the same errors. Kindly advise.
The variables in an array must be either all numeric or all character. Run
proc contents data=&ADMISSION;
run;
to see the types of the relevant variables.
I checked. The variables created in the array are numeric of length 8. I have modified the code previously to create the character variables I want using a put statement. Is this correct practice to achieve all character variables?
data RAW.READMISSIONS_INDEX_PN_1216_KYLE; set &ADMISSION; array DIAG_C(26:53) DIAG_C26 DIAG_C27 DIAG_C28 DIAG_C29 DIAG_C30 DIAG_C31 DIAG_C32 DIAG_C33 DIAG_C34 DIAG_C35 DIAG_C36 DIAG_C37 DIAG_C38 DIAG_C39 DIAG_C40 DIAG_C41 DIAG_C42 DIAG_C43 DIAG_C44 DIAG_C45 DIAG_C46 DIAG_C47 DIAG_C48 DIAG_C49 DIAG_C50 DIAG_C51 DIAG_C52 DIAG_C53; do i = 26 to 53; DIAG_C(i) = ' '; DIAG(i) = put(DIAG_C(i), 8.); end; run;
I updated the syntax (below). The new variables are still numeric. I need character variables. I am unsure how to do this.
data RAW.READMISSIONS_INDEX_PN_1216_KYLE; set &ADMISSION; array DIAG_N(26:53) DIAG_N26 DIAG_N27 DIAG_N28 DIAG_N29 DIAG_N30 DIAG_N31 DIAG_N32 DIAG_N33 DIAG_N34 DIAG_N35 DIAG_N36 DIAG_N37 DIAG_N38 DIAG_N39 DIAG_N40 DIAG_N41 DIAG_N42 DIAG_N43 DIAG_N44 DIAG_N45 DIAG_N46 DIAG_N47 DIAG_N48 DIAG_N49 DIAG_N50 DIAG_N51 DIAG_N52 DIAG_N53; array DIAG(26:53) DIAG26 DIAG27 DIAG28 DIAG29 DIAG30 DIAG31 DIAG32 DIAG33 DIAG34 DIAG35 DIAG36 DIAG37 DIAG38 DIAG39 DIAG40 DIAG41 DIAG42 DIAG43 DIAG44 DIAG45 DIAG46 DIAG47 DIAG48 DIAG49 DIAG50 DIAG51 DIAG52 DIAG53; array EDGSCD_N(01:12) EDGSCD_N01 EDGSCD_N02 EDGSCD_N03 EDGSCD_N04 EDGSCD_N05 EDGSCD_N06 EDGSCD_N07 EDGSCD_N08 EDGSCD_N09 EDGSCD_N10 EDGSCD_N11 EDGSCD_N12; array EDGSCD(01:12) EDGSCD01 EDGSCD02 EDGSCD03 EDGSCD04 EDGSCD05 EDGSCD06 EDGSCD07 EDGSCD08 EDGSCD09 EDGSCD10 EDGSCD11 EDGSCD12; array EVRSCD_N(01:12) EVRSCD_N01 EVRSCD_N02 EVRSCD_N03 EVRSCD_N04 EVRSCD_N05 EVRSCD_N06 EVRSCD_N07 EVRSCD_N08 EVRSCD_N09 EVRSCD_N10 EVRSCD_N11 EVRSCD_N12; array EVRSCD(01:12) EVRSCD01 EVRSCD02 EVRSCD03 EVRSCD04 EVRSCD05 EVRSCD06 EVRSCD07 EVRSCD08 EVRSCD09 EVRSCD10 EVRSCD11 EVRSCD12; array PVSNCD_KF_N(01:25) PVSNCD_KF_N01 PVSNCD_KF_N02 PVSNCD_KF_N03 PVSNCD_KF_N04 PVSNCD_KF_N05 PVSNCD_KF_N06 PVSNCD_KF_N07 PVSNCD_KF_N08 PVSNCD_KF_N09 PVSNCD_KF_N10 PVSNCD_KF_N11 PVSNCD_KF_N12 PVSNCD_KF_N13 PVSNCD_KF_N14 PVSNCD_KF_N15 PVSNCD_KF_N16 PVSNCD_KF_N17 PVSNCD_KF_N18 PVSNCD_KF_N19 PVSNCD_KF_N20 PVSNCD_KF_N21 PVSNCD_KF_N22 PVSNCD_KF_N23 PVSNCD_KF_N24 PVSNCD_KF_N25; array PVSNCD_KF(01:25) PVSNCD_KF01 PVSNCD_KF02 PVSNCD_KF03 PVSNCD_KF04 PVSNCD_KF05 PVSNCD_KF06 PVSNCD_KF07 PVSNCD_KF08 PVSNCD_KF09 PVSNCD_KF10 PVSNCD_KF11 PVSNCD_KF12 PVSNCD_KF13 PVSNCD_KF14 PVSNCD_KF15 PVSNCD_KF16 PVSNCD_KF17 PVSNCD_KF18 PVSNCD_KF19 PVSNCD_KF20 PVSNCD_KF21 PVSNCD_KF22 PVSNCD_KF23 PVSNCD_KF24 PVSNCD_KF25; do i = 26 to 53; DIAG_N(i) = ' '; DIAG(i) = put(DIAG_N(i), 8.); end; do j = 01 to 12; EVRSCD_N(j) = ' '; EVRSCD(j) = put(EVRSCD_N(j), 8.); end; do k = 01 to 25; PVSNCD_KF_N(k) = ' '; PVSNCD_KF(k) = put(PVSNCD_KF_N(k), 8.); end; do l = 01 to 12; EDGSCD_N(l) = ' '; EDGSCD(l) = put(EDGSCD_N(l), 8.); end; run; proc contents data=RAW.READMISSIONS_INDEX_PN_1216_KYLE; run;
Declare them as characters before the SET statement.
length diag26-diag53 $8.;
If you want to define the variables as character then tell SAS that.
Either define them first using a LENGTH statement
length DIAG26-DIAG53 $8 ;
, or just tell the ARRAY statement that you want them to character variables.
array DIAG(26:53) $8 DIAG26-DIAG53 ;
This works.
I have a variable named PROC#, where the # is a number between 16 and 25. It is bolded blue within the do loop as if to generate a proc statement, which is not what I want. How do I edit this to make it an array?
data RAW.READMISSIONS_INDEX_PN_1216; set &ADMISSION; array PROC(16:25) $7 PROC16-PROC25; do m = 16 to 25; PROC(m) = ' '; end; run;
Is this something you're actually trying to do? If so, you can use CALL MISSING instead.
call missing(of proc16-proc25);
No arrays or loops needed.
When you define an array as :
array DIAG(28)
It is indexed from 1 to 28.
You can create your own indexes as illustrated:
array Diag(26:53) ;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.