Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | "Joseph Coveney" <jcoveney@bigplanet.com> |
To | <statalist@hsphsun2.harvard.edu> |
Subject | st: Re: recoding question |
Date | Tue, 25 Dec 2012 02:12:39 +0900 |
Peter Lachenbruch wrote: I have a string variable with codes Markedly Improved Markedly Worse Moderately Improved Moderately Worse Slightly Improved Slightly Worse Unchanged I want to convert these to a numeric variable, but ordered in terms of improvement. I was looking for a way to do this easily in one step, but didn't find a simple command like encode or recode. I ended up using encode to get the variables numeric and then recoding them. Is there a simple way to do this? For seven categories it's no big deal, but if I had 20 categories and needed to do this repeatedly, it would be a pain. Another option would be to prefix a numeral for the codes but that seems clumsy... -------------------------------------------------------------------------------- How about something like the ado-file below? I've got only the last release of Stata (11.2) on the laptop in front of me at the moment, but I believe that it should work for Stata 12.1, as well. Put the two directions or senses ("Worse" and "Improved" in your case) in ascending order in the option for directions, and do likewise with the ordered degrees or magnitudes ("Slightly", "Moderately" and "Markedly" in your case) in the option for degrees, paying attention to case. If you have a midpoint, put that in the option for zero point. Take care with it, because there's no user-input validation; it's pretty lean, but you can embellish it as you like. Joseph Coveney program define engrade version 11.2 syntax varlist(string), DIrections(string) DEgrees(string) /// Label(string) [Zero(string)] local sign "-" foreach direction of local directions { local index 0 foreach degree of local degrees { local ++index label define `label' `sign'`index' "`degree' `direction'", add } local sign "+" } if "`zero'" != "" label define `label' 0 "`zero'", add label define `label' .m Missing, add tempvar tmpvar0 quietly foreach var of varlist `varlist' { encode `var', generate(`tmpvar0') label(`label') noextend replace `tmpvar0' = .m if mi(`var') replace `var' = "" destring `var', replace replace `var' = `tmpvar0' label values `var' `label' drop `tmpvar0' } end --- Preliminary Test --- . input str25 prescores prescores 1. "Markedly Improved" 2. "Markedly Worse" 3. "Moderately Improved" 4. "Moderately Worse" 5. "Slightly Improved" 6. "Slightly Worse" 7. "Unchanged" 8. "" 9. end . . clonevar postscores = prescores (1 missing value generated) . . engrade *scores, di(Worse Improved) de(Slightly Moderately Markedly) /// > l(Improvements) z(Unchanged) . . sort prescores . list, noobs separator(0) abbreviate(20) +-------------------------------------------+ | prescores postscores | |-------------------------------------------| | Markedly Worse Markedly Worse | | Moderately Worse Moderately Worse | | Slightly Worse Slightly Worse | | Unchanged Unchanged | | Slightly Improved Slightly Improved | | Moderately Improved Moderately Improved | | Markedly Improved Markedly Improved | | Missing Missing | +-------------------------------------------+ . label list Improvements: -3 Markedly Worse -2 Moderately Worse -1 Slightly Worse 0 Unchanged 1 Slightly Improved 2 Moderately Improved 3 Markedly Improved .m Missing . . exit end of do-file * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/faqs/resources/statalist-faq/ * http://www.ats.ucla.edu/stat/stata/