Bookmark and Share

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]

st: Using "x" from the ""x" real change made" output of -replace-


From   [email protected]
To   [email protected]
Subject   st: Using "x" from the ""x" real change made" output of -replace-
Date   Mon, 3 Dec 2012 17:28:02 -0500

Hi all,

How do I go about using "x" from the ""x" real change made" output of
-replace-?
I am using Stata12 for Windows.

I am using an iterative procedure and I would like to stop iterating when
there are no more real changes happening through -replace-.

Here is a code that would work if "r(change)" after replace would return
"x" from the ""x" real change made" output of replace.

gen ID3 = ID2
local change = 1
while `change' > 0{
sort ID t2
by ID: replace ID3=ID3[1]
local change1 = r(change)
sort ID2 t2
by ID2: replace ID3=ID3[1]
local change2 = r(change)
replace ID2=ID3
local change = `change1' + `change2'
}

Based on the help file of generate and replace, there does not seem to be
a direct way of obtaining "x" from the ""x" real change made" output. Does
a statalist user have a suggestion?

The following works, but I am seeking a more elegant/fast solution:

gen ID3 = ID2
local change = 1
while `change' > 0{
sort ID t2
by ID: gen diff=1 if ID3!=ID3[1]
count if diff==1
local change1 = r(N)
by ID: replace ID3=ID3[1]
sort ID2 t2
by ID2: gen diff2=1 if ID3!=ID3[1]
count if diff2==1
local change2 = r(N)
by ID2: replace ID3=ID3[1]
replace ID2=ID3
drop diff diff2
local change = `change1' + `change2'
}

Thank you

- Maxime Roy


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index