Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, is already up and running.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

From   daniel klein <>
Subject   Re: st: Using Percent of another variable to gen new var
Date   Mon, 25 Jul 2011 17:12:01 +0200


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

* set data
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

*   For searches and help try:

© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index