Announcement

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

  • Label variables from a list of names

    Hi all,

    I am trying to label variables from a local list of names. Lamentably to me, I only could label all the variables with the last name of the list, example:


    Code:
    clear all
    set more off
    sysuse auto
    
    foreach v of varlist * {
       label var `v' ""
    }
    
    local names uno dos tres cuatro cinco seis siete ocho nueve diez once doce
    
    forvalues i=1(1)12 {
    foreach var of varlist * {
        local name : word `i' of `names'
        label variable `var' "`name'"
    }
    }
    
    describe
    
    
    Contains data from C:\Program Files (x86)\Stata13\ado\base/a/auto.dta
      obs:            74                          1978 Automobile Data
     vars:            12                          13 Apr 2013 17:45
     size:         3,182                          (_dta has notes)
    ----------------------------------------------------------------------------------------------------------------
                  storage   display    value
    variable name   type    format     label      variable label
    ----------------------------------------------------------------------------------------------------------------
    make            str18   %-18s                 doce
    price           int     %8.0gc                doce
    mpg             int     %8.0g                 doce
    rep78           int     %8.0g                 doce
    headroom        float   %6.1f                 doce
    trunk           int     %8.0g                 doce
    weight          int     %8.0gc                doce
    length          int     %8.0g                 doce
    turn            int     %8.0g                 doce
    displacement    int     %8.0g                 doce
    gear_ratio      float   %6.2f                 doce
    foreign         byte    %8.0g      origin     doce
    ----------------------------------------------------------------------------------------------------------------
    Sorted by:  foreign
    Thanks in advance
    Rodrigo
    Last edited by Rodrigo Badilla; 20 Jan 2021, 15:01.

  • #2
    Code:
    clear all
    set more off
    sysuse auto
    
    local names uno dos tres cuatro cinco seis siete ocho nueve diez once doce
    foreach var of varlist _all {
        local list `list' `var'
    }
    forvalues i=1(1)12 {
            local name : word `i' of `names'
            local varname : word `i' of `list'
            label variable `varname' `name'
    }
    
    . describe
    
    Contains data from C:\Program Files\Stata16\ado\base/a/auto.dta
      obs:            74                          1978 Automobile Data
     vars:            12                          13 Apr 2018 17:45
                                                  (_dta has notes)
    ----------------------------------------------------------------------------------------------------------------------------
                  storage   display    value
    variable name   type    format     label      variable label
    ----------------------------------------------------------------------------------------------------------------------------
    make            str18   %-18s                 uno
    price           int     %8.0gc                dos
    mpg             int     %8.0g                 tres
    rep78           int     %8.0g                 cuatro
    headroom        float   %6.1f                 cinco
    trunk           int     %8.0g                 seis
    weight          int     %8.0gc                siete
    length          int     %8.0g                 ocho
    turn            int     %8.0g                 nueve
    displacement    int     %8.0g                 diez
    gear_ratio      float   %6.2f                 once
    foreign         byte    %8.0g      origin     doce
    ----------------------------------------------------------------------------------------------------------------------------
    Sorted by: foreign
         Note: Dataset has changed since last saved.
    Last edited by Ali Atia; 20 Jan 2021, 15:21.

    Comment


    • #3
      Ali Atia Exist some posibility to make the same but with 2 words in labels,

      I tried with tokenize but failed

      Code:
      tokenize `""etiqueta uno" "etiqueta dos" "etiqueta tres" "etiqueta cuatro" "etiqueta cinco" "etiqueta seis" "etiqueta siete" "etiqueta ocho" "etiqueta nueve" "etiqueta diez" "etiqueta once" "etiqueta doce""'
      
      foreach var of varlist _all {
          local list `list' `var'
      }
      forvalues i=1(1)12 {
              local name : "``i''"
              local varname : word  `i' of `list'
              label variable `varname' "`name'"
      }
      Last edited by Rodrigo Badilla; 20 Jan 2021, 16:18.

      Comment


      • #4
        I have success with a little change of my second request:

        Code:
         tokenize `""etiqueta uno" "etiqueta dos" "etiqueta tres" "etiqueta cuatro" "etiqueta cinco" "etiqueta seis" "etiqueta siete" "etiqueta ocho" "etiqueta nueve" "etiqueta diez" "etiqueta once" "etiqueta doce""'
        
        foreach var of varlist _all {    
        
            local list `list' `var'
        
        }
        
        forvalues i=1(1)12 {
        
            local name   "``i''"
            local varname : word `i' of `list'
            label variable `varname' "`name'"
        
        }
        
        describe
        
        Contains data from C:\Program Files (x86)\Stata13\ado\base/a/auto.dta
          obs:            74                          1978 Automobile Data
         vars:            12                          13 Apr 2013 17:45
         size:         3,182                          (_dta has notes)
        ----------------------------------------------------------------------------------------------------------------
                      storage   display    value
        variable name   type    format     label      variable label
        ----------------------------------------------------------------------------------------------------------------
        make            str18   %-18s                 etiqueta uno
        price           int     %8.0gc                etiqueta dos
        mpg             int     %8.0g                 etiqueta tres
        rep78           int     %8.0g                 etiqueta cuatro
        headroom        float   %6.1f                 etiqueta cinco
        trunk           int     %8.0g                 etiqueta seis
        weight          int     %8.0gc                etiqueta siete
        length          int     %8.0g                 etiqueta ocho
        turn            int     %8.0g                 etiqueta nueve
        displacement    int     %8.0g                 etiqueta diez
        gear_ratio      float   %6.2f                 etiqueta once
        foreign         byte    %8.0g      origin     etiqueta doce
        ----------------------------------------------------------------------------------------------------------------
        Sorted by:  foreign
        
        
        
                  
        }
        Last edited by Rodrigo Badilla; 20 Jan 2021, 16:45.

        Comment


        • #5
          This can be trimmed down to one loop.


          Code:
          sysuse auto, clear 
          
          tokenize uno dos tres cuatro cinco seis siete ocho nueve diez once doce 
          
          local i = 1 
          foreach var of varlist _all {    
              label variable `var' "etiqueta ``i''"
              local ++i 
          }

          Comment


          • #6
            Thanks Ali Atia and Nick Cox for you reply. Good trick in the last reply (local ++1).

            Regards

            Comment


            • #7
              Always good to know how to do this with basic commands. It is a bit more convenient with elabel (SSC)

              Code:
              elabel variable (_all) ///
                  ("etiqueta uno"    ///
                   "etiqueta dos"    ///
                   "etiqueta tres"   ///
                   "etiqueta cuatro" ///
                   "etiqueta cinco"  ///
                   "etiqueta seis"   ///
                   "etiqueta siete"  ///
                   "etiqueta ocho"   ///
                   "etiqueta nueve"  ///
                   "etiqueta diez"   ///
                   "etiqueta once"   ///
                   "etiqueta doce")

              Or, in two steps:

              Code:
              elabel variable (_all) ///
                  (uno dos tres cuatro cinco seis siete ocho nuve diez once doce)
              
              elabel variable (_all) (= "etiqueta " + @)

              Comment

              Working...
              X