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]

From |
Nick Cox <njcoxstata@gmail.com> |

To |
statalist@hsphsun2.harvard.edu |

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

Date |
Mon, 3 Dec 2012 22:43:03 +0000 |

I doubt that anyone would want to try to follow exactly what you are doing here without much more explanation. But this question reminds me of a discussion several years ago on Statalist, although I won't try to search for "replace" as a keyword in the archives. In principle, it would be very useful if -replace- were a r-class command so that you could just inspect say r(change) to see if it were 0. Or so it might seem. Any way, changing -replace- to a r-class command would wreak havoc among programs and do files across Stata, to say nothing of other issues. And -replace- does not do that now, so speculation helps you not at all. So, in general I know of no substitute for comparing the absolute or relative difference of latest with previous. Depending on what kind of iteration is being used, convergence might be flagged by something other than exact zero change. Nick Maxime Roy asked > 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/

**Follow-Ups**:**Re: st: Using "x" from the ""x" real change made" output of -replace-***From:*maximer@andrew.cmu.edu

**References**:**st: Using "x" from the ""x" real change made" output of -replace-***From:*maximer@andrew.cmu.edu

**st: Using "x" from the ""x" real change made" output of -replace-***From:*maximer@andrew.cmu.edu

- Prev by Date:
**st: Using "x" from the ""x" real change made" output of -replace-** - Next by Date:
**Re: st: Loading long string variables (from SQL) into Stata** - Previous by thread:
**st: Using "x" from the ""x" real change made" output of -replace-** - Next by thread:
**Re: st: Using "x" from the ""x" real change made" output of -replace-** - Index(es):