Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Accessing the number of changes made by a "replace" command

From   David Kantor <[email protected]>
To   [email protected]
Subject   Re: st: Accessing the number of changes made by a "replace" command
Date   Fri, 18 Mar 2005 12:21:14 -0500

At 10:03 AM 3/18/2005 +0000, David Fischer wrote:
Hi all,
I am attempting to create a "while" loop that executes, amongst other things, a "replace" command on a particular variable. I would like this loop to continue until the "replace" command cannot make any more changes to the data. In order to do this I would like to be able to access the number of (real) changes made by the "replace" command, so that I can say something like "loop while (number of real changes)!=0". Is there a way to do this in Stata? I thought there might be an underscore variable that would do this, but I can't find one.
David Fisher.
Coincidentally, just yesterday, I wrote to Stata Tech Support, asking about this and suggesting that they provide a returned value. It's potentially under consideration, but there are some issues involved.

Until then, the only way to correctly do it is ...
gen savedcopy = myvar // possibly with a type inserted after -gen-
replace myvar = whatever...
count if savedcopy != myvar
and test r(N)

One other suggestion would be along the lines of...
count if condition
... and pick up the value of r(N) here ...
replace myvar = whatever if condition

This doesn't work in general, because you may count more observations that actually get a changed value. It is correct only if you are certain that every observation that qualifies under the condition will, indeed, receive a changed value in the -replace- -- as is the case in the example that Michael Blasnik presented.

The "issues" I alluded to above (as I see it) are that, if they were to place the value into an r() returned value, it could break some existing programs and do-files. Such a change would cause other r() values to be wiped out, and your program might be expecting to find it:

summ thisvar
replace someothervar = something
...code that looks at the r() values from the above -summ- command...

This would cease to work if the -replace- were made r-class. (The above example may be poor coding practice, but it would work, in Stata as we know it now.) Making it s-class, as Michael suggested, would cure the above example, but still not solve the problem in general. Thus, if such a change gets implemented, it would need some warnings to go with it. But, of course, -version- ought to take care of it as well.

I hope this has been interesting.

David Kantor
Institute for Policy Studies
Johns Hopkins University
[email protected]
* For searches and help try:

© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index