BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
robertrao
Quartz | Level 8

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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.

View solution in original post

8 REPLIES 8
Linlin
Lapis Lazuli | Level 10

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;

robertrao
Quartz | Level 8

Hi Thanks for the reply.

I still get that error even after putting the dollar sign

Regards

Astounding
PROC Star

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.

robertrao
Quartz | Level 8

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;

Astounding
PROC Star

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.

Reeza
Super User

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.

robertrao
Quartz | Level 8

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

Tom
Super User Tom
Super User

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 955 views
  • 3 likes
  • 5 in conversation