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

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


From   Sanmitra Ghosh <sanmitraz@yahoo.co.uk>
To   statalist@hsphsun2.harvard.edu
Subject   st: Problem with "while" and "for" loops.
Date   Mon, 24 Jun 2002 12:26:55 +0100 (BST)

Dear all,
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.
Sincerely,
Sanmitra Ghosh

__________________________________________________
Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.com
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))
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))



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index