Dear users,
I have a list of variables that I would like to rename.
I would like to modify the variable names by applying the following changes to the original variable names:
- Remove all characters before "x"
- Add "knows" at the beginning of each variable.
- Replace the symbol "_" by the symbol "-" .
Here's an example of what I would like to accomplish with this set of variables.
- Old variable_name : Q3_8_x1_1
- New variable name: knows1-1
In the original variable names, the first substring of the variable is common to all variables (Q3_8_x), and the second part changes for each variable, ranging from 1_1, 1_2.....to 10_10.
I made an attempt to create a loop, but it's incomplete.
I get an error, and I'm not sure on how to code the replacement of "_" by the symbol "-".
Thanks a lot for the help.
I have a list of variables that I would like to rename.
I would like to modify the variable names by applying the following changes to the original variable names:
- Remove all characters before "x"
- Add "knows" at the beginning of each variable.
- Replace the symbol "_" by the symbol "-" .
Here's an example of what I would like to accomplish with this set of variables.
- Old variable_name : Q3_8_x1_1
- New variable name: knows1-1
In the original variable names, the first substring of the variable is common to all variables (Q3_8_x), and the second part changes for each variable, ranging from 1_1, 1_2.....to 10_10.
I made an attempt to create a loop, but it's incomplete.
I get an error, and I'm not sure on how to code the replacement of "_" by the symbol "-".
Code:
foreach v of varlist Q3_8_x1_1 - Q3_8_x10_10 { local new = substr("`v'",strpos(`v',"x"),5) rename `v' knows`new' }
Comment