Search code examples
stringsasdo-loopsdatastep

SAS Index on Array


I am trying to search for a keyword in a description field (descr) and if it is there define that field as a match (what keyword it matches on is not important). I am having an issue where the do loop is going through all entries of the array and . I am not sure if this is because my do loop is incorrect or because my index command is inocrrect.

data JE.KeywordMatchTemp1;
  set JE.JEMasterTemp;
  if _n_ = 1 then do;
    do i = 1 by 1 until (eof);
    set JE.KeyWords end=eof;
    array keywords[100] $30 _temporary_;
    keywords[i] = Key_Words;
  end;
  end;
  match = 0;
  do i = 1 to 100 until(match=1);
    if index(descr, keywords[i]) then match = 1;
  end;
  drop i;
run;

Solution

  • Add another condition to your DO loop to have it terminate when any match is found. You might want to also remember how many entries are in the array. Also make sure to use INDEX() function properly.

    data JE.KeywordMatchTemp1;
      if _n_ = 1 then do;
        do i = 1 by 1 until (eof);
          set JE.KeyWords end=eof;
          array keywords[100] $30 _temporary_;
          keywords[i] = Key_Words;
        end;
        last_i = i ;
        retain last_i ;
      end;
      set JE.JEMasterTemp;
      match = 0;
      do i = 1 to last_i while (match=0) ;
        if index(descr, trim(keywords[i]) ) then match = 1;
      end;
      drop i last_i;
    run;