Dear Stata users,
Suppose I have one continuous variable and one categorical variable. I want to calculate sum of continuous variable for each category of the categorical variable, and then calculate proportions of them to sum of the continuous variable (i.e. the total). For example, sum of continuous variable for category A equals 2000, for category B equals 3000, and the total equals 5000, so I will get two proportions: 2000/5000=40% and 3000/5000=60%. Steps to do that is very easy, however, I have so many continuous variables that I want to know if there's some commands to use other than by calculating one by one or by using a loop.
Suppose I have one continuous variable and one categorical variable. I want to calculate sum of continuous variable for each category of the categorical variable, and then calculate proportions of them to sum of the continuous variable (i.e. the total). For example, sum of continuous variable for category A equals 2000, for category B equals 3000, and the total equals 5000, so I will get two proportions: 2000/5000=40% and 3000/5000=60%. Steps to do that is very easy, however, I have so many continuous variables that I want to know if there's some commands to use other than by calculating one by one or by using a loop.
Code:
sysuse auto egen price_foreign=sum(price), by(foreign) egen price_total=sum(price) gen price_proportion=price_foreign/price_total*100 tabulate price_proportion
Comment