Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Changing labels in a string variable


From   Svend Juul <SJ@SOCI.AU.DK>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Changing labels in a string variable
Date   Sat, 10 May 2008 14:30:39 +0200

Tomasz wrote:
 
I have a beginner's problem. I am using Stata/IC 10.0 for Windows. In
my data set I have a string (str3) type variable COUNTRY (see below).
It does not have missing cases. In this variable I wanted to recode
numeric country codes (Country ID) into country names (e.g. '040' is
being replaced by 'Austria' etc.). I used two commands:
 
label define COUNTRY_label 040 "AUSTRIA" 056 "BELGIUM" 203 "CZECH
REPUBLIC" 208 "DENMARK" 246 "FINLAND" 250  "FRANCE" 276 "GERMANY" 300
"GREECE" 348 "HUNGARY" 352 "ICELAND" 372 "IRELAND" 380 "ITALY" 428
"LATVIA" 438  "LIECHTENSTEIN" 442 "LUXEMBOURG" 528 "NETHERLAND" 578
"NORWAY" 616 "POLAND" 620 "PORTUGAL" 703 "SLOVAKIA" 724  "SPAIN" 752
"SWEDEN" 756 "SWITZERLAND" 826 "UNITED KINGDOM"
 
label values COUNTRY COUNTRY_label
 
but STATa's response to the second command was:
 
may not label strings
r(181);
 
How can I recode labels in the string variable? Or where I can look
for information how to do that?
 
======================================================================
 
You must generate a numeric country code first, then attach labels:
 
      clear
      input str3 country
      040
      056
      203
      end
 
      generate ncountry=real(country)
      label define country_label 40 "AUSTRIA" 56 "BELGIUM" ///
         203 "CZECH REPUBLIC"
      label values ncountry country_label
 
      list
           +--------------------------+
           | country         ncountry |
           |--------------------------|
        1. |     040          AUSTRIA |
        2. |     056          BELGIUM |
        3. |     203   CZECH REPUBLIC |
           +--------------------------+
 
      list , nolabel
           +--------------------+
           | country   ncountry |
           |--------------------|
        1. |     040         40 |
        2. |     056         56 |
        3. |     203        203 |
           +--------------------+
 
You might want to display leading zeros in the numeric variable.
If you had no value labels attached, you could do it by:
 
     format ncountry %03.0f
 
but the attachment of labels overrides that format.
 
Hope this helps
Svend

________________________________________________________ 
 
Svend Juul
Institut for Folkesundhed, Afdeling for Epidemiologi
(Institute of Public Health, Department of Epidemiology)
Vennelyst Boulevard 6 
DK-8000 Aarhus C,  Denmark 
Phone, work:   +45 8942 6090 
Phone, home:   +45 8693 7796 
Fax:           +45 8613 1580 
E-mail:        sj@soci.au.dk 
_________________________________________________________ 


*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   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