I have a string variable (numberlist) that contains a list of numbers with spaces in between them (e.g. "41 54 1 46 54 23"). I want to see if a different variable in my dataset (id) is in this list of numbers. The obvious approach is using strpos:
gen match=1 if strpos(numberlist, string(id))
The problem with this is is that it matches 1 to 41. Which makes sense, 1 is indeed in 41, but it is not the behaviour I am looking for.
I suppose I could simply -split- the numberlist variable, and then loop over all the resulting variables. This is a bit cumbersome though, and I'm afraid it might balloon the size of the dataset (at 35 MB it is already rather large). A more elegant solution would be preferable.
gen match=1 if strpos(numberlist, string(id))
The problem with this is is that it matches 1 to 41. Which makes sense, 1 is indeed in 41, but it is not the behaviour I am looking for.
I suppose I could simply -split- the numberlist variable, and then loop over all the resulting variables. This is a bit cumbersome though, and I'm afraid it might balloon the size of the dataset (at 35 MB it is already rather large). A more elegant solution would be preferable.
Comment