Statalist


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

RE: st: Gen & replace commands


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: Gen & replace commands
Date   Fri, 26 Oct 2007 18:17:36 +0100

Maarten's last alternative is not equivalent to Ziad's code. 

In 

gen drugcat = cond(inlist(drug1, 1, 2, 3), drug1, ///
              cond(inlist(drug2, 1, 2, 3), drug2, 0))

the values of -drug1- take precedence. In Ziad's code 
it is the other way round. 

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

Maarten buis

--- Ziad El-Khatib <Ziad.El-Khatib@ki.se> wrote:
> Patient was using either "drug1" and "drug2".
> I am trying to create new category to put the value of adherence to
> the two drugs in one column (drugcat). Drug1 and drug2 have values of
> 1, 2, 3 or empty.
> 
> i tried
> 
> 
> gen drugcat=0
> replace drugcat=drug1 if drug1=1 | drug1=2 | drug1=3
> replace drugcat=drug2 if drug2=1 | drug2=2 | drug2=3
> 

In logical statements you need to use double equal signs when you mean
equal to: 
gen drugcat=0
replace drugcat=drug1 if drug1==1 | drug1==2 | drug1==3
replace drugcat=drug2 if drug2==1 | drug2==2 | drug2==3

An alternative is:
gen drugcat=0
replace drugcat=drug1 if inlist(drug1, 1, 2, 3)
replace drugcat=drug2 if inlist(drug1, 1, 2, 3)

Or:
gen drugcat = cond(inlist(drug1, 1, 2, 3), drug1, ///
              cond(inlist(drug2, 1, 2, 3), drug2, 0))

For more see: -help if- -help inlist- and -help cond-


*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index