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]

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


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: Using "x" from the ""x" real change made" output of -replace-
Date   Mon, 3 Dec 2012 22:11:05 +0000

This is essentially anonymous. As the FAQ makes explicit, we ask for
full real names on Statalist.

Nick

On Mon, Dec 3, 2012 at 9:33 PM,  <[email protected]> wrote:

> 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'
> }
*
*   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