Statalist The Stata Listserver


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

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


From   "Newson, Roger B" <[email protected]>
To   <[email protected]>
Subject   st: RE: programming: identifying a discrete versus a continuous variable
Date   Sun, 17 Dec 2006 14:48:14 -0000

The problem here is that it all depends what you mean by "discrete". At
the machine level, all variables stored by a digital computer have a
finite number of possible values, even if that number is very large. I
usually tell first year medical students that, in practice, a discrete
variable is one for which it is useful to know the frequency, in a
sample, of each possible value. It is not easy for a computer (or even a
human being) to test for that condition.

I suspect that the way forward, in your case, is to give the variables a
characteristic at the point of being created, with a name such as
discon. In your program to create the dataset, you might have the lines

char gender[discon] discrete
char noselength[discon] continuous
char age[discon] continuous
char agegroup[discon] discrete

Then, in the program to do the analysis, you might have the lines

global vars1 "gender noselength age agegroup"
foreach x of varlist $vars1{
if("``x'[discon]'" == "discrete"){
*do these lines of code
}
else{
*do these lines of code
}
}

To find out more about variable and dataset chatacteristics in Stata,
type

whelp char

I hope this helps.

Best wishes

Roger


Roger Newson
Lecturer in Medical Statistics
Respiratory Epidemiology and Public Health Group
National Heart and Lung Institute
Imperial College London
Royal Brompton campus
Room 33, Emmanuel Kaye Building
1B Manresa Road
London SW3 6LR
UNITED KINGDOM
Tel: +44 (0)20 7352 8121 ext 3381
Fax: +44 (0)20 7351 8322
Email: [email protected] 
www.imperial.ac.uk/nhli/r.newson/

Opinions expressed are those of the author, not of the institution.

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Brent Fulton
Sent: 17 December 2006 01:16
To: [email protected]
Subject: st: programming: identifying a discrete versus a continuous
variable

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/

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