I am new-ish to STATA . I have a variable called wk1g12 and I am simply trying to store a 1 in it if certain criteria are met.
The problem is when I try to reference it with "`t'g12"[`m'] = 1 it fails. The parts of this string I am forming to reference an actual variable are `t' = "wk1" and "g12".
I thought combining these to form the string "wk1g12" would allow me to reference the variable with the same name.
However, I get errors like "wk1g12 is not a valid command name" . I have attempted all sorts of different versions of this but none work.
Any pointers on how I can reference the variable correctly by buidling a string of the same name... or saying this is not possible... would help a lot... or any pointers how I could reference it in the loops I have formed!
Thanks for any help!
Code:
foreach t of local time_nodups { forvalues m=1/`=_N' { * to track whether this person has +1 to a given time point (t) yet local count = 0 * iterate over all variables foreach var of varlist _all { * if this variable refers to the correct timepoint (t) if(substr("`var'", -3, 3) == "`t'") { * and if not counted yet for this timepoint(t) if(`count' == 0) { * and if the value matches 1 or 2 if(`var'[`m'] == 1 | `var'[`m'] == 2 ) { * to exit for this person local count = 1 * now when i create a 'string' to 'get' the name of a variable... * and store a value in it.. it fails with "wk1g12 is not a valid command name" "`t'g12"[`m'] = 1 } } } } } }
Comment