DATA Step, Macro, Functions and more

count occurences of specific words in a separate table

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 15
Accepted Solution

count occurences of specific words in a separate table

Hi everyone,

 

I have two tables. Table 1 looks like this :

 

cat
dog
lion
hamster
bear
guinea pig

 

And table 2 looks like this :

 

my dog is stupid
that cat likes my dog
fat hamster
bear are dangerous
lions are dangerous
lions eat antelopes
I like cats and lions
giraffes are cute

 

What I'd like as a result is to have a count of table 1 words occurences in table 2. To make myself clear, something like this :

 

words of

table 1

occurrences in table 2
cat2
dog2
lion3
hamster1
bear1
guinea pig0

 

You will notice that plural forms of words, like "lions" for instance, are counted. In fact, we're looking for the chain of character "lion" in table 2, not the exact word.

 

I've been trying to split the strings in table 2 into words with the scan function, and running some proc freq and merges, but I'm not going anywhere.

 

Don't hesitate to ask any question. English is not my mother language so maybe I'm not being very clear.


Accepted Solutions
Solution
‎01-30-2018 09:43 AM
PROC Star
Posts: 1,283

Re: count occurences of specific words in a separate table

[ Edited ]
Posted in reply to alex_philby

An SQL approach

 

data have1;
input animals$50.;
datalines;
cat
dog
lion
hamster
bear
guinea pig
;

data have2;
input sentence$200.;
infile datalines truncover;
datalines;
my dog is stupid
that cat likes my dog
fat hamster
bear are dangerous
lions are dangerous
lions eat antelopes
I like cats and lions
giraffes are cute
;

proc sql;
	create table want as
	select animals
		  ,sum(ifn(find(sentence, strip(animals))>0, 1, 0)) as occurences
	from have1, have2
	group by animals
	order by calculated occurences desc;
quit;

 

View solution in original post


All Replies
PROC Star
Posts: 1,283

Re: count occurences of specific words in a separate table

Posted in reply to alex_philby

What if the word "dog" appears twice in the same line?

Occasional Contributor
Posts: 15

Re: count occurences of specific words in a separate table

Sorry, it would count as a single occurence Smiley Happy
Solution
‎01-30-2018 09:43 AM
PROC Star
Posts: 1,283

Re: count occurences of specific words in a separate table

[ Edited ]
Posted in reply to alex_philby

An SQL approach

 

data have1;
input animals$50.;
datalines;
cat
dog
lion
hamster
bear
guinea pig
;

data have2;
input sentence$200.;
infile datalines truncover;
datalines;
my dog is stupid
that cat likes my dog
fat hamster
bear are dangerous
lions are dangerous
lions eat antelopes
I like cats and lions
giraffes are cute
;

proc sql;
	create table want as
	select animals
		  ,sum(ifn(find(sentence, strip(animals))>0, 1, 0)) as occurences
	from have1, have2
	group by animals
	order by calculated occurences desc;
quit;

 

Occasional Contributor
Posts: 15

Re: count occurences of specific words in a separate table

Thanks a lot. It works with the small data I've given as an example. Sadly, when I use it on the tables I'm working with (table 1 contains 1.7 millions observations and table 2, 7k observations), I get a "sort execution failure" error. I'm now trying to deal with this...I've found it could be a memory issue.

Valued Guide
Posts: 580

Re: count occurences of specific words in a separate table

Posted in reply to alex_philby

Using a hash-object and an iterator:

 

data have1;
input animal $50.;

occurences = 0;

datalines;
cat
dog
lion
hamster
bear
guinea pig
;
run;

data have2;
input sentence $200.;
infile datalines truncover;

datalines;
my dog is stupid
that cat likes my dog
fat hamster
bear are dangerous
lions are dangerous
lions eat antelopes
I like cats and lions
giraffes are cute
;
run;

data _null_;
   if 0 then set work.have1;

   set work.have2 end= jobDone;

   if _n_ = 1 then do;
      declare hash h(dataset: 'work.have1', multidata: 'yes');
      declare hiter iter('h');
      h.defineKey('animal');
      h.defineData('animal', 'occurences');
      h.defineDone();
   end;

   rc = iter.first();

   do while (rc = 0);
      if find(sentence, animal, 'it') then do;
         occurences = occurences + 1;
         h.replace();         
      end;

      rc = iter.next();
   end;

   if jobdone then do;
      h.output(dataset: 'work.want');
   end;
run;

proc sort data=work.want;
   by descending occurences;
run;

Without the not-exact-match requirement something like the 5th example in http://support.sas.com/documentation/cdl/en/lecompobjref/69740/HTML/default/viewer.htm#p00ilfw5pzcjv... could be created - faster and less code.

Occasional Contributor
Posts: 15

Re: count occurences of specific words in a separate table

Posted in reply to andreas_lds
It works with small data tables, but my SAS session still crashes when I try to run it on the 1mil+ observations tables.
Trusted Advisor
Posts: 1,345

Re: count occurences of specific words in a separate table

[ Edited ]
Posted in reply to alex_philby

This program does not attempt a Cartesian join in memory, so it may be more efficient:

 

Editted modification.  I overlooked the fact that you wanted frequencies in descending order, with just 2 columns.  Here's a revised program using the same approach:

 

data have1;
input animals $50.;
datalines;
cat
dog
lion
hamster
bear
guinea pig
;

data have2;
input sentence $200.;
infile datalines truncover;
datalines;
my dog is stupid
that cat likes my dog
fat hamster
bear are dangerous
lions are dangerous
lions eat antelopes
I like cats and lions
giraffes are cute
;
proc sql noprint;
  select max(length(trim(animals))) into :maxlen from have1;
  select quote(trim(animals))       into :val_list separated by "," from have1;
quit;
%put _user_;

data want (keep=animals freq);
  array n_   {&sqlobs} ;
  array vals {&sqlobs} $&maxlen (&val_list);

  set have2 end=eoh;
  do w=1 to &sqlobs;
    n_{w} + (find(sentence, strip(vals{w}))>0);
  end;

  if eoh;
  do i=1 to &sqlobs;
    ix=whichn(max(of n_{*}),of n_{*});
    animals=vals{ix};
    freq=n_{ix};
    output;
    n_{ix}=.;
  end; 
run;

 

 

 

 

Occasional Contributor
Posts: 15

Re: count occurences of specific words in a separate table

This one too is working with small data tables, but makes my SAS session crash when I try to run it on the 1mil+ observations tables. Sorry everyone, I guess it's just a memory problem. I can run simple data step on the mentionned tables, but your programs makes my session crash, for some reason.
Super User
Posts: 10,784

Re: count occurences of specific words in a separate table

Posted in reply to alex_philby

I don't understand

"In fact, we're looking for the chain of character "lion" in table 2, not the exact word."

 

 

data have1;
input animals$50.;
datalines;
cat
dog
lions
hamster
bear
guinea pig
;

data have2;
input sentence$200.;
infile datalines truncover;
datalines;
my dog is stupid
that cat likes my dog
fat hamster
bear are dangerous
lions are dangerous
lions eat antelopes
I like cats and lions
giraffes are cute
;

data _null_;
 if _n_=1 then do;
   if 0 then set have1;
   declare hash h(dataset:'have1');
   declare hiter hi('h');
   h.definekey('animals');
   h.definedone();

   declare hash hh();
   hh.definekey('animals');
   hh.definedata('animals','n');
   hh.definedone();
 end;
set have2 end=last;
do while(hi.next()=0);
 if find(sentence,strip(animals)) then do;
  if hh.find()=0 then do;n=n+1;hh.replace(); end;
   else do;n=1;hh.add();end;
 end;
end;

if last then hh.output(dataset:'want');
run;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 9 replies
  • 204 views
  • 6 likes
  • 5 in conversation