I am new to using Stata, I have always made corrections using already clean bases.
I have the variables "sales" and "economic_sector". I need to create another variable called "MARKET_SHARE", which calculates the ratio of sales to total sales by economic sector.
I was trying to calculate the value of total sales given the condition like this:
egen float TOTAL_1=rowtotal(cond(economic_sector==1,sales,0))
egen float TOTAL_2=rowtotal(cond(economic_sector==2,sales,0))
egen float TOTAL_3=rowtotal(cond(economic_sector==1,sales,0))
gen marketshare = 0
replace marketshare=sales/TOTAL_1 if economic_sector==1
replace marketshare=sales/TOTAL_2 if economic_sector==2
replace marketshare=sales/TOTAL_3 if economic_sector==3
but replace marketshare=sales/TOTAL_1 if economic_sector==1, gives me "factor variables and time-series operators not allowed"
Any help would be very usefull.
Related Posts with Sum of values of a conditional variable
Plotting -xtmixed- growth curve modelHello, I am using Stata 15.1 and I want to plot my growth curve model. I wave 5 waves and I want to…
Restructure dates in sequential mannerI got a raw dataset in a wide format that includes hundreds of variables defining drug prescriptions…
new variable: replace with nonumeric variable contentHey, I want to generate a new variable out of a variable that is already existing. However, the exi…
creating an mutinomial variableHello, I am trying to establish a multinomial logit model in stata. I have a panel of 60 countries, …
Quintiles comparisonIs there a non parametric method to compare quintiles in STATA? like we compare median …
Subscribe to:
Post Comments (Atom)
0 Response to Sum of values of a conditional variable
Post a Comment