Announcement

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

  • Adding value label automatically

    Hello,

    I would like to put value label automatically on my database.
    I have two same csv files, one with label and one without

    If i import them into Stata i have those one :

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(id x1 x2 x3)
     1 1 2 10
     2 1 4  2
     3 1 3  4
     4 2 2  5
     5 2 1  2
     6 1 2  0
     7 1 2 10
     8 2 3  2
     9 2 4  3
    10 2 2  2
    end
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float id str1 x1 str2 x2 float x3
     1 "A" "US" 10
     2 "A" "FR"  2
     3 "A" "UK"  4
     4 "B" "US"  5
     5 "B" "ES"  2
     6 "A" "US"  0
     7 "A" "US" 10
     8 "B" "UK"  2
     9 "B" "FR"  3
    10 "B" "US"  2
    end


    I would like to get this from the two above :

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(id x1 x2 x3)
     1 1 2 10
     2 1 4  2
     3 1 3  4
     4 2 2  5
     5 2 1  2
     6 1 2  0
     7 1 2 10
     8 2 3  2
     9 2 4  3
    10 2 2  2
    end
    label values x1 x1
    label def x1 1 "A", modify
    label def x1 2 "B", modify
    label values x2 x2
    label def x2 1 "ES", modify
    label def x2 2 "US", modify
    label def x2 3 "UK", modify
    label def x2 4 "FR", modify

    I don't know if we can do it,

    Thanks,

  • #2
    merge first, then apply labmask from the Stata Journal.

    Comment


    • #3
      Code:
      clear all
      frames reset
      
      frame rename default values
      input float(id x1 x2 x3)
       1 1 2 10
       2 1 4  2
       3 1 3  4
       4 2 2  5
       5 2 1  2
       6 1 2  0
       7 1 2 10
       8 2 3  2
       9 2 4  3
      10 2 2  2
      end
      
      frame create labels
      frame change labels
      input float id str1 x1 str2 x2 float x3
       1 "A" "US" 10
       2 "A" "FR"  2
       3 "A" "UK"  4
       4 "B" "US"  5
       5 "B" "ES"  2
       6 "A" "US"  0
       7 "A" "US" 10
       8 "B" "UK"  2
       9 "B" "FR"  3
      10 "B" "US"  2
      end
      
      // find the labels
      ds , has(type string)
      local labs = r(varlist)
      keep id `labs'
      
      // give the variables with labels a different name
      local labvars = ""
      foreach var of varlist `labs' {
          rename `var' `var'_lb
          local labvars = "`labvars' `var'_lb"
      }
      
      // merge the two datasets
      frame change values
      frlink 1:1 id, frame(labels)
      frget `labvars' , from(labels)
      
      // attach the labels to the values
      // requires labmask, type -search labmask- in Stata to find it.
      foreach var of varlist `labs' {
          labmask `var', values(`var'_lb)
      }
      ---------------------------------
      Maarten L. Buis
      University of Konstanz
      Department of history and sociology
      box 40
      78457 Konstanz
      Germany
      http://www.maartenbuis.nl
      ---------------------------------

      Comment


      • #4
        Thank you both, it works perfectly

        Comment

        Working...
        X