Statalist


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

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


From   "Martin Weiss" <martin.weiss1@gmx.de>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: AW: How to change value based on string variable?
Date   Wed, 22 Apr 2009 18:08:43 +0200

<> 

Put the string in quotation marks

*************
clear*

input  str10  country 
Argentina
Aruba    
Australia
Austria  
Bahrain  
end

compress
gen revenue=90

replace revenue=100 if country=="Australia"
list, noobs
*************



HTH
Martin


-----Ursprüngliche Nachricht-----
Von: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von Mike Kim
Gesendet: Mittwoch, 22. April 2009 18:05
An: statalist@hsphsun2.harvard.edu
Betreff: st: How to change value based on string variable?

Hi all,

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?

Thank you in advance.
Mike. 

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index