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]

st: Re: Replace current values matching certain condition using values from other observations?


From   "Joseph Coveney" <[email protected]>
To   <[email protected]>
Subject   st: Re: Replace current values matching certain condition using values from other observations?
Date   Sat, 25 Jan 2014 17:33:22 +0900

Chris Yang wrote:

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.

--------------------------------------------------------------------------------

In Stata, you tend to avoid looping over data.  You can often take advantage of
the fact that its data operations are "vectorized".

Joseph Coveney

. input byte(group seq var1 var2 var3)

        group       seq      var1      var2      var3
  1. 1       1     3      2     3
  2. 1       2     1      1     2
  3. 1       3     2      2     4
  4. 2       1     3      2     1
  5. 2       2     3      3     3
  6. 3       1     3      2     1
  7. end

. 
. bysort group (seq): replace var1 = var1[1] if seq > 1
(2 real changes made)

. by group: replace var2 = var2[1] if seq > 1
(2 real changes made)

. 
. list, noobs sepby(group)

  +----------------------------------+
  | group   seq   var1   var2   var3 |
  |----------------------------------|
  |     1     1      3      2      3 |
  |     1     2      3      2      2 |
  |     1     3      3      2      4 |
  |----------------------------------|
  |     2     1      3      2      1 |
  |     2     2      3      2      3 |
  |----------------------------------|
  |     3     1      3      2      1 |
  +----------------------------------+

. 
. exit

end of do-file

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