Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

Re: st: matching observations across variables


From   Sergiy Radyakin <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: matching observations across variables
Date   Fri, 28 Jun 2013 23:11:15 -0400

Yogesh, edit in the last line to implement your logic. You do not
describe when the outcome should be missing, what to do if there are
no neighbors, etc. The problem is pretty standard and its solution is
somewhere in the FAQ. However it was faster to write it from scratch
than search for it and you took some effort to write a code to
generate an example data. That encourages responses (at least imho).
It might be useful for you to read about data transformation within
households data (given ids of parents recover the ages of parents for
children, and other similar problems).

Best, Sergiy Radyakin

clear
input str2 state city neighboring_city1 neighboring_city2
neighboring_city3 outcome neighbor
"A" 1 3 4 5 1 .
"A" 2 1 4 5 1 .
"A" 3 1 5 6 0 1
"A" 4 1 2 . 0 1
"A" 5 1 2 3 0 1
"A" 6 5 4 . 0 0
end

drop state neighbor
list

isid city
preserve
keep city outcome
rename city city_ref
rename outcome outcome_ref
sort city_ref

tempfile city_ref
save `city_ref'
restore

forval i=1/3 {
    rename neighboring_city`i' city_ref
    sort city_ref
    merge city_ref using "`city_ref'", keep(outcome_ref) nokeep
    drop _merge
    rename city_ref neighboring_city`i'
    rename outcome_ref outcome`i'
}

sort city

*** edit here to implement your logic
generate neighbor=(outcome==0) & inlist(1, outcome1, outcome2, outcome3)
*************************************
list

On Fri, Jun 28, 2013 at 8:46 PM, Yogesh Uppal
<[email protected]> wrote:
> Hi Statalisters,
>
> I have a problem that I am not sure how to solve. I have data of the
> following form:
>
> input str2 state city neighboring_city1 neighboring_city2
> neighboring_city3 outcome neighbor
> "A" 1 3 4 5 1 .
> "A" 2 1 4 5 1 .
> "A" 3 1 5 6 0 1
> "A" 4 1 2 . 0 1
> "A" 5 1 2 3 0 1
> "A" 6 5 4 . 0 0
> end
>
> neighboring_city1, neighboring_city2, and neighboring_city3 identify
> all the neighboring cities of cities in the variable "city". outcome
> is some variable I am interested in. This is how I have set up the
> data so far. I want to create a variable called "neighbor" that is 1
> for all cities that have 0 as outcome and are a neighbor to a city
> with 1 as outcome. For example, the neighbor variable I created here
> is 1 for city 3 because it is neighbor to city 1. However, city 6 is 0
> because it is not a neighbor to a city that has 1 as outcome.
>
> I hope I laid out the problem clearly. I would appreciate it if anyone
> can suggest a solution.
>
> thanks in advance,
>
> Yogesh
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/statalist-faq/
> *   http://www.ats.ucla.edu/stat/stata/
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index