Statalist The Stata Listserver


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

Re: st: RE: Creating loops to drop observations each time regression is run


From   "Neil Shephard" <nshephard@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: RE: Creating loops to drop observations each time regression is run
Date   Sun, 2 Apr 2006 08:52:11 +0800

On 4/2/06, Thuy Le <tl6775a@american.edu> wrote:
> You can use the 'foreach'. See help on 'foreach' for details.

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
}

Note that countries where there are two parts to the name need to be
encapsulated in double quotes.

Nick Cox has written a "Speaking Stata" article on this very topic...

Cox, NJ (2002) Speaking Stata: How to face lists with fortitude. Stata
Journal 2:202-222

http://ideas.repec.org/a/tsj/stataj/v2y2002i2p202-222.html

Alternatively you may be interested in Roger Newson's -parmby- command
(part of the parmest package).  See -ssc desc parmest- for more
information and -ssc install parmest- to install.

Neil
--
"The best safety device in climbing is always situated between your
ears" - Ross Weiter, Perth Rock Climbing Guide (2002)

Email - nshephard@gmail.com / neilshep@cyllene.uwa.edu.au
Website - http://slack.ser.man.ac.uk/
Blog - http://slack---line.blogspot.com/
Flickr - http://www.flickr.com/photos/slackline/

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