Statalist


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

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


From   David Kantor <kantor.d@att.net>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: How to change value based on string variable?
Date   Wed, 22 Apr 2009 12:26:46 -0400

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/



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