turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- SAS Procedures
- /
- Arrays

Topic Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

04-30-2013 01:54 PM

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;

Accepted Solutions

Solution

04-30-2013
02:22 PM

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to robertrao

04-30-2013 02:22 PM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to robertrao

04-30-2013 02:17 PM

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;

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Linlin

04-30-2013 02:24 PM

Hi Thanks for the reply.

I still get that error even after putting the dollar sign

Regards

Solution

04-30-2013
02:22 PM

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to robertrao

04-30-2013 02:22 PM

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.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Astounding

04-30-2013 02:47 PM

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;

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to robertrao

04-30-2013 02:54 PM

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.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to robertrao

04-30-2013 03:01 PM

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.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Reeza

04-30-2013 03:42 PM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to robertrao

04-30-2013 06:04 PM

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;