[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: efficiently creating 1/0 variables from existing string y/n variables |

Date |
Mon, 28 Sep 2009 11:14:22 +0100 |

In addition, -multencode- from SSC offers a way to -encode- several string variables at once, with the key twist that the same set of value labels is used throughout. Nick n.j.cox@durham.ac.uk Martin Weiss -replace-ing these variables with numeric counterparts would require you to change the -type- from string to numeric as well. I think the best way to do this for many variables is to employ a loop over them, and have the old ones -drop-ped automatically... ************** clear* input str1 alc_now y y y y y y n y end clonevar alc_now1=alc_now clonevar alc_now2=alc_now clonevar alc_now3=alc_now qui ds, has(type string) foreach var in `r(varlist)'{ encode `var', gen(`var'_new) drop `var' } list, noobs ************** Michael McCulloch I have a long list of string variables which contain only "y", "n", or are missing. I'm converting them to new numeric variables with values 1 if the old variable is "y", and 0 if "n". For example: gen etoh_now=alc_now=="y" This gives, correctly: .. list alc_now etoh_now, clean alc_now etoh_now 1. y 1 2. y 1 3. y 1 4. y 1 5. y 1 6. y 1 7. n 0 8. y 1 Is there a more efficient way to do this, where I can change "y" to 1 and "n" to 0 without creating a new variable and manually dropping the old variable? * * 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: efficiently creating 1/0 variables from existing string y/n variables***From:*Michael McCulloch <mcculloch@pinestreetfoundation.org>

**st: RE: efficiently creating 1/0 variables from existing string y/n variables***From:*"Martin Weiss" <martin.weiss1@gmx.de>

- Prev by Date:
**Re: st: logic for changing values for grouped variables** - Next by Date:
**RE: st: logic for changing values for grouped variables** - Previous by thread:
**st: RE: efficiently creating 1/0 variables from existing string y/n variables** - Next by thread:
**st: RE: RE: efficiently creating 1/0 variables from existing string y/n variables** - Index(es):

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