Brent--
Just to operationalize Svend's solution for your case, you could
foreach x of varlist $vars1{
qui insp `x'
if r(N_unique)==2 {
*do these lines of code if x has 2 categ
}
else{
*do these lines of code if not
}
}
or
foreach x of varlist $vars1{
qui insp `x'
local cat=r(N_unique)
su `x', meanonly
if `cat'==2 & r(min)==0 & r(max)==1 {
*do these lines of code if x is indicator var
}
else{
*do these lines of code if not
}
}
On 12/17/06, Svend Juul <[email protected]> wrote:
From "Brent Fulton" <[email protected]>
To <[email protected]>
Subject st: programming: identifying a discrete versus a continuous variable
Date Sat, 16 Dec 2006 17:15:49 -0800
--------------------------------------------------------------------------------
Brent Fulton wrote:
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).
...
---------------------------------------------------------------------
I think you might benefit from -inspect-:
. sysuse auto
. quietly inspect weight
. return list
...
r(N_unique) = 64
...
You could then use the number of unique values to determine whether you
would consider the variable discrete or "continuous" (no variables are
truly continuous, but anyway).
*
* 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/