I am trying to find a way to loop element by element operations across two lists of variables. If anyone know how to help me I would be extremely grateful.
Consider the following example. I would like to look at the effects of length and weight on prices. I know how to loop a regression changing a variable in the following way.
Code:
webuse auto2.dta,clear * loop my regression over a list of variable foreach var of varlist weight length{ reg price `var' turn }
Code:
* create grouping variables gen lowweight=1 replace lowweight=0 if weight>2240 gen lowlength=1 replace lowlength==0 if length<170 * manually the regression output I would like is produced by the following regressions reg price length turn if lowlength==1 reg price weight turn if lowweight==1
I have tried the following, but it loops every element of the first list over every element of the second list. Hence it is not an element-wise loop
Code:
foreach var of varlist weight length{ foreach grp of varlist lowweight lowlength{ reg price `var' turn if `grp'==1 } }
If anyone has any suggestion on how to address this I would be immensely grateful
Thanks
0 Response to How to loop element-by-element operations (e.g regression)
Post a Comment