0
votes

I am trying to write a code in Stata. I am currently working with a data set with the unit of observation as households. There are also variables for each member of the household e.g hv101_01 for person one in the household, hv101_02 for the second person in the household and up to hv101_39, all of which contain the same label. This is is the case for many variables.

I will like to change the names of all variables to the label name. I was able to figure this out as:

foreach v of var * {
local lbl : var label `v'
local lbl = strtoname("`lbl'")
rename `v' `lbl'
label variable `lbl' "`v'"
}

But when it reaches the variables that are for the second member of the household e.g hv101_02, Stata says that the variable name is already defined. I know that this is because hv101_01 already has taken that label name.

I will like to add _02 or whatever number is behind the variable when the variable is changed to the label name. Can someone please help with a code for this.

Thank you for the anticipated response.

2

2 Answers

1
votes

Consider the following toy example based on your variable names:

clear
set obs 1

forvalues i = 11 / 15 {
    generate hv101_`i' = rnormal()
    label variable hv101_`i' ExampleVarLabel
}

describe, fullnames 

Contains data
  obs:             1                          
 vars:             5                          
 size:            20                          
-------------------------------------------------------------------------------------------------------------------------------------
              storage   display    value
variable name   type    format     label      variable label
-------------------------------------------------------------------------------------------------------------------------------------
hv101_11        float   %9.0g                 ExampleVarLabel
hv101_12        float   %9.0g                 ExampleVarLabel
hv101_13        float   %9.0g                 ExampleVarLabel
hv101_14        float   %9.0g                 ExampleVarLabel
hv101_15        float   %9.0g                 ExampleVarLabel
-------------------------------------------------------------------------------------------------------------------------------------
Sorted by: 
     Note: Dataset has changed since last saved.

The following then works for me:

foreach v of var * {
    local lbl : variable label `v'
    rename `v' `lbl'`=substr("`v'", strpos("`v'", "_"), .)'
    label variable `lbl'`=substr("`v'", strpos("`v'", "_"), .)' `v'
}

describe, fullnames

Contains data
  obs:             1                          
 vars:             5                          
 size:            20                          
-------------------------------------------------------------------------------------------------------------------------------------
              storage   display    value
variable name   type    format     label      variable label
-------------------------------------------------------------------------------------------------------------------------------------
ExampleVarLabel_11
                float   %9.0g                 hv101_11
ExampleVarLabel_12
                float   %9.0g                 hv101_12
ExampleVarLabel_13
                float   %9.0g                 hv101_13
ExampleVarLabel_14
                float   %9.0g                 hv101_14
ExampleVarLabel_15
                float   %9.0g                 hv101_15
-------------------------------------------------------------------------------------------------------------------------------------
Sorted by: 
     Note: Dataset has changed since last saved.

EDIT:

If the labels are the same and the variable numbering sequential, you do not even need a loop:

local lbl : variable label hv101_11
rename hv101_* `lbl'_#, renumber(11)
1
votes

This is the correct answer as provided by Andrew Musau in https://www.statalist.org/forums/forum/general-stata-discussion/general/1541234-renaming-long-list-of-variables-with-loop

foreach v of var hvidx_01 - hv124_39 {
         local lbl : var label `v'
         local lbl= subinstr("`lbl'"," ","_",.)
         local end=substr("`v'", -1, 2)
         rename `v' `lbl'_`end'
         label variable `lbl'_`end' `v'
 }