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

st: RE: programming question


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: programming question
Date   Wed, 3 Jul 2002 15:27:34 +0100

Maureen Paul wrote 
> 
> I am writing a programme where I would like the user to input a 
> variable for which they would like a test for heteroscedasticity. I 
> am trying to match that variable to a a corresponding e2var?q, then 
> generate a variable equal to that e2var?q which I will then use in 
> the heteroscedasticity test. I have tried so many things and what 
> follows is my lastest attempt. I suppose using the if statement 
> within the while loop may be a problem but I have tried various other 
> things. Any suggestions?
> 
> local k=2
>   while "``k''" ~= "" {
> 	local kless1 = `k' - 1
> 	tempvar e2var`kless1'q  hetvar
> 	ge `e2var`kless1'q' = `e2qq' * ``k'' `if'
>          
>       if "`heteroscedasticity'" ~= "" { parse "`heteroscedasticity'", 
> parse(" ") 
> 	    local namevar "`1'" 
>  	    foreach var of varlist `varlist' {
> 	          if "`var'"=="``k''" {ge `hetvar'=`e2var`kless1'q'' `if'} 
> 	     }
> 
> local k = `k'+1
> 
> }
>                          .
>                          .
>                          .
>                          .
> 
> if "`heteroscedasticity'" ~= "" {
> 	quietly regress x  y  `hetvar'  z  `if' [`weight' `exp'], noconstant
> 	if e(N) ~= `obs' { display "ERROR " e(N) "observations, when should 
> be " `obs' }
> 	display in text "Heteroscedasticity w.r.t. `namevar'" _col(48) "{c 
> |}" %11.7f as result e(r2)*e(N) %18.7f chi2tail(1,e(r2)*e(N)) 
> 
> }

You are trying to match the elements of a `varlist' with `k'. 

We need to see what is upstream of this to start to try to diagnose 
what is going wrong. 

In addition, what is going wrong? What error message do 
you get? At what point does the program crash? 

However, what you give here makes it evident that you are overwriting 
your own macros. 

Before you enter the -while- loop, `k' is 2 and so 
"``k''" is "`2'". But the -parse- zaps those macros. 
Even if the contents of `heteroscedasticity' is just one word, 
`2' will be set to an empty string. If it isn't `2' will contain
something else. 

The way to avoid that is something like 

	local namevar : word 1 of `heteroscedasticity' 

and avoid the -parse-. 

Nick 
[email protected] 
*
*   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