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

st: using scalars in a loop


From   "Dimitriy V. Masterov" <[email protected]>
To   [email protected]
Subject   st: using scalars in a loop
Date   Tue, 11 Nov 2003 18:26:32 -0600 (CST)

Dear Statalist,

I am curious why I cannot use a scalar in a forvalues loop. I was trying
to write a loop that will create a variable ind that equals 1 if the var1
contains the letter "r", 0 otherwise. I suspect it has something to do
with the frontslash in the second example, but I have not found a way to
get around it. Some sentences about the backslash in [U] 21.3.9 hint that
this might be the problem, though I don't know for sure.

The code below works fine to do what I want:
**********************************************************
local n=_N
gen ind=0
forvalues v=1/`n' {
        replace ind=index(var1[`v'],"r") in `v'
}
replace ind=1 if (ind>1 & ind !=.)
***********************************************************


while this code does not work:
***********************************************
scalar n=_N
gen ind=0
forvalues v=1/n {
        replace ind=index(var1[`v'],"r") in `v'
}
replace ind=1 if (ind>1 & ind !=.)
***********************************************

And this code works fine, though it's a different kind of loop.
***********************************
scalar n=_N

local i=1
	while `i'<n {
		di "`i'"
	local i=`i'+1
}
***********************************

Thank you in advance for your help.


Dimitriy V. Masterov

_______________________________________________________________________________
Dimitriy V. Masterov

Work:
Center for Social Program Evaluation
1155 East 60th St. Room 038
Chicago, IL 60637
Work: (773)256-6005
Fax: (773)256-6313

Home:
1312 East 53rd St., Apt.309
Chicago, IL 60615
Mobile: (773)220-2760


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