Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

Re: st: converting string to numerals


From   Sergiy Radyakin <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: converting string to numerals
Date   Wed, 20 Nov 2013 15:23:51 -0500

Hello, Ramesh,

generate byte opinion=.
replace opinion=-2 if opstr=="strongly disagree"
replace opinion=-1 if opstr=="disagree"
replace opinion=0 if opstr=="neither disagree nor agree"
replace opinion=1 if opstr=="agree"
replace opinion=2 if opstr=="strongly agree"
assert !missing(opinion) if !missing(opstr)

Note the utility of the assert command here, it will capture the typos
in strings, e.g. ("Agree" instead of "agree").

Best, Sergiy



On Wed, Nov 20, 2013 at 2:48 PM, Jonas Krüger
<[email protected]> wrote:
> Did you thought about regexr?  should be pretty easy to find patterns
> and replace them.  http://www.stata.com/help.cgi?regexm()
>
> On Wed, Nov 20, 2013 at 8:15 PM, Nick Cox <[email protected]> wrote:
>> -encode- does not understand English in the way that you do. My guess
>> is that it did not encode your values randomly at all, but as
>>
>> 1 agree
>> 2 disagree
>> 3 neither ...
>> 4 strongly agree
>> 5 strongly disagree
>>
>> which is an alphabetical order.
>>
>> What you need is covered by the syntax and documentation of -encode-,
>> namely to define your labels in advance:
>>
>> label define mylabels -2 "strongly disagree"  <and so forth>
>> encode var, gen(var1) label(mylabels)
>>
>> while at the same time using more evocative variable names.
>>
>> Nick
>> [email protected]
>>
>>
>> On 20 November 2013 19:07, ramesh <[email protected]> wrote:
>>
>>  I have a very basic question about coding string variables. My variable is
>>> expressed as strongly disagree, disagree, neither disagree nor agree, agree,
>>> and strongly agree. I want to code them -2 if strongly disagree, -1 if
>>> disagree, 0 if neither disagree nor agree, 1 if agree, and 2 if strongly
>>> agree. I used encode(var), gen(var1), but it coded the scales randomly. Is
>>> there any direct way to preserve the order in data coding?
>> *
>> *   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/

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


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index