Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: programming question


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: programming question
Date   Wed, 23 Jul 2003 20:41:36 +0100

John Baughman
> 
> I have a fair amount of experience using Stata 
> interactively, but little 
> programming experience, and I now have run into a problem 
> that can only be 
> solved with programming (I think).  Any help with how I 
> ought to think 
> about it would be greatly appreciated.  Thus, my first post to the 
> list.  By the way, I'm using Intercooled Stata 8.1.
> 
> The dataset has a string var whose values correspond to the 
> names of dummy 
> variables in the dataset.  What I want to do is generate a 
> new dummy var 
> which equals 1 for a given observation if the dummy for the 
> var whose name 
> corresponds to that observation's string value also equals 
> 1; the new dummy 
> will be 0 otherwise.  I think an illustration of what the 
> data might look 
> like will help more than that convoluted description:
> 
> obs     strvar  aaa     bbb     ccc     newvar
> 1       aaa     1       1       1       1
> 2       ccc     1       0       1       1
> 3       bbb     1       0       1       0
> 4       ccc     0       0       1       1
> 5       aaa     0       0       0       0
> 
> etc etc in various permutations.
> 
> The actual dataset has fifty string values and fifty corresponding 
> dichotomous vars and around 5500 observations.  Any ideas 
> about how I ought 
> to think about this and/or what commands might be useful in 
> getting this 
> coded?  I sense there is something simple I'm missing . . .
> 

gen newvar = . 
forval i = 1/`= _N' { 
	replace newvar = `= strvar[`i']' == 1 in `i' 
}

Nick 
[email protected] 

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index