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:12:01 +0200

```Jonathan,

here is a(nother) way to get the numerical values. Note however that
this approach seems very error-prone. It might be better to hold
numerical values as value labels in new variables, or, even better
than that, to look for another approach to solve the underlying
problem.

* 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 f p

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

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

* proportions to new variable
g newvar_q5`opp' = 0 /* note that this will result in 0
_not_ missing values for cases in which q5a == 2 q5b == .*/
forval j = 1/`= rowsof(`p')' {
qui replace newvar_q5`opp' = `p'[`j', 2] if q5`opp' == `p'[`j', 1]
}
}

* view result
tab1 *
l in 1/10

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/
```