Hi everyone,
I'm trying to create a loop that considers four different variables in parallel. I tested the code using levelsof with the sysauto dataset like this:
The problem is that the lists returned in the local macros are of different lengths because only make and price are unique, so when I try to loop over make (stored in `input'), mpg and turn are empty for the last values of `input'. I tried using -valuesof- instead, like this:
But now the problem is that the local macro created for each variable from r(values) gets updated each time r(values) is called, and the locals cannot then be used in the loop.
Can anyone suggest a way to solve this? Or another way of doing this?
Thanks!
Sonia
I'm trying to create a loop that considers four different variables in parallel. I tested the code using levelsof with the sysauto dataset like this:
Code:
sysuse auto, clear keep in 1/5 local i = 0 levelsof make, local(input) di `input' levelsof price, local(output) di `output' levelsof mpg, local(mpg) di `mpg' levelsof turn, local(turn) di `turn' di `input' foreach v of local input{ local i = `i' + 1 local newoutput : word `i' of `output' di "`newoutput'" local newmpg : word `i' of `mpg' di "`newmpg'" local newturn : word `i' of `turn' di "`newturn'" }
Code:
sysuse auto, clear keep in 1/10 local i = 0 valuesof(make) di r(values) local input r(values) di `input' valuesof(price) di r(values) local output r(values) di `output' valuesof(mpg) di r(values) local mpg r(values) di `mpg' valuesof(turn) di r(values) local turn r(values) di `turn' di `input' foreach v of local input{ local i = `i' + 1 local newoutput : word `i' of `output' di "`newoutput'" local newmpg : word `i' of `mpg' di "`newmpg'" local newturn : word `i' of `turn' di "`newturn'" }
Can anyone suggest a way to solve this? Or another way of doing this?
Thanks!
Sonia
Comment