Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: RE: copy values of matched cases


From   "Patrick Sturgis" <p.sturgis@surrey.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: RE: copy values of matched cases
Date   Tue, 8 Apr 2003 12:28:13 +0100

Many thanks

does this require version 8? I get the error message 'version 8 not
supported' when I run command:

 levels serno2, local(levels)

even though I have successfully installed levels.

Patrick

-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu]On Behalf Of Nick Cox
Sent: 08 April 2003 11:20
To: statalist
Subject: st: RE: copy values of matched cases


Patrick Sturgis
>
> I have the following problem:
>
> I have data on individuals indexed by a unique identifier, SERNO. In
> addition to SERNO, I have a vector of variables Z which are
> measurements
> taken on each individual at time t. Lastly, I have a
> variable SERNO2 which,
> for each case, gives the value of SERNO for a matched case
> in the data set.
> What I wish to do is create a second vector of variables,
> X, which copy the
> values of Z for the case indexed by SERNO2 to each value of
> SERNO. Any
> suggestions?
>

I assume that each value of -serno- occurs just once.
There are various ways to check this, e.g. -isid-,
-duplicates-.

Here is a rather tedious approach. Something more
elegant is probably possible.

     +---------------------------+
     | serno   z1   z2   serno2  |
     |---------------------------|
  1. |    11    6   11       14  |
  2. |    12    7   12       13  |
  3. |    13    8   13       13  |
  4. |    14    9   14       12  |
     +---------------------------+

Set up copies, initially missing:

foreach v of var z* {
	qui gen c`v' = .
}

Get an observation number variable (serno
may already be that)

gen long obsno = _n

With -levels- from SSC,

levels serno2, local(levels)

foreach l of local levels {
	su obsno if serno == `l', meanonly
	foreach v of var z* {
		qui replace c`v' = `v'[`r(min)'] if serno2 == `l'
	}
}

That is, the look-up is done by finding
the observation number for each serial number.

l

     +----------------------------------------------+
     | serno   z1   z2   serno2   cz1   cz2   obsno |
     |----------------------------------------------|
  1. |    11    6   11       14     9    14       1 |
  2. |    12    7   12       13     8    13       2 |
  3. |    13    8   13       13     8    13       3 |
  4. |    14    9   14       12     7    12       4 |
     +----------------------------------------------+

A linked FAQ has just gone up at
http://www.stata.com/support/faqs/data/foreach.html

which in turn supersedes

http://www.stata.com/support/faqs/data/for.html

Ernest Berkhout suggested that the latter should be updated in
a Statalist posting on 24 February.

Nick
n.j.cox@durham.ac.uk

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



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index