Announcement

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

  • Labeling variables using a loop depending on the variable name

    Hi

    I am using Stata 13.1 for Windows. I have a dataset with 402 numerical and string variables whose names are shorthand for what they represent. I have listed some of the variable names below:

    bl_el_cnst_cst
    bl_mnt_cnst_cst
    bl_ov_cnst_cst
    st_cv_cnst_cst
    st_el_cnst_cst
    bl_el_cond
    bl_mnt_cond
    bl_ov_cond

    I would like to label them using a loop, such that all variables that end in _cnst_cst have "construction cost" as the last two words of their label; all those ending with _cond would have "condition" as the last two words of their label.
    All variables that start with bl_el have "building electrical" as the first two words of their label; those that start with bl_mnt have "building maintenance as the first two words of the label; those that start with bl_ov have "building overall" as the first words of the label etc.

    So the variables would be labeled as follows:
    bl_el_cnst_cst "building electrical construction cost"
    bl_mnt_cnst_cst "building maintenance construction cost"
    bl_ov_cnst_cst "building overall construction cost"
    st_cv_cnst_cst "site civil construction cost"
    st_el_cnst_cst "site electrical construction cost"
    bl_el_cond "building electrical condition"
    bl_mnt_cond "building maintenance condition"
    bl_ov_cond "building overall condition"

    I'm trying the following code, but I think add and modify are only for label define rather than label var:

    foreach var of varlist bl_el* {
    label var `var' "building electrical"
    }

    foreach var of varlist *_cnst_cst {
    label var `var' "construction cost", add
    }

    Any help would be greatly appreciated. Thanks
    Megan

  • #2
    Here is some technique that might be useful. The key is using a macro extended function to retrieve the initial part of the variable label to then combine with the terminal part of the variable label. See help extended_fcn for details.
    Code:
    clear
    set obs 1
    gen bl_el_cnst_cst = 0
    gen bl_mnt_cnst_cst = 0
    gen bl_ov_cnst_cst = 0
    gen bl_el_cond = 0
    gen bl_mnt_cond = 0
    gen bl_ov_cond = 0
    
    foreach var of varlist bl_el_* {
        label variable `var' "Building Electrical"
        }
    foreach var of varlist bl_mnt_* {
        label variable `var' "Building Maintenance"
        }
    foreach var of varlist bl_ov_* {
        label variable `var' "Building Overall"
        }
    foreach var of varlist *_cnst_cst {
        local lbl : variable label `var'
        label variable `var' "`lbl' Construction Cost"
        }
    foreach var of varlist *_cond {
        local lbl : variable label `var'
        label variable `var' "`lbl' Condition"
        }
        
    describe
    Code:
    Contains data
      obs:             1                          
     vars:             6                          
     size:            24                          
    ------------------------------------------------------------------------------------------------
                  storage   display    value
    variable name   type    format     label      variable label
    ------------------------------------------------------------------------------------------------
    bl_el_cnst_cst  float   %9.0g                 Building Electrical Construction Cost
    bl_mnt_cnst_cst float   %9.0g                 Building Maintenance Construction Cost
    bl_ov_cnst_cst  float   %9.0g                 Building Overall Construction Cost
    bl_el_cond      float   %9.0g                 Building Electrical Condition
    bl_mnt_cond     float   %9.0g                 Building Maintenance Condition
    bl_ov_cond      float   %9.0g                 Building Overall Condition
    ------------------------------------------------------------------------------------------------
    Sorted by:
         Note: Dataset has changed since last saved.
    Last edited by William Lisowski; 02 Jan 2017, 08:15.

    Comment


    • #3
      Here's an alternative to William's solution...

      Code:
      clear
      set obs 1
      gen bl_el_cnst_cst = 0
      gen bl_mnt_cnst_cst = 0
      gen bl_ov_cnst_cst = 0
      gen bl_el_cond = 0
      gen bl_mnt_cond = 0
      gen bl_ov_cond = 0
      gen st_cv_cnst_cst = 0
      gen st_el_cnst_cst = 0
      
      // position 1
      local bl building
      local st site
      
      // position 2
      local el electrical
      local mnt maintenance
      local cv civil
      local ov overall
      
      // position 3
      local cond condition
      local cnst_cst construction cost
      
      foreach x in bl st {
          foreach y in el mnt cv ov {
              foreach z in cond cnst_cst {
                  capture label var `x'_`y'_`z' "``x'' ``y'' ``z''"
              }
          }
      }

      The disadvantage here might be that the code attempts to label variables that aren't in the data (thus the addition of capture). I treat "condition" and "construction cost" as the third position because "construction cost" always went together in the examples you listed.

      Comment


      • #4
        Fantastic!! Thank you so much! Really appreciate it

        Comment


        • #5
          Fantastic codes, I learned a lot. Thank you so much!

          Comment

          Working...
          X