Statalist


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

RE: st: RE: gen newvar satisfying two conditions


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: RE: gen newvar satisfying two conditions
Date   Thu, 18 Dec 2008 18:57:47 -0000

Not quite. The original request from bw to me implies 

gen hadsx = cond(plannedsx == "yes" | emergsx == "yes", "yes", "no") 

if you want to keep things all string. Note that _either_ planned _or_
emergency surgery implies | (or), not & (and). 

Martin's implication that you should want numeric variables with value
labels will be correct sooner or later. For that, 

1abel def yesno 1 "yes" 0 "no" 
encode plannedsx, gen(n_plannedsx) label(yesno) 
encode emergsx, gen(n_emergsx) label(yesno) 

is one way to proceed.

There are many hybrid approaches. For example 

gen byte hadsx = (plannedsx == "yes") | (emergsx == "yes") 

gen byte hadsx = max((plannedsx == "yes"), (emergsx == "yes")) 

both have numeric result. 

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

b. water

martin, thanks, your one liner works.
 
martin.weiss

> For the first step, try -encode-. The third one sounds like -recode-
to
> me...
>
> Of course you could also just
>
> gen byte hadsx= plannedsx==1 & emergsx==1
>
> after -encode-ing...

b. water

> stata 8.2, windows xp pro,
>
> i have two variables: plannedsx (for previous planned surgery) and
emergsx
> (for previous emergency surgery), each is answered either yes or no
> (strings). i want to generate a new variable hadsx (i.e. has had
surgery
> previously whether as a cold procedure or an emergent one, also a yes
or
> no). an approach i could use (but haven't tried yet - but i think is
do-ably
> with minimal pain):
>
> first: generate and replace yes or no for each variable with 0 (for
no) and
> 1 (for yes)
> second: generate a temporary variable tempvar consisting the sum of
those
> two variables, which therefore would only contain only values from 0,
1 or
> 2.
> third: generate and replace 0 with a "no" and anything>0 with a "yes"
>
> i am wondering whether there is a more direct method to achieve what i
> wanted. would appreciate advice/help in this data management matter.

*
*   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   |   What's new   |   Site index