I am trying to rename a set of variables in Stata 16.1. The new variable name I want to use is stored across three observations, and I want to combine them into a variable name. I've written some toy code below to test my proposed approach.
Code:
clear all * toy dataset set obs 3 gen v1 = "" replace v1 = "part" in 1 replace v1 = "of" in 2 replace v1 = "varname" in 3 list * proposed code for renaming local p1 v1[1] local p2 v1[2] local p3 v1[3] di `p1' `p2' `p3' rename v1 `p1'_`p2'_`p3'
Code:
1 new variable name invalid You attempted to rename v1 to v1[1]_v1[2]_v1[3]. That is an invalid Stata variable name. r(198);
The reason seems to be because the local macros p1 p2 p3 output the variable name with explicit subscript e.g. v1[1] rather than the actual underlying value e.g. "part". Weirdly, however the display command correctly outputs the values stored in each observation.
How I can I get the local macro to pass the values stored in v1[1] v1[2] and v1[3] to the rename command rather than just the variable names?
Thanks in advance,
Dan
0 Response to Local macro returns variable name and subscript rather than value
Post a Comment