Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: st: Assigning values from a list


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: Assigning values from a list
Date   Fri, 21 May 2010 12:23:51 +0100

Maarten's code can be tweaked further. 

As Austin Nichols emphasised testing decimals for equality is a recipe for difficulty if not disaster. Work with integers instead 

In addition the | operator can be used to streamline 

gen byte virus = (                        ///
                 inlist(var1, `list') +   ///
                 inlist(var2, `list') +   ///
                 inlist(var3, `list') +   ///
                 inlist(var4, `list')     ///
                 ) > 1                    ///
                 if !missing(var1, var2, var3, var4)

to

gen byte virus = inlist(var1, `list') |   ///
                 inlist(var2, `list') |   ///
                 inlist(var3, `list') |   ///
                 inlist(var4, `list')     ///
                 if !missing(var1, var2, var3, var4)

(I guess > 1 was a typo for > 0.) 

Nick 
n.j.cox@durham.ac.uk 

Maarten buis

--- On Fri, 21/5/10, marietherese.kelly@flinders.edu.au wrote:
> I have 4 variables that are basically DSM codes and I would
> like to search through those codes.
> 
> I would like to define lists of diseases and then pick
> which cases have those diseases
> 
> So instead of writing 
> 
> gen virus=(var1==53.20) |(var1==54.42) |(var1==54.43) | /*
> */ (var1==76.00) |(var1==76.90) 
> 
> replace virus=((var2==53.20) |(var2==54.42) |(var2==54.43) | /*
> */ (var2==76.00) |(var2==76.90)) if virus==.
> 
> replace virus=((var3==53.20) |(var3==54.42) |(var3==54.43) | /*
> */ (var3==76.00) |(var3==76.90)) if virus==.
> 
> replace virus=((var4==53.20) |(var4==54.42) |(var4==54.43) | /*
> */ (var4==76.00) |(var4==76.90)) if virus==.
> 
> 
> Repeat for several other categories of diseases - ends up
> with very long and confusing code.
> 
> 
> Is there a way of defining a list eg virus={53.20, 54.42,
> 54.43, 76.00, 76.90}
> 
> and checking to see whether var1-4 have values in the
> list?

You could do something like this:

local list "53.20, 54.42, 54.43, 76.00, 76.90"
gen byte virus = (                        ///
                 inlist(var1, `list') +   ///
                 inlist(var2, `list') +   ///
                 inlist(var3, `list') +   ///
                 inlist(var4, `list')     ///
                 ) > 1                    ///
                 if !missing(var1, var2, var3, var4)


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index