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.
Please correct me
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;
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.
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;
Hi Thanks for the reply.
I still get that error even after putting the dollar sign
Regards
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.
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;
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.
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.
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
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.