## Arrays

Solved
Super Contributor
Posts: 1,041

# Arrays

Hi,

I have the following code.

There are a max of 50 codes in the array diag

some can have only 5 codes etc....

i want to put the codes which satisfy the if condition in  seperate columns.

Assuming the max of 10 codes a single observation can satisfy in the if condition ....I am using the following code and get an error

ERROR: Array subscript out of range at line 454 column 13.

data diag_array;
set daig_cohort;
array diag(50) dx1-dx50;
array diag_codes(10) diag_codes1 - diag_codes10;

do i=1 to 35;
if diag(i) in ('123','245','687','890','432','472', '42760', '42761', '4278', '42781', '42789', '4279') then do;
diag_flag=1;
diag_codes(i)=diag(i);
drop i /*diag_code*/;

end;
end;
run;

Accepted Solutions
Solution
‎04-30-2013 02:22 PM
Super User
Posts: 6,785

## Re: Arrays

Also, since i goes up to 50, you can't use it to reference an array that has only 10 elements.  That's the source of the error message about array subscript out of range.  Instead, add before the IF/THEN statement:

j=0;

Then inside the IF/THEN group:

diag_flag=1;

j + 1;

diag_codes{j} = diag{i};

drop i j;

Good luck.

All Replies
Super Contributor
Posts: 1,636

## Re: Arrays

Hi,

if dx1-dx50 are character variables, you need to change

array diag_codes(10) diag_codes1 - diag_codes10;

to

array diag_codes(10) \$ diag_codes1 - diag_codes10;

Super Contributor
Posts: 1,041

## Re: Arrays

I still get that error even after putting the dollar sign

Regards

Solution
‎04-30-2013 02:22 PM
Super User
Posts: 6,785

## Re: Arrays

Also, since i goes up to 50, you can't use it to reference an array that has only 10 elements.  That's the source of the error message about array subscript out of range.  Instead, add before the IF/THEN statement:

j=0;

Then inside the IF/THEN group:

diag_flag=1;

j + 1;

diag_codes{j} = diag{i};

drop i j;

Good luck.

Super Contributor
Posts: 1,041

## Re: Arrays

Hi,

This one works for me.

so j=0 has to be prior to the do loop and after the array statement??? AND

diag_codes(j)=diag(i); has to be after j+1 strictly...

If i changed the order again i getr the same error!!!

Lastly,

dont we need to put j=1 instead of j=0????or both would work fine???

data diag_array;
set daig_cohort;
array diag(50) dx1-dx50;
array diag_codes(10) diag_codes1 - diag_codes10;

j=0
do i=1 to 35;
if diag(i) in ('123','245','687','890','432','472', '42760', '42761', '4278', '42781', '42789', '4279') then do;
diag_flag=1;

j+1;
diag_codes(j)=diag(i);
drop i  j /*diag_code*/;

end;
end;
run;

Super User
Posts: 6,785

## Re: Arrays

Whatever combination of statements you use, the important part is this.  When SAS executes this statement, j must be between 1 and 10:

diag_codes{j} = diag{i};

If j is 0 at that point, you will get the same error message.

Super User
Posts: 23,771

## Re: Arrays

you have an array sized 10, but 12 elements in your IN() component, so you'll run out of spaces if one person has all 12 diagnosis.

Super Contributor
Posts: 1,041

## Re: Arrays

Hi,

Those 12 in the in operator are a subset of 50 codes(max number) from the diag array.

so i am checking to look for any of these 12 codes  in the 50(some might have only 5 codes but the other 45 are missing..

Hope you got what i am trying to explain

Thanks

Super User
Posts: 8,120

## Re: Arrays

Another way to think about the problem is instead of copying the DX codes you could set separate flag variables for each of the codes.

This code will create 10 flag variables. FLAG1 = '1' when diagnosis code '123' is present in any of the 50 DX variables.  FLAG2 is for diagnosis code '245'. etc.

data want ;

set diag_cohort ;

array dx dx1-dx50 ;

array search (10) \$5 _temporary_ ('123','245','687','890','432','472', '42760', '42761', '4278', '42781', '42789', '4279') ;

array flag (10) \$1 ;

do i=1 to dim(search);

if which(search(i),of dx(*) ) then flag(i)='1';

else flag(i)='0';

end;

run;

🔒 This topic is solved and locked.