Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: programming: identifying a discrete versus a continuous variable


From   "Austin Nichols" <[email protected]>
To   [email protected]
Subject   Re: st: programming: identifying a discrete versus a continuous variable
Date   Sun, 17 Dec 2006 05:32:56 -0500

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/



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