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

st: RE: Avoiding loops (the while loop)


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Avoiding loops (the while loop)
Date   Sat, 26 Jun 2004 13:31:19 +0100

You say you want to avoid loops. I don't 
know why, as the alternative is to type 
out every command. 

Also, this looks like 21 variables to me. 

I can't comment on how efficient your 
code is without seeing it. 

As I understand your problem, I would 
tackle it something like this: 

qui forval v = 1/10 { 
	local V : di %02.0f `v' 
	gen int`V' = . 
	gen slope`V' = . 
	forval m = 1/12 { 
		regress y`V' x`V' if month == `m' 
		replace int`V' = _b[_cons] if month == `m' 
		replace slope`V' = _b[x`V'] if month == `m' 
	} 
}  

egen tag = tag(month) 
list int* slope* if tag 

(See also my answer to your other question.) 

Nick
[email protected] 

Subhankar Nayak
 
> I want to avoid loops in the following analysis and will 
> appreciate it if
> anyone can suggest an alternative..
> I have a very simple dataset...
> 
> 11 variables:    (month),    (y01-y10),    and (x01-x10) for 
> 1000 months
> So, it is a 1000*11 dataset...
> 
> What I want to do is: each month, I regress (y01-y10) on 
> (x01-x10) and store
> the intercept and slope...
> I need these intercept and slopes for later analysis...
> 
> What I am doing now is a while loop based on month: carry the 
> regressions on
> a monthly basis and store the results.
> This seems a very inefficient way of handling this. It is 
> taking time too,
> because I have to do many similar analyses later on.
 

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