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]
st: Replace current values matching certain condition using values from other observations?
From
Chris Yang <[email protected]>
To
[email protected]
Subject
st: Replace current values matching certain condition using values from other observations?
Date
Fri, 24 Jan 2014 22:43:02 -0500
Dear All,
I have a dataset that resembles the following structure:
group seq var1 var2 var3
1 1 3 2 3
1 2 1 1 2
1 3 2 2 4
2 1 3 2 1
2 2 3 3 3
3 1 3 2 1
...
Now, within each group, for all the observations whose seq > 1, I want
to replace the values for var1 and var2 with those of the observation
whose seq == 1. For example, for group 1 above, after the
replacements, it would look like this:
group seq var1 var2 var3
1 1 3 2 3
1 2 3 2 2
1 3 3 2 4
If there is no seq > 1 in a given group, then no replacement is needed.
Intuitively, it seems that I need a looping structure to go through
all the observations one by one. And at each step i will check the seq
variable. If it is greater than 1, then look up the values for var1
and var2 from the observation *within the same group* whose seq == 1,
and use them to update the current observation. The question is that
how do I do such look-ups in a loop?
As always, is there a better/more efficient way of doing it? Your
thoughts and suggestions are appreciated.
Best regards,
Chris
*
* 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/