 Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: Using Percent of another variable to gen new var

 From daniel klein To statalist@hsphsun2.harvard.edu Subject Re: st: Using Percent of another variable to gen new var Date Mon, 25 Jul 2011 17:57:54 +0200

```... to illustrate a possible starting point for a solution using value
labels. I think this might be closer to what you have in mind.

* set data
clear
set obs 202
foreach x in a b {
g q5`x' = 1 + int(5*runiform())
}
replace q5b = . if q5b == 2

* view
tab1 *

*create new variables
tempname l p

foreach x in b a {
loc opp = cond("`x'" == "b", "a", "b")

* frequencies and proportions into matrix
qui ta q5`x' ,matrow(`l') matcell(`p')
mat `p' = `l', (`p'/r(N))*100

* create value labels
forval j = 1/`= rowsof(`p')' {
la de q5`x'_p `= `p'[`j', 1]' /*
*/ "`=`p'[`j', 1]' (q5`x' `= round(`p'[`j', 2], .01)' %)" /*
*/ ,modify
}
la val q5`x' q5`opp'_p
}

* view result
tab1 *

Best
Daniel
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```