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

From |
"Nick Cox" <n.j.cox@durham.ac.uk> |

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

Subject |
st: RE: re: creating a variable from many variables |

Date |
Tue, 6 Oct 2009 19:26:36 +0100 |

Thanks for the various plugs. A more elegant method than is implemented in -egen, rall()- is gen byte four = 1 foreach v of var var* { replace four = 0 if !(missing(`v') | `v' == 4) } An alternative for the middle line is replace four = 0 if !inlist(`v', 4, .) However, note the implication that any observation on which all values are missing is also tagged 1. Rajaram might want to exclude any such observations. It's not clear from the original post as there is no such example. Nick n.j.cox@durham.ac.uk Martin Weiss ============ Also note Nick`s recent column http://www.stata-journal.com/article.html?article=pr0046 Kit Baum ======== ssc install egenmore (Nick Cox's grab bag of egen fns) . egen four = rall(var*), c(@ == 4 | mi(@)) . l +------------------------------------------------+ | var1 var2 var3 var4 var5 var6 four | |------------------------------------------------| 1. | 4 . 4 4 4 . 1 | 2. | 2 4 . . . . 0 | 3. | 4 4 4 4 4 4 1 | 4. | 2 3 4 4 4 4 0 | 5. | 4 4 4 4 4 4 1 | |------------------------------------------------| 6. | 4 . . . . . 1 | 7. | 3 3 3 3 3 3 0 | 8. | 4 3 . . . . 0 | 9. | 2 3 1 4 3 . 0 | 10. | 1 1 . . . . 0 | +------------------------------------------------+ Rajaram Subramanian Potty ========================= I have about 6 variables and I want to create a variable based on all these variables. If the all the variables take a particular value, I want to generate the variable as 1 otherwise 0. The problem is that for some of the cases some variables will not be applicable. The following is an example of the data for 10 cases. In the follwing example if the value of all the applicable variables is 4 then a created variable is 1 otherwise it is 0 as shown below. var1 var2 var3 var4 var5 var6 createdvar 4 . 4 4 4 . 1 2 4 . . . . 0 4 4 4 4 4 4 1 2 3 4 4 4 4 0 4 4 4 4 4 4 1 4 . . . . . 1 3 3 3 3 3 3 0 4 3 . . . . 0 2 3 1 4 3 . 0 1 1 . . . . 0 * * 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**:**st: re: creating a variable from many variables***From:*Kit Baum <baum@bc.edu>

- Prev by Date:
**st: R: a person-period multi-episode event history file** - Next by Date:
**Re: st: RE: nested forvalues loop** - Previous by thread:
**Re: st: re: creating a variable from many variables** - Next by thread:
**st: Recall: accessing results of margins/adjust** - Index(es):

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