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

From |
"Martin Weiss" <martin.weiss1@gmx.de> |

To |
<statalist@hsphsun2.harvard.edu> |

Subject |
Re: st: Create a new variable based on some other variables |

Date |
Thu, 21 May 2009 13:10:35 +0200 |

<>

*** clear* set obs 50 local a 1 local b 8 set seed 12345 forv i=1/3{ gen A`i'=`a'+/* */int((`b'-`a'+1)*runiform()) } compress list, noobs //Martin gen B=(A1==1)*1 replace B=(A2==3)*2 replace B=(A3==8)*3 gen Balt=cond(A1 == 1, 1, /* */cond(A2 == 3, 2, /* */cond(A3 == 8, 3, 0)/* */)) //Martin gen Balt2=(A1==1)*1 + (A2==3)*2/* */+(A3==8)*3 //Eric`s recommendation gen Balt3=1 if A1==1 replace Balt3=2 if A2==3 replace Balt3=3 if A3==8 list, noo h(25) *** HTH Martin _______________________

To: <statalist@hsphsun2.harvard.edu> Sent: Thursday, May 21, 2009 12:39 PM Subject: RE: st: Create a new variable based on some other variables

Let's spell out that there is a difference between gen B = cond(A1 == 1, 1, cond(A2 == 3, 2, cond(A3 == 8, 3, 0))) and gen B=(A1==1)*1 + (A2==3)*2+(A3==8)*3 The order in -cond()- is crucial. Once for example observations with A1 == 1 are dealt with, they are not revisited, regardless of whether A2 == 3 or A3 == 8 in the same observation. Nick n.j.cox@durham.ac.uk

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

**References**:**RE: st: Create a new variable based on some other variables***From:*"Nick Cox" <n.j.cox@durham.ac.uk>

**Re: st: Create a new variable based on some other variables***From:*"Martin Weiss" <martin.weiss1@gmx.de>

**RE: st: Create a new variable based on some other variables***From:*"Nick Cox" <n.j.cox@durham.ac.uk>

- Prev by Date:
**RE: st: Create a new variable based on some other variables** - Next by Date:
**RE: st: RE: insheet numeric variables as strings** - Previous by thread:
**RE: st: Create a new variable based on some other variables** - Next by thread:
**Re: st: Create a new variable based on some other variables** - Index(es):

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