Selecting consecutive elements from an array

Reply
N/A
Posts: 1

Selecting consecutive elements from an array

I am trying to find a way to select consecutive elements in an array for comparison using the least amount of code as possible. For example, I have two arrays, Dx and ICD, and I want to compare each element of Dx to a group of elements in ICD... I'm checking to see if a particular diagnosis code belongs to a certain family of codes.

Ex.

data test.test;

     set test.dx;

     retain cat1 cat2 cat3....... cat49;

     ARRAY DX(4) DX1 DX2 DX3 DX4;

     ARRAY ICD(49)  $7. c1 - c49('code1', 'code2', 'code3',....       ....., 'code49');

do I= 1 to dim(DX);

     if DX(I) = ICD(1) then cat1='first family';

     else if DZ(I) =ICD(2) then cat1='second family;

/* what if I wanted to compare a single value from the first array to a group of consecutive values in the second array

say... else if DX(I) = ICD(3) - ICD(20) then cat3='third family';     * I know this is syntactically incorrect

If I didn't want to do a DX(I) =ICD(3) DX(I) =ICD(4)......    ..... DX(I)=ICD(20) comparison for each element separately,

how can I do this in one step?

Super User
Posts: 10,497

Re: Selecting consecutive elements from an array

I think you need to provide some example input data and expected output.

It also may be worth considering a custom format or two for your ICD codes.

If you aren't really married to "first" "second" etc

Also I'm not sure if your example code in the else is what you intended as you have Cat1='second family' but then

do I=1 to dim(dx);

     if DX(I) = ICD(1) then cat1='first family';

     else if DX(I) =ICD(2) then cat1='second family;

     else do j=3 to 20;

          if dx(I) = ICD(j) then cat3 = 'third family';

     end;

end;

and since you have cat1-cat49 and c1-c49 I suspect there's more going on than you may have stated, which is why example input and output data may be needed.

Trusted Advisor
Posts: 1,204

Re: Selecting consecutive elements from an array

Not sure about the logic behind this looping. How about if dx1=icd5 but not equal to icd6 what will be the value of cat3 then?

Respected Advisor
Posts: 3,777

Re: Selecting consecutive elements from an array

I might consider making some arrays of the families and then using them with IN array-name operator.  Also what @stat@sas said that loop logic looks fishy.

data test.test;
   set test.dx;
   retain cat1 cat2 cat3....... cat49;
   ARRAY DX(
  • DX1 DX2 DX3 DX4;
  •    ARRAY ICD(49$7. c1 - c49('code1', 'code2', 'code3',....       ....., 'code49');
       array fam3
  • c3-c20;
  •    array fam4
  • c21-c31;

  •   
    do I= 1 to dim(DX);
          if      DX(I) eq ICD(1) then cat1='first family';
         
    else if DX(I) eq ICD(2) then cat1='second family';
         
    else if DX(I) in fam3 then cat3='third family';
    Ask a Question
    Discussion stats
    • 3 replies
    • 210 views
    • 0 likes
    • 4 in conversation