Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: Re: Identify and Replace Values
From
Eric Booth <[email protected]>
To
"<[email protected]>" <[email protected]>
Subject
Re: st: Re: Identify and Replace Values
Date
Tue, 13 Jul 2010 21:30:10 +0000
<>
The upshot of the example I provided was to use -tokenize- and a loop to replace your `others' variables with something that -inlist()- could understand.
The problem with the last 2 lines of code that you asked about again is that Stata sees the "if inlist(traded_models, "`others'")" part as "if inlist(traded_models, "``"aa aa"' `"f3"' `"f4"' `"f5"' `"g1"' `"g2"' `"g3"'")--if you're using the data I provided in my example (by the way, check out Maartin Buis's FAQ on how to run examples provided on Statalist ((http://www.maartenbuis.nl/example_faq/index.html))).
You'll notice that this matches up with what is displayed if you run " di `"`others'"'" in my example. The problem is, as Martin Weiss points out, that -inlist- expects the comma separated list which is why your last two lines of code returns 0 changes.
The combination of -tokenize- and a -while- loop is probably a bit more complicated than using a -foreach- loop, so you could exchange my previous example with the code below to get the same result using -foreach- (also, I've traced some of the -replace- syntax with -tr- (from SSC) so you can see how Stata is interpreting it):
**************!
clear
inp str10 (traded_model new_model)
"g1" "f1"
"g2" "f2"
"g3" "f3"
"g4" "f4"
"f1" "g4"
"f2" "f5"
"g4" "g5"
"g5" "aa aa"
end
levelsof traded_model, local(traded)
levelsof new_model, local(new)
local onlytraded : list traded - new
local onlynew : list new - traded
local others : list onlynew | onlytraded
****************
di `"`onlytraded'"'
di `"`onlynew'"'
di `"`others'"'
****************
// I'VE ONLY CHANGED BELOW THIS LINE -->
//FIRST, install -tr- to take a look at -replace- inside the loop
which tr
if _rc ssc instal tr, replace
**
//SECOND, run a -foreach- loop
foreach v of local others {
tr: replace traded_model="OTHERS" if inlist(traded_model,"`v'")
tr: replace new_model ="OTHERS" if inlist(new_model,"`v'")
}
**************!
This will give you the same result. Finally, you could use macro extended functions to change your others macro so that -inlist- will accept it without a loop of any sort. For example, substitute the -foreach- loop above with this code:
*****
local others: subinstr local others " " ",", all
di `"`others'"'
tr: replace traded_model="OTHERS" if inlist(traded_model, `others' ) //note that you don't need the double quotes around `others' here
tr: replace new_model ="OTHERS" if inlist(new_model,`others')
*****
^ Again, with any of these methods, you don't need to use -subinstr- to change spaces to _ for your models.
~ Eric
__
Eric A. Booth
Public Policy Research Institute
Texas A&M University
[email protected]
Office: +979.845.6754
On Jul 13, 2010, at 5:59 AM, Hobst wrote:
I dont get that one.. What are you doing with this inp str10? But anyways i
have already renamed all the Model Names, so i am still working with the
following code:
levelsof traded_model, local(traded)
levelsof new_model, local(new)
local onlytraded : list traded - new
local onlynew : list new - traded
local others : list onlynew | onlytraded
replace traded_model="others" if inlist(traded_model,"`others'")
replace new_model ="others" if inlist(new_model,"`others'")
Does anybody see a reason why i always get the "0 real changes made" for the
last two commands? Is there a mistake in the syntax somewhere, i dont see
the problem. I think there must be something wrong with lines 3-5, i cant
find the error.....
Thank you..
--
View this message in context: http://statalist.1588530.n2.nabble.com/Identify-and-Replace-Values-tp5283591p5286941.html
Sent from the Statalist mailing list archive at Nabble.com.
*
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/