Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[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   maximer@andrew.cmu.edu
To   statalist@hsphsun2.harvard.edu
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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index