# Generate a variable with percentage values

#### Daniela.R

##### New Member
Hi,
I need some help to generate a new variable with percentage values. I have a dataset like the file I have attached (first five columns) and I want to generate, in STATA, the variable in the last column (in red). Thus, I want to calculate the percentage values of the variable 'st_t2010' by 'istat2010' and 'cod_citt' having as denominator the row in which 'cod_citt=1000'.
If I had only the first 9 rows (only a municipality in this case) I could obtain my new variable by typing:
bys istat2010 ( cod_citt) : gen qst_t2010=( st_t2010/ st_t2010[9])*100

But how can I generate the same results for more municipalities? It is possible to use the expression 'if' to select the denominator (in this case 'if cod_citt == 1000') instead of entering the number of the row in the brackets?

Thanks.

#### bukharin

##### RoboStataRaptor
If you didn't have the total rows (which you generally wouldn't want for such an analysis in Stata), I would use something like:
Code:
egen total_st_t2010=total(st_t2010), by(istat2010)
gen qst_t2010=100 * st_t2010 / total_st_t2010
If you want to keep the total rows you could halve total_st_t2010 before running the second line.

#### Daniela.R

##### New Member
Hi,

thank you so much. I don't need to keep the total rows, thus your solution is perfect!