I am using Stata 9.2 and am running a loop on multiple variables; some are
continuous and some are discrete (all made binary). The hypothetical loop
follows, and I'd like to know how to write a flexible "if" statement
(flexible in the sense that $vars1 may contain 20 variables that I'd like to
be able to change without rewriting the if statement).
foreach x of varlist $vars1{
if(`x' is discrete){
*do these lines of code
}
else{
*do these lines of code
}
}
The following solutions aren't that flexible. For example, I could write the
following if I knew variables 3 and 20 in $vars were discrete.
scalar variable_number=1
foreach x of varlist $vars1{
if(inlist(scalar(variable_number),3,20)){
*do these lines of code
}
else{
*do these lines of code
}
scalar variable_number= scalar(variable_number) + 1
}
I've also tried the following, but the if statement is always evaluated as
true:
if(inlist(`x', var3_name, var20_name)){
Lastly, I've tried to add characteristics to variables:
char var3_name[one] "discrete"
But I can't get the if statement to evaluate a characteristic.
I'd appreciate anyone's suggestions.
Thanks,
Brent
___________________________________
Brent D. Fulton, PhD
Health Services Researcher
Petris Center at UC Berkeley
Phone: 510-643-4102
Fax: 510-643-4281
Email: [email protected]
www.petris.org
*
* 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/