You effectively want the most frequent word in a set of words, all of which are in the same observation. This can also be done using a single character variable containing that list of words:
data want (drop=_: i);
set have;
length _word_list $100 _word $3;
_word_list=catx(' ',of hp_:);
do while (_word_list^=' ');
_word=scan(_word_list,-1);
_freq=0;
do i=1 to countw(_word_list);
if scan(_word_list,i)=_word then _freq=_freq+1;
end;
if _freq > mode_freq then do;
mode_freq=_freq;
mode=_word;
end;
_word_list=tranwrd(_word_list,trim(_word),' ');
end;
run;
Note that the assignment of a value to _WORD using
_word=scan(_word_list,-1);
continually takes the rightmost available value (i.e. the latest) instead of the leftmost. Then, if there is a tie in the frequency of two words, the subsequent statement
if _freq > mode_freq then do;
guarantees that the most recent (rightmost) word with the tied frequency is assigned, per the request.
... View more