Statalist The Stata Listserver


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

Re: st: Re: Macro syntax help


From   Maarten buis <[email protected]>
To   [email protected]
Subject   Re: st: Re: Macro syntax help
Date   Sun, 5 Feb 2006 19:11:06 +0000 (GMT)

Hi Meryle,

The most likely cause I can think of is that the variable cutoff varries in ways it shouldn't. The
example below (using a constant cutoff, and incorporating Michaels suggestions) seems to work ok. 
An additional suggestion is to use indentation when you write a loop in your do-file, this makes
your code much more readable.

HTH,
Maarten

*----------begin example-------------
set more off
sysuse auto, clear

sum mpg
local m = r(mean)
local sd = r(sd)

gen bfor = .
gen sefor = .
gen nfor = .

local counter = 0
forvalues range = .5(.1)2 {
	quietly {
		local lb = `m' - `range'*`sd' 
		local ub = `m' + `range'*`sd'
		noisily di "lower bound = " `lb' ", and upper bound = " `ub'	

		reg price mpg foreign if mpg>`lb' & mpg<`ub'
		replace bfor = _b[foreign] in `++counter'
                /*`++counter' means add 1 to `counter' and than use it*/
		replace sefor = _se[foreign] in `counter'
		replace nfor = e(N) in `counter'
	}
}
gen window = .4 + .1*_n if bfor ~=.
/*no need to define window in the loop*/
/*_n is 1 in the first record, 2 in the second, etc*/
/*so .4+.1*_n is .5 in the first record, .6 in the second, etc*/
*------------end example------------------

--- Michael Blasnik <[email protected]> wrote:
> First, you don't need to create the indic and blah variables since regress 
> returns the number of observations in e(N) and, even if it didn't, you
> could just use a count command and r(N).  It also isn't clear why you issue
> the same regress command twice and place the same result into two different
> variables.

> ----- Original Message ----- 
> "Meryle Weinstein" <[email protected]> wrote:
> > I'm using the following syntax to run a regression analysis on a
> > subset of cases that fall within a range of values (above/below the
> > cutoff).  For some reason it's running the analysis on a random
> > selection of cases that fall all over the place -- some are within the
> > range, while others are way out of the range.  Any help in finding a
> > bug in the program is appreciated.
> >
> > gen below = .
> > gen above = .
> > gen observ = .
> > gen window = .
> >
> > gen treatfx_m4_2 = .
> > gen se_m4_2 = .
> > gen diff_m4_2 = .
> >
> > local counter = 0
> >
> > forvalues temp=0.25(0.05)10 {
> > replace below = cutoff - `temp'
> > replace above = cutoff + `temp'
> >
> > local counter = `counter' + 1
> >
> > regress p34mg4 ppov bor1t1 if ppov > below & ppov < above
> > replace diff_m4_2 = _b[bor1t1] in `counter'
> >
> > regress p34mg4 ppov bor1t1 if ppov > below & ppov < above
> > replace treatfx_m4_2  = _b[bor1t1] in `counter'
> > replace se_m4_2 = _se[bor1t1] in `counter'
> > gen indic = 1 if ppov > below & ppov < above
> > egen blah = sum(indic)
> > replace observ = blah in `counter'
> >
> > drop blah indic
> >
> > replace window = `temp' in `counter'
> > }
 


-----------------------------------------
**between 1/2/2006 and 31/3/2006 I will be**
**visiting the UCLA, during this time the**
**best way to reach me is by email**

Maarten L. Buis
Department of Social Research Methodology
Vrije Universiteit Amsterdam
Boelelaan 1081
1081 HV Amsterdam
The Netherlands

visiting adress:
Buitenveldertselaan 3 (Metropolitan), room Z214

+31 20 5986715

http://home.fsw.vu.nl/m.buis/
-----------------------------------------


		
___________________________________________________________ 
To help you stay safe and secure online, we've developed the all new Yahoo! Security Centre. http://uk.security.yahoo.com
*
*   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