| |
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]
Re: st: reorganizing data
Anna, let me chip in. Both variables should be of the type string...
below is a copy and paste job of your data and Nick's code that works
----------------------------------------------------------------------------
input str1 City str1 Person
A 1
A 2
B 1
B 5
C 1
C 5
C 4
D 8
D 1
end
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 wrote:
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?
Thanks again,
Anna
From: "Nick Cox" <[email protected]>
Reply-To: [email protected]
To: <[email protected]>
Subject: RE: st: reorganizing data
Date: Mon, 4 Sep 2006 18:40:56 +0100
My code said "Persons"; you typed "persons".
If you type "Persons" in one place and "persons"
in another, then you break the code.
Nick
[email protected]
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/
_________________________________________________________________
Search from any web page with powerful protection. Get the FREE Windows Live
Toolbar Today! http://get.live.com/toolbar/overview
*
* 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/
*
* 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/