I have a dataset which includes consumption quantity and price per unit for 112 food items. Therefore, each observation has 112 variables for quantity and 112 variables for price. I want to calculate total expenditures on food by multiplying each quantity consumed with its corresponding price. I could write 112 lines of syntax, but I was wondering whether there is a faster way of doing this? I tried using the foreach command but it seems to be limited to multiplying each variable with a constant instead of each variable with a different variable. The data is structured as follows:
I want to multiply i1_1_5 (quantity) with i1_1_4 (price) for each food item, so i1_2_5 multiplied by i1_2_4 and so on for each of the 112 items.
Does anybody have any advice?
Thanks
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input double(i1_1_5 i1_2_5 i1_3_5 i1_4_5 i1_5_5 i1_1_4) int(i1_2_4 i1_3_4 i1_4_4 i1_5_4) 5 . . . . 8000 . . . . 3.5 . . . 2 7600 . . . 2500 7 . . . . 9000 . . . . 4 . . . . 7600 . . . . 7 . . . . 9000 . . . . 5 . . . . 10000 . . . . 7 . . . . 8000 . . . . 7 . . . 7 9000 . . . 2000 7 . . . 2 . . . . 2000 10 . . . 4 9500 . . . 2600 end
Does anybody have any advice?
Thanks
Comment