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

st: RE: rearranging observations within a variable


From   "Stephen P Jenkins" <[email protected]>
To   <[email protected]>
Subject   st: RE: rearranging observations within a variable
Date   Tue, 10 Feb 2004 17:03:30 -0000

> -----Original Message-----
> From: [email protected] 
> [mailto:[email protected]] On Behalf Of 
> Daniel Mueller
> Sent: 10 February 2004 16:17
> To: Statalist
> Subject: st: rearranging observations within a variable
> 
> 
> Hi.
> 
> There's probably a neat trick to that, but can't find it...
> 
> I have one variable:
>      +-----+
>      | var |
>      |-----|
>   1. |   1 |
>   2. |   3 |
>   3. |   5 |
>   4. |   7 |
>   5. |   9 |
>      |-----|
>   6. |  11 |
>   7. |  13 |
>   8. |  15 |
>   9. |  17 |
>  10. |  19 |
>      +-----+
> 
> The observations of this variable I want to rearrange to 
> arrive at the following:
>      +-----+
>      | var |
>      |-----|
>   1. |   1 |
>   2. |   . |
>   3. |   3 |
>   4. |   . |
>   5. |   5 |
>      |-----|
>   6. |   . |
>   7. |   7 |
>   8. |   . |
>   9. |   9 |
>  10. |   . |
>      +-----+
> i.e. I have to -expand- the data set to get running 
> observation numbers with missing values for observations, 
> which were previouly missing.
> 
> How can I do that??


. ge id = _n

. list

     +-----------+
     | var1   id |
     |-----------|
  1. |    1    1 |
  2. |    3    2 |
  3. |    5    3 |
  4. |    7    4 |
  5. |    9    5 |
     |-----------|
  6. |   11    6 |
  7. |   13    7 |
  8. |   15    8 |
  9. |   17    9 |
     +-----------+

. expand 2
(9 observations created)

. bysort id: replace var1 = . if _n == 2
(9 real changes made, 9 to missing)

. l

     +-----------+
     | var1   id |
     |-----------|
  1. |    1    1 |
  2. |    .    1 |
  3. |    3    2 |
  4. |    .    2 |
  5. |    5    3 |
     |-----------|
  6. |    .    3 |
  7. |    7    4 |
  8. |    .    4 |
  9. |    9    5 |
 10. |    .    5 |
     |-----------|
 11. |   11    6 |
 12. |    .    6 |
 13. |   13    7 |
 14. |    .    7 |
 15. |   15    8 |
     |-----------|
 16. |    .    8 |
 17. |   17    9 |
 18. |    .    9 |
     +-----------+




Stephen
-------------------------------------------------------------
Professor Stephen P. Jenkins <[email protected]>
Institute for Social and Economic Research
University of Essex, Colchester CO4 3SQ, U.K.
Tel: +44 1206 873374.  Fax: +44 1206 873151.
http://www.iser.essex.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/



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