From: "Nick Cox" <email@example.com>
Subject: st: RE: retrieving a three-letter word from a string variable
Date: Fri, 28 May 2004 16:04:51 +0100
One way to do this is
gen whatever = "HMV" if index(myvar, "HMV")
If you're fond of -cond()- you might prefer
gen whatever = cond(index(myvar, "HMV"), "HMV", "")
Neither is a retrieval solution, but the
result is the same.
Another way to do it is through retrieval:
gen whatever = substr(myvar,index(myvar, "HMV"),3)
if index(myvar, "HMV")
but I can't see any advantage to that.
> I have a dataset that contains several string variables.
> Suppose one string variable contains the following values in
> one of the observations:
> Another string variable contains :
> COMSTL GTRI(HMV)
> I want stata to create a new variable that retrieves only the
> three-letter word "HMV" from the string variable. I
> understand that the substr command does that but because the
> starting point of the word HMV is not the same for every
> variable , it would be difficult to identify the correct
> starting position to read the string and retrieve the first
> three letters HMV.
> Can somebody help please? I have tried the other string
> commands but could not understand one that does what I want.
* For searches and help try: