Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Steve Nakoneshny <scnakone@ucalgary.ca> |
To | "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu> |
Subject | Re: st: Encoding string variables: the one var that has no variance encodes differently than the rest |
Date | Tue, 7 May 2013 17:04:44 -0600 |
David, Stata encodes string vars in alphabetical order. If your vars encode "TRUE" as a 2, I would assume that "FALSE" would be a "1" in your encoded vars. Each variable is treated independently of the others; it encodes based on what is present, not what the contents of the string are and how it encoded the previous var. Knowing this limitation, you can do a quick fix of your non-variant var with something like -recode var1_encoded (2=1)- There are certainly other solutions, but this is internally consistent and fast (but ugly) Steve On 2013-05-07, at 4:50 PM, David de Jong wrote: > Hi all, > > New Stata (12.1) user here, and I'm a first time poster. I hope > someone might be able to help with something. I've searched the web, > help files, and archives to no avail. > > I tried to encode a series of variables (type str5), with the following: > > qui foreach v of var var1-var10 { > encode `v', generate(`v'_encoded) > } > > All of the variables seemed to convert as they should (to type long), > except for the only variable that also happened to have no variance: > all cases in the original variable had the string "TRUE." Instead of > converting to a "2" as did all instances of "TRUE" in the other > variables, it was converted to "1." > > The same problem appears when I try it like this: > > encode var1, generate(var1_encoded) > > I'd be very grateful for any insights into what might be going on. > > D. de Jong > > ~ > Department of Clinical and Social Sciences in Psychology > University of Rochester > 498c Meliora Hall > Rochester, NY 14627 > * > * 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/ * * 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/