Hello,
I am trying to use a command that only works if the names of my variables have the last character to be the one I am trying to total using commands "egen" and "robs". I have 112 variables with lists that have up to 10 cars and 12 months. It looks like person_eligcar_1_1, person_eligcar_2_1, person_eligcar_3_1...... person_eligcar_10_11, person_eligcar_10_12.
Unfortunately, I need all these digits to be switched in the variable name. For ex. person_eligcar_2_1 need to be person_eligcar_1_2. I have no idea how to do this other than rename them manually. The loop Im trying to make is something like this:
foreach v of varlist person_eligcar_1_1, person_eligcar_2_1, person_eligcar_3_1................................ .................... person_eligcar_10_12, {
rename v substr(v, 1, 15) + substr(v_1_2, 18) + _ + substr(v_1_2, 16)
}
My thinking is that, I should keep the first 15 characters of the string, change the 16th character to the 18th, and the 18th to the 16th.
The rename command is not letting me do this.
Ultimately I was to use egen countcars1 = robs(person_eligcar_1_1 - person_eligcar_1_10), it will only work if the last number is the number representing the cars.
Any help would be greatly appreciated!
I am trying to use a command that only works if the names of my variables have the last character to be the one I am trying to total using commands "egen" and "robs". I have 112 variables with lists that have up to 10 cars and 12 months. It looks like person_eligcar_1_1, person_eligcar_2_1, person_eligcar_3_1...... person_eligcar_10_11, person_eligcar_10_12.
person_eligcar_1_1 | person_eligcar_1_2 | ….......... | person_eligcar_10_12 | |
1 | 2 | 2 | 1 | |
2 | 3 | 1 | 2 | |
3 | 1 | 4 | 1 | |
4 | 1 | 2 | 4 |
Unfortunately, I need all these digits to be switched in the variable name. For ex. person_eligcar_2_1 need to be person_eligcar_1_2. I have no idea how to do this other than rename them manually. The loop Im trying to make is something like this:
foreach v of varlist person_eligcar_1_1, person_eligcar_2_1, person_eligcar_3_1................................ .................... person_eligcar_10_12, {
rename v substr(v, 1, 15) + substr(v_1_2, 18) + _ + substr(v_1_2, 16)
}
My thinking is that, I should keep the first 15 characters of the string, change the 16th character to the 18th, and the 18th to the 16th.
The rename command is not letting me do this.
Ultimately I was to use egen countcars1 = robs(person_eligcar_1_1 - person_eligcar_1_10), it will only work if the last number is the number representing the cars.
Any help would be greatly appreciated!
Comment