Announcement

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

  • Replacing using loops

    Hi All,

    Please can someone assist with the following; I'm very new to stata;

    Var1
    Bongo
    Kassena
    Bolgatanga
    Tolon
    Salvelgu
    Yendi
    Karaga

    This variable(Var1) is a string variables.In one step how can i replace Bongo=1, Kassena=2 Bolgatanga=3 Tolon =4 etc

    Thanks for your assistance

    Daniel


  • #2
    If these were in alphabetical order, it could be done with a single command. But in the circumstance, I think the shortest thing you can do is:

    Code:
    label define var1_label 1 "Bongo" 2 "Kassena" 3 "Bolgatanga" 4 "Tolton" 5 "Salvelgu" 6 "Yendi" 7 "Karaga"
    encode Var1, gen(numeric_Var1) label(var1_label)

    Comment


    • #3
      No loop needed

      Code:
      help encode
      
      label def mylabel 1 Bongo 2 Kassena 3 Bolgatanga 4 Tolon 66 "Cape Coast" 
      encode var1, gen(numvar) label(mylabel)

      Comment


      • #4
        If Var1 is sorted in your data in the order you would like the numeric variable, and you want a sequential numeric variable from 1 to _N, then you can use Nick Cox's -labmask- [SJ8-2]

        Code:
        net describe gr0034, from(http://www.stata-journal.com/software/sj8-2)]
        with the following

        Code:
        gen X=_n
        labmask X, values(Var1)
        
        desc X
        list Var1 X
        list Var1 X, nolab
        Stata/MP 14.1 (64-bit x86-64)
        Revision 19 May 2016
        Win 8.1

        Comment


        • #5
          Many thanks Nick Cox and Carole Wilson

          Comment

          Working...
          X