Quantcast
Channel: Statalist
Viewing all articles
Browse latest Browse all 65533

multiplying and summing quantity and price for 112 items

$
0
0
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:

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
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



Viewing all articles
Browse latest Browse all 65533

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>