Hi all,
I am using Stata 14.2 on windows. My problem should not be very hard but I cannot seem to grasp what command exactly I should use.
My data has a random amount of observations for every ID and for every observation it has a value 1 for one of the function dummies.
What I would like is for every observation of an ID to have a value 1 for a dummy if any other observation within that ID group has a 1 for that dummy.
So for example it should look like this for ID 2062:
I tried
and that copied a value of 1 to each woon dummy in the ID group, but when I tried to do that for the other functions it did not replace the values correctly.
I have to admit I don't really understand how to apply [_n] and [_N], I thought that if I used the above command, it would read 'if woon[_N] == 1' as 'if for any observation for this ID the value of woon == 1 give all observations within this ID group a value of 1 for woon'.
Thank you for reading,
Lonneke
I am using Stata 14.2 on windows. My problem should not be very hard but I cannot seem to grasp what command exactly I should use.
My data has a random amount of observations for every ID and for every observation it has a value 1 for one of the function dummies.
What I would like is for every observation of an ID to have a value 1 for a dummy if any other observation within that ID group has a 1 for that dummy.
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input long ID float(woon bijeenkomst gemengd kantoor industrie winkel overig onderwijs sport) 2010 0 0 1 0 0 0 0 0 0 2012 0 0 1 0 0 0 0 0 0 2012 1 0 0 0 0 0 0 0 0 2012 1 0 0 0 0 0 0 0 0 2017 0 1 0 0 0 0 0 0 0 2017 1 0 0 0 0 0 0 0 0 2017 1 0 0 0 0 0 0 0 0 2017 1 0 0 0 0 0 0 0 0 2017 1 0 0 0 0 0 0 0 0 2033 0 0 1 0 0 0 0 0 0 2037 0 0 1 0 0 0 0 0 0 2047 0 0 1 0 0 0 0 0 0 2052 0 0 1 0 0 0 0 0 0 2062 0 0 1 0 0 0 0 0 0 2062 0 0 1 0 0 0 0 0 0 2062 0 0 1 0 0 0 0 0 0 2062 0 0 0 0 0 1 0 0 0 2062 1 0 0 0 0 0 0 0 0 2079 0 0 1 0 0 0 0 0 0 2080 0 0 1 0 0 0 0 0 0 2081 0 0 0 0 1 0 0 0 0 2081 1 0 0 0 0 0 0 0 0 2084 0 0 1 0 0 0 0 0 0 2084 1 0 0 0 0 0 0 0 0 2088 0 0 1 0 0 0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0 0 0 0 0 0 2101 1 0 0 0 0 0 0 0 0 2101 1 0 0 0 0 0 0 0 0 2101 1 0 0 0 0 0 0 0 0 2101 1 0 0 0 0 0 0 0 0 2101 1 0 0 0 0 0 0 0 0 2101 1 0 0 0 0 0 0 0 0 2101 1 0 0 0 0 0 0 0 0 2110 0 0 1 0 0 0 0 0 0 2146 0 0 1 0 0 0 0 0 0 2146 1 0 0 0 0 0 0 0 0 2150 0 0 1 0 0 0 0 0 0 2150 1 0 0 0 0 0 0 0 0 2150 1 0 0 0 0 0 0 0 0 2150 1 0 0 0 0 0 0 0 0 2150 1 0 0 0 0 0 0 0 0 2150 1 0 0 0 0 0 0 0 0 2150 1 0 0 0 0 0 0 0 0 2160 0 0 1 0 0 0 0 0 0 2163 0 0 1 0 0 0 0 0 0 2165 0 0 1 0 0 0 0 0 0 2169 0 0 1 0 0 0 0 0 0 2190 0 0 1 0 0 0 0 0 0 2197 0 0 1 0 0 0 0 0 0 2197 1 0 0 0 0 0 0 0 0 2197 1 0 0 0 0 0 0 0 0 2203 0 0 1 0 0 0 0 0 0 end
Code:
2062 1 0 1 0 0 1 0 0 0 2062 1 0 1 0 0 1 0 0 0 2062 1 0 1 0 0 1 0 0 0 2062 1 0 1 0 0 1 0 0 0 2062 1 0 1 0 0 1 0 0 0
Code:
bysort ID: replace woon = 1 if woon[_N] == 1
I have to admit I don't really understand how to apply [_n] and [_N], I thought that if I used the above command, it would read 'if woon[_N] == 1' as 'if for any observation for this ID the value of woon == 1 give all observations within this ID group a value of 1 for woon'.
Thank you for reading,
Lonneke
Comment