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

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


From   "Michael Blasnik" <michael.blasnik@verizon.net>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: Re: Accessing the number of changes made by a "replace" command
Date   Fri, 18 Mar 2005 08:33:03 -0500

Although you can solve this problem by using a count statement immediately before the replace command to test how many will be replaced, I still wish that Stata would make the number of changes available to the user. In a very large dataset, the extra time required just for the added count command can add up, especially in a loop that may execute many times through. For example, when I need to clean up text variables that may have multiple consecutive embedded spaces (sometimes up to 20 or 30 spaces may be padded inside a string) and I want no more than one space, I need to do this:

count if index(name," ")
while r(N)>0 {
replace name=subinstr(name," "," ",.)
count if index(name," ")
}

On a dataset with 1,000,000 observations and a string variable that may require 10-20 passes through this loop and perhaps several of these string variables, the time required for the count statements can become non-trivial. Although it isn't a tremendous amount of time, it seems like purely wasted time since the information needed to avoid the count statement is printed right on the screen.

I would think that it wouldn't be too difficult to have the replace command put the number of changes in someplace like s(changes).

Michael Blasnik
michael.blasnik@verizon.net

----- Original Message ----- From: "David Fisher" <djfisher81@msn.com>
To: <statalist@hsphsun2.harvard.edu>
Sent: Friday, March 18, 2005 5:03 AM
Subject: st: Accessing the number of changes made by a "replace" command



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.
Thanks!
David Fisher.
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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