Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: reorganizing data


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: reorganizing data
Date   Wed, 6 Sep 2006 17:53:11 +0100

Note also that you could do two passes, one
to ascertain the max number of others, and
another to assign to variables. A bit inefficient, 
but perhaps a little cleaner. 

Code also untested. 

local nmax = 1 

levelsof Person, local(Persons)
qui foreach P of local Persons {
 	levelsof City if Person == "`P'", local(Cities)
 	local which
 	foreach C of local Cities {
 		levels Person if Person != "`P'" & City == "`C'", ///
   				 local(work) clean
 		local which : list which | work
 	}
 	local nwhich : word count `which' 
	local nmax = max(`nmax', `nwhich') 
}

forval i = 1/`nmax' { 
	gen Other`i' = "" 
} 

qui foreach P of local Persons {
 	levelsof City if Person == "`P'", local(Cities)
 	local which
 	foreach C of local Cities {
 		levels Person if Person != "`P'" & City == "`C'", ///
   				 local(work) clean
 		local which : list which | work
 	}
 	tokenize `which' 
	local nwhich : word count `which' 
 	forval i = 1/`nwhich' { 
 		replace Other`i' = "``i''" if Person == "`P'"
 	}
 }

Nick 
n.j.cox@durham.ac.uk 

Nick Cox
 
> It can be done. 
> 
> Anna Lehman
>  
> > Going back to your suggestion,
> > If the number of observations is large and the information 
> > does not fit into 
> > a string variable,
> > is there any way I can still store the obtained information?
> > For example, since  for person 1, the list is: 2, 5 4 and 8,
> > Others would contain a string with "2 5 4 8". That is fine. 
> > The problem is 
> > that if the list has many numbers they won't fit into the 
> > variable "Others".
> > Can I store the different numbers (2,5, 4 and 8) in different 
> > columns/variables (instead of creating the variable Others)? 
> > This is the 
> > only way I can think of dealing with a large number of 
> > observations but I'm 
> > not sure how to operationalize it... Any suggestions?

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index