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

st: RE: Problem with "while" and "for" loops.


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Problem with "while" and "for" loops.
Date   Mon, 24 Jun 2002 13:19:05 +0100

Sanmitra Ghosh wrote

> I am trying to calculate trend corrected coefficient
> of variation. But the do file is not running properly.
> I tried it using both while and for loops. The problem
> with the while loop is that if there is any missing
> value for the group variable, the do file terminates
> there giving a r(2000) message. This problem doesn't
> occur with the for loop, since there is a nostop
> option. However, it is giving another error message
> which is r(123), i.e. numlist has too many numbers. I
> don't know how to get rid of these problems. Could
> anyone kindly give me some suggestions? I am attaching
> the do files.

First, please note that Statalist members are asked
_not_ to send attachments to the list, for the usual
reasons (members whose software cannot handle attachments,
precautions against viruses, etc.). This is explained in the FAQ.

Sanmitra's -trial1.do- was

use "C:\WINDOWS\DESKTOP\temp.dta"
sort stcode distcode time
sum stdist
local min = r(min)
local max = r(max)
gen trend1 = 0
local i = 1980
while `i' <= 1991 {
local j = `i' - 1979
replace trend1 = `j' if time == `i'
local i = `i' + 1}
gen trend2 = 0
local i = 1992
while `i' <= 2000 {
local j = `i' - 1991
replace trend2 = `j' if time == `i'
local i = `i' + 1}
gen period = 0
replace period = 1 if time >= 1992
gen adjrsq1 = 0
gen adjrsq2 = 0
local x = `min'
while `x' <= `max'{
reg crop trend1 if (stdist == `x' & period == 0)
replace adjrsq1 = e(r2_a) if (stdist == `x' & period == 0)
reg crop trend2 if (stdist == `x' & period == 1)
replace adjrsq2 = e(r2_a) if (stdist == `x' & period == 1)
local x = `x' + 1}
macro drop _min _max _i _j _x
gen cov1 = sqrt(covarr1*(1 - adjrsq1*adjrsq1))
gen cov2 = sqrt(covarr2*(1 - adjrsq2*adjrsq2))

and -trial2.do- was

use "C:\WINDOWS\DESKTOP\temp.dta"
sort stcode distcode time
sum stdist
local min = r(min)
local max = r(max)
gen trend1 = 0
local i = 1980
while `i' <= 1991 {
local j = `i' - 1979
replace trend1 = `j' if time == `i'
local i = `i' + 1}
gen trend2 = 0
local i = 1992
while `i' <= 2000 {
local j = `i' - 1991
replace trend2 = `j' if time == `i'
local i = `i' + 1}
gen period = 0
replace period = 1 if time >= 1992
gen adjrsq1 = 0
gen adjrsq2 = 0

for NUM in num `min'/`max', nos : reg crop trend1 if (stdist == NUM & period
== 0) \replace adjrsq1 = e(r2_a) if (stdist == NUM & period == 0) \reg crop
trend2 if (stdist == NUM & period == 1) \replace adjrsq2 = e(r2_a) if
(stdist == NUM & period == 1)

macro drop _min _max _i _j
gen cov1 = sqrt(covarr1*(1 - adjrsq1*adjrsq1))
gen cov2 = sqrt(covarr2*(1 - adjrsq2*adjrsq2))

One approach to the troublesome loop is to learn about -capture-.

local x = `min'
while `x' <= `max'{
	reg crop trend1 if (stdist == `x' & period == 0)
	replace adjrsq1 = e(r2_a) if (stdist == `x' & period == 0)
	reg crop trend2 if (stdist == `x' & period == 1)
	replace adjrsq2 = e(r2_a) if (stdist == `x' & period == 1)
	local x = `x' + 1
}

I guess that the problem arises if -stdist- is never equal to
a particular value of `x'. In that circumstance the regression
will crash, and the -replace- statement following must not
be attempted:

local x = `min'
while `x' <= `max'{
	capture reg crop trend1 if (stdist == `x' & period == 0)
	if _rc == 0 {
		replace adjrsq1 = e(r2_a) if (stdist == `x' & period == 0)
	}
	capture reg crop trend2 if (stdist == `x' & period == 1)
	if _rc == 0 {
		replace adjrsq2 = e(r2_a) if (stdist == `x' & period == 1)
	}
	local x = `x' + 1
}

A secondary point is that in Stata 7, -forvalues- is nicer
for this construct:

forval x = `min'/`max'{
	capture reg crop trend1 if (stdist == `x' & period == 0)
	if _rc == 0 {
		replace adjrsq1 = e(r2_a) if (stdist == `x' & period == 0)
	}
	capture reg crop trend2 if (stdist == `x' & period == 1)
	if _rc == 0 {
		replace adjrsq2 = e(r2_a) if (stdist == `x' & period == 1)
	}
}

Another secondary point is that -if e(sample)- is useful, here
and elsewhere, if only as a matter of conciseness:

forval x = `min'/`max'{
	capture reg crop trend1 if (stdist == `x' & period == 0)
	if _rc == 0 {
		replace adjrsq1 = e(r2_a) if e(sample)
	}
	capture reg crop trend2 if (stdist == `x' & period == 1)
	if _rc == 0 {
		replace adjrsq2 = e(r2_a) if e(sample)
	}
}

I can't identify what is wrong with the numlist.

An FAQ in this territory -- cycling through all possible values --

http://www.stata.com/support/faqs/data/for.html

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