.- help for ^prod^ .- Calculating the product of observations --------------------------------------- ^^egen^^ newvar = ^prod(^expression^)^ [^if^ exp] [^in^ range] [^, by(^grouping varlist^) pm^iss^(ignore|missing|1)^] Description ----------- ^prod^ provides a multiplicative function for ^^egen^^ analogous to the additive ^sum^ function. The product of observations of meeting optional ^in^ and ^if^ conditions is returned in . would most commonly be an existing variable in the data set. Options ------- ^by(^grouping varlist^)^ specifies the variables defining groups within which the products are calculated. ^pm^iss^(ignore|missing|1)^ specifies what is to be the product of a group at least one of whose observations is missing. ^pm^iss^(ignore)^, the default, ignores missing values and returns the product of all non-missing values in the group. If all values are missing, then missing is returned. ^pm^iss^(missing)^ returns missing for the product of observations in a group if any observation in the group is missing. ^pm^iss^(1)^ returns 1 for the product of observations in a group if all observations in the group are missing. Otherwise it returns the product of all non-missing values. This choice would rarely be made (see Appendix in STB insert). Examples -------- ^use^ testprod1 ^list^ score gvar1 gvar2 1. 3 1 1 2. 4 1 1 3. 5 1 1 4. 1 1 2 5. 2 1 2 6. 10 1 2 7. 4 2 1 8. 5 2 1 9. 6 2 1 10. -3 2 2 11. -1 3 1 12. -3 3 1 13. 4 3 1 14. 2 3 2 15. 5 3 2 16. 0 4 1 17. -23 4 2 ^egen^ px = ^prod(^score^)^ ^egen^ px0 = ^prod(^score^)^ ^if^ score != 0 ^egen^ px1 = ^prod(^score^), by(^gvar1^)^ ^egen^ px12 = ^prod(^score^), by(^gvar1 gvar2^)^ ^list^ score gvar1 gvar2 px px0 px1 px12 1. 3 1 1 0 1192000000 1200 60 2. 4 1 1 0 1192000000 1200 60 3. 5 1 1 0 1192000000 1200 60 4. 1 1 2 0 1192000000 1200 20 5. 2 1 2 0 1192000000 1200 20 6. 10 1 2 0 1192000000 1200 20 7. 4 2 1 0 1192000000 -360 120 8. 5 2 1 0 1192000000 -360 120 9. 6 2 1 0 1192000000 -360 120 10. -3 2 2 0 1192000000 -360 -3 11. -1 3 1 0 1192000000 120 12 12. -3 3 1 0 1192000000 120 12 13. 4 3 1 0 1192000000 120 12 14. 2 3 2 0 1192000000 120 10 15. 5 3 2 0 1192000000 120 10 16. 0 4 1 0 . 0 0 17. -23 4 2 0 1192000000 0 -23 Note that a missing value is returned for observations excluded by ^if^ or ^in^ qualifiers. (See, for example, obs 16 on px0.) ^use^ testprod2 ^li^ score2 group 1. 4 1 2. 2 1 3. 12 1 4. .25 1 5. 2 2 6. 3 2 7. . 2 8. . 3 9. . 3 ^egen^ newp = ^prod(^score2^)^, ^by(^group^)^ ^egen^ newpi = ^prod(^score2^)^, ^by(^group^)^ ^pmiss(ignore)^ ^egen^ newpm = ^prod(^score2^)^, ^by(^group^)^ ^pmiss(missing)^ ^egen^ newp1 = ^prod(^score2^)^, ^by(^group^)^ ^pmiss(1)^ ^li^ score2 group newp newpi newpm newp1 1. 4 1 24 24 24 24 2. 2 1 24 24 24 24 3. 12 1 24 24 24 24 4. .25 1 24 24 24 24 5. 2 2 6 6 . 6 6. 3 2 6 6 . 6 7. . 2 6 6 . 6 8. . 3 . . . 1 9. . 3 . . . 1 Author ------ Philip Ryan Department of Public Health University of Adelaide South Australia pryan@@medicine.adelaide.edu.au Acknowledgements ---------------- Michael Blasnik substantially enhanced the efficiency of the code. Clyde Schechter suggested the option to specify that the product of observations in the empty set should be 1. Nick Cox and Tom Steichen provided helpful suggestions. Contributors to the Stata List provided an interesting thread on handling missing values. Also see -------- Manual: [R] ^egen^ On-line: help for ^egen^