Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Destring and create label value

    Hi,

    I have a string variable "city" of cities (London, Paris, Madrid). I want to destring it and create a code like that : London=1 ; Paris=2; Madrid=3 but i want to keep labels
    Can we do that with 1 line of "destring"?
    I know i can just do that :

    replace city="1" if city=="London"
    replace city="2" if city=="Paris"
    replace city="3" if city=="Madrid"
    destring city, replace

    label define City 1 "London" 2 "Paris" 3 "Madrid"
    label values city City

    but is there not an easier and faster method?

    Thanks !

  • #2
    Code:
    label define Cities 1 London 2 Paris 3 Madrid
    encode city, generate(cid) label(Cities) noextend
    Code:
    help encode

    Comment


    • #3
      Thanks !

      Comment


      • #4
        I am not 100% sure, but I have some suspicion that what you guys are doing might potentially result in a mess. (The other way of achieving this task, apart from -encode-, is -egen, group, label-)

        If you just type

        Code:
        . sysuse auto, clear
        (1978 Automobile Data)
        
        . keep make
        
        . encode make, gen(mk1)
        
        . egen mk2 = group(make), label
        
        . list in 1/7, sep(0)
        
             +-----------------------------------------------+
             | make                      mk1             mk2 |
             |-----------------------------------------------|
          1. | AMC Concord       AMC Concord     AMC Concord |
          2. | AMC Pacer           AMC Pacer       AMC Pacer |
          3. | AMC Spirit         AMC Spirit      AMC Spirit |
          4. | Buick Century   Buick Century   Buick Century |
          5. | Buick Electra   Buick Electra   Buick Electra |
          6. | Buick LeSabre   Buick LeSabre   Buick LeSabre |
          7. | Buick Opel         Buick Opel      Buick Opel |
             +-----------------------------------------------+
        Stata will allocate the labels appropriately.

        If you manually define the label like you are doing, is there not a chance that you mislabel the cities?

        I mean when one does specify encode with label like Joseph did, does Stata know that she needs to encode London with the code of 1? Or she will encode them anyways she likes, and then apply the label ex post most likely resulting in mis-labelled cities?

        Comment


        • #5
          Also see labmask (part of labutil) and elabel, all from SSC.

          Code:
          . sysuse auto, clear
          (1978 Automobile Data)
          
          . keep make
          
          . *ssc install elabel
          . elabel define makelbl = levels(make)
          
          . encode make , generate(mk3) label(makelbl)
          
          . list in 1/7 , sep(0)
          
               +-------------------------------+
               | make                      mk3 |
               |-------------------------------|
            1. | AMC Concord       AMC Concord |
            2. | AMC Pacer           AMC Pacer |
            3. | AMC Spirit         AMC Spirit |
            4. | Buick Century   Buick Century |
            5. | Buick Electra   Buick Electra |
            6. | Buick LeSabre   Buick LeSabre |
            7. | Buick Opel         Buick Opel |
               +-------------------------------+
          Last edited by daniel klein; 12 Oct 2020, 08:06.

          Comment

          Working...
          X