Hello!

It seems that the command
.egen newvar = nss(myvariable ) , find (string)
does not seem to take as input a string variable value, only a string literal

I have data something like this (this is a toy problem)
ID PETMET PETOWNED
1 dog dog,dog,cat,cat,guinea pig
2 cat dog, dog, cat, fish, snake
And I want a variable that counts the number of occurances of petmet in petowned, preferably without reinflating the petowned variable

I cannot do
egen ownedandmet = nss(petowned), find(petmet) OR
egen ownedandmet = noccur(petowned) , string (petmet)

I need to loop through all values of the variable petmet to do this, since neither of the functions returns the desired value (it returns all zeros). Is this meant to be that way?

levelsof petmet , local (pets)
foreach item in pets {
egen temp = nss(petowned) if petmet == "`item'" , find("`item'")
replace ownedandmet = temp if petmet == "`item'"
drop temp
}


I have quite a few levels of petmet (some 3000 for one case) . Is there a better way to achieve this that I haven't considered or found in my searches?

Grateful for any suggestions
/Barbro