Announcement

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

  • Create new variable from existing keeping original labels

    Hello!

    I've got a problem: when creating a new variable (region) from several existing variables (one variable for each country) I need to keep the labels (for example, 4 is 'Madrid' and 5 is 'Barcelona').

    Given that 'Spain' is a variable taking value from 1 to 17 (one for each Spanish region, labelled 1-AndalucĂ­a, 2-Asturias, and so on...) and .i for the rest of the countries, and 'N' is another variable taking value from 1 to 99 (one for each region of the country N), how could I merge both variables in a new variable 'REGIONS' without losing the labels of each region, so that for example 1 is AndalucĂ­a for the country Spain and 1 is Region1 for the country N?

    I've tried:

    clonevar region = Spain if Spain != .i

    so that it clonates Spain with it's labels, but I don't know how to add the other country regions without losing their labels.

    Thanks a lot!
    Last edited by Sara Suarez-Fernandez; 25 Sep 2018, 01:24.

  • #2
    No data example (FAQ Advice #12), so I'll make up my own to show some technique.

    Code:
    clear 
    input frog toad 
    1  .      
    2  . 
    3  . 
    .  1
    .  2
    .  3
    end 
    label define frog 1 "green" 2 "black" 3 "blue"
    label val frog frog 
    label define toad 1 "PINK" 2 "PURPLE" 3 "MAGENTA" 
    label val toad toad 
    
    decode frog, gen(frog_s) 
    decode toad, gen(toad_s) 
    
    gen both_s = cond(!missing(frog), frog_s, toad_s) 
    encode both_s, gen(both) 
    
    list, sep(3) 
    
         +--------------------------------------------------------+
         |  frog      toad   frog_s    toad_s    both_s      both |
         |--------------------------------------------------------|
      1. | green         .    green               green     green |
      2. | black         .    black               black     black |
      3. |  blue         .     blue                blue      blue |
         |--------------------------------------------------------|
      4. |     .      PINK               PINK      PINK      PINK |
      5. |     .    PURPLE             PURPLE    PURPLE    PURPLE |
      6. |     .   MAGENTA            MAGENTA   MAGENTA   MAGENTA |
         +--------------------------------------------------------+

    Comment

    Working...
    X