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   Mon, 4 Sep 2006 19:42:20 +0100

I tested my code on your example data. 

See Ronnie Babigumira's posting for corroboration.

As is evident from the code, I took both variables 
to be string. If your variables were not 
string, the code needs to be modified, although 
I don't think that would explain the problem you 
reported. 

I cannot see that the format of the variables 
has any possible bearing on the point. It may 
be that you use the word "format" in a non-Stata
sense, but if so I do not know what sense you intend
and you will have to explain it to me. Perhaps you 
mean variable type, but that point is already made. 

The first step for you is to copy what Ronnie
has made very clear and to check that you 
can reproduce it. 

If that works, you need to modify it for your 
real data. If you can't get that to work, you should
report in detail the exact code you used and what
happened. 

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

Anna Lehman
 
> Sorry I wasn't clear. I dont' believe that is the problem 
> since in my code I 
> typed "persons" everywhere.
> Do you think it may have to do with the format of the variables?
> 
Nick Cox

> >My code said "Persons"; you typed "persons".
> >If you type "Persons" in one place and "persons"
> >in another, then you break the code.

Anna Lehman

> > > Thanks for your message.
> > > I tried running the code you sent me and I get the following
> > > error message:
> > > ". qui foreach P of local persons {
> > > option local incorrectly specified"
> > >
> > > What am I doing wrong?

n j cox

> > > >This should work with toy datasets. If your identifiers are
> > > >long, or your number of observations is large, the information
> > > >won't fit into a string variable, so the lines mentioning
> > > >"Others" should be deleted.
> > > >
> > > >gen Others = ""
> > > >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
> > > >	}
> > > >	noi di "`P': `which'"
> > > >	replace Others = "`which'" if Person == "`P'"
> > > >}

Anna Lehman

> > > >I have a dataset with the following structure:
> > > >
> > > >City   Person_id
> > > >A          1
> > > >A          2
> > > >B          1
> > > >B          5
> > > >C          1
> > > >C          5
> > > >C          4
> > > >D          8
> > > >D          1
> > > >
> > > >I would like to obtain the following:
> > > >for each and every person, a list with the people that have
> > > apartments in
> > > >the same city (independently of which city). For example,
> > > for person 1,
> > > >this
> > > >list would be: 2, 5 4 and 8. And for person 5 the list 
> would be: 1 .
> > > >
> > > >Can you think of a relatively easy way of acomplishing this?

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