Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: creating loops to drop observations


From   Kit Baum <[email protected]>
To   [email protected]
Subject   st: creating loops to drop observations
Date   Mon, 3 Apr 2006 07:31:45 -0400

But why drop observations? Stata's if qualifier is perfectly capable of excluding them...

use dataset, clear
forval x=1/N {
regress y x if code ~= "country`x'"
}

That will run much faster for a sizable dataset since it does not keep reading the data over and over.
Furthermore if you are going to save any results of this cross- country estimation you do not want to keep changing the dataset.

Kit Baum, Boston College Economics
http://ideas.repec.org/e/pba1.html


On Apr 3, 2006, at 2:33 AM, statalist-digest wrote:


Although in this example forval may be more appropriate...

use dataset.dta, clear
drop if code=="country1"
regress y x

use dataset.dta, clear
drop if code=="country2"
regress y x

Assuming that code is always countryN then you could...

forval x = 1/N{
use dataset, clear
drop if(code == "country`x'")
regress y x
}

You would substitute N in the above for the maximum country number.

If however you actually have named countries such as "Australia",
"United States", "Germany", "Nepal", "United Kingdom", "France" etc.
then you would have to use foreach....

foreach x in Australia "United States" France Germany "United Kingdom" Nepal{
use dataset, clear
drop if(code == "`x'")
regress y x
}
*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index