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 on April 23, and its replacement, statalist.org is already up and running.


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

Re: st: Use 2 variables to gen 10 new variables


From   daniel klein <klein.daniel.81@googlemail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Use 2 variables to gen 10 new variables
Date   Thu, 28 Jul 2011 18:54:40 +0200

Jonathan,

here is another (last) try. I still do not see what you want to do
with variables like these, and I strongly recommend looking for other
solutions to your problem.

Note that the created variables have no conection to the observations
in the dataset, just like the variables generated by -prgen- (from
findit -spost9-), meaning that values are filled in starting from the
first observation.
Also note, that the approach is not (yet) very stable. All variables
have to hold the same number of values (5 in the example below). I did
not -set seed- so just run the example a couple of times (or to speed
things up, delete a complete category from x1) to see the problem. I
guess you can probably fix this, however since I cannot figure out
what these variables are needed for, I lack motivation.

* set data
clear
set obs 20

forval j = 1/2 {
	g x`j' = 1 +int(5*runiform())
}
replace x1 = . in 1
la de x 1 Y1 2 Y2 3 Y3 4 Y4 5 Y5
la val x* x

* view
tab1 *

* new variables
forval j = 1/5 {
	qui g y`j' = .
}
la de y 1 X1 2 X2
la val y* y

* fill in values
tempname freq freqx1 freqx2

forval j = 1/2 {
	qui ta x`j' ,matcell(`freqx`j'')
}

forval j = 1/5 {
	loc to = `freqx1'[`j', 1]
	qui replace y`j' = 1 in 1/`to'
	loc from = `to' + 1
	loc to = `to' + `freqx2'[`j', 1]
	qui replace y`j' = 2 in `from'/`to'
}

* view
l
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/


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