Statalist


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

AW: st: How to change value based on string variable?


From   "Martin Weiss" <[email protected]>
To   <[email protected]>
Subject   AW: st: How to change value based on string variable?
Date   Wed, 22 Apr 2009 18:34:26 +0200

<> 

"(I believe that Martin replied with this same advice, but omitted the 
-:country- part.)"


No, I did not, although it is a good idea explored here:

http://www.stata-journal.com/sjpdf.html?articlenum=dm0009


I never -encode-d in the first place, but put the string itself into
quotation marks...


HTH
Martin


-----Ursprüngliche Nachricht-----
Von: [email protected]
[mailto:[email protected]] Im Auftrag von David Kantor
Gesendet: Mittwoch, 22. April 2009 18:27
An: [email protected]
Betreff: Re: st: How to change value based on string variable?

At 12:04 PM 4/22/2009, Mike wrote:
>My data include 50 countries. Sometimes I need to change some values in
>another variable based on country name. If I leave country names as string,
>the following does not work, for example.
>
>replace revenue=100 if country==Australia
>
>So, I usually change country names into numeric using "encode."
>
>  label list country
>            1 Argentina
>            2 Aruba
>            3 Australia
>            4 Austria
>            5 Bahrain
>.......
>
>Then, I use the following code:
>
>replace revenue=100 if country==3
>
>However, if I drop or add some contries later Australia is no longer 3 and
I
>have to go through my do file to correct every possible mistake which is
>frustrating. Is there any better way to do this job so that I can reduce
>errors in data management?

Two principles apply here.
First, it is often better to explicitly define the label, rather than 
to have encode create it.  Or you can have encode create it the first 
time, and then write out the definition to a do file, using
label save

I would do that in a process whose only role is to create that label
do-file.

Subsequently, you can run that do-file, and then -encode- your 
variable using that label in the -label()- option; I usually use the 
-noextend- option as well -- to assure that only labels I explicitly 
created are used.

Secondly, whenever I refer to an explicit value, I use the 
"string":labelname form:
replace revenue=100 if country== "Australia":country

It's a bit verbose, but prevents the problem that you mentioned. In 
this form, Stata looks up "Australia" in the label country, finds the 
numeric value and substitutes it. Thus,
"Australia":country
gets converted to whatever the correct numeric value is -- 3 in the 
present scheme, but it could be different if things change.

(I believe that Martin replied with this same advice, but omitted the 
-:country- part.)

HTH
--David

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


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



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