I want to (re)label my variables automatically, based on a variable name → new label lookup table.
I currently do it as follows:
Edit: a space friendly alternative could be something along the lines of:
However, this suppose to hardcode the relabelling mapping. I would prefer to define the lookup table externally.
1. How to achieve the same result using the following label_lookup_table.csv?
2. What if I want to use the following generic_label_lookup_table.csv (which also contains renaming_pairs not relevant for the given dataset + has labels with spaces)?
I currently do it as follows:
Code:
sysuse auto
foreach renaming_pair in "make model" "mpg mileage" "rep78 repair" {
label variable `=word("`renaming_pair'", 1)' `"`=word("`renaming_pair'", 2)'"'
}
Code:
sysuse auto
foreach renaming_pair in "make|model of the car" "mpg|mileage" "weight|weight in pounds" {
local var_name = regexr("`renaming_pair'", "\|.*", "")
local label_name = regexr("`renaming_pair'", ".*\|", "")
label variable `var_name' "`label_name'"
}
However, this suppose to hardcode the relabelling mapping. I would prefer to define the lookup table externally.
1. How to achieve the same result using the following label_lookup_table.csv?
Code:
var_name, new_label make, model mpg, mileage rep78, repair
Code:
var_name, new_label make, model of the car mpg, mileage rep78, repair foo, bar baz, buzz

Comment