Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

AW: st: AW: add new record for individual in dataset


From   "Martin Weiss" <martin.weiss1@gmx.de>
To   <statalist@hsphsun2.harvard.edu>
Subject   AW: st: AW: add new record for individual in dataset
Date   Wed, 28 Oct 2009 13:25:29 +0100

<> 


Your code puts the missing "end" into the first spell within "id", while
Marcus wanted it to reside in the last one...

At least this is what I get from your code:

	+--------------------------+
	id   begin    end   cens 
	--------------------------
1.	1    1997      .      0 
2.	1    2006   2006      1 
	--------------------------
3.	2    1997      .      0 
4.	2    2003   2003      1 
	--------------------------
5.	3    1997      .      0 
6.	3    2004   2004      0 
7.	3    2007   2007      1 
	--------------------------
8.	4    1997      .      0 
9.	4    2006   2006      1


HTH
Martin


-----Ursprüngliche Nachricht-----
Von: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von Scott Merryman
Gesendet: Mittwoch, 28. Oktober 2009 13:24
An: statalist@hsphsun2.harvard.edu
Betreff: Re: st: AW: add new record for individual in dataset

Here is another way:

clear
input id      begin   end             cens
1       1997    2006       0
2       1997    2003       0
3       1997    2004       0
3       2004    2007          0
4       1997    2006       0
end
bys id: gen foo  = cond(_n == _N,2,1)
expand foo
bys id (begin): replace begin = end[_n-1] if _n >1
by id: replace end = cond(_n==1, . , begin)
by id:replace cens = 1 if _n == _N
drop foo
l, sepby(id)


Scott


On Wed, Oct 28, 2009 at 6:50 AM, Martin Weiss <martin.weiss1@gmx.de> wrote:
>
> <>
>
>
>
> *************
> clear*
>
> input  byte(id )        begin   end             cens
> 1       1997    2006       0
> 2       1997    2003       0
> 3       1997    2004       0
> 3       2004    2007          0
> 4       1997    2006       0
> end
>
> compress
> list, noobs sepby(id)
> tempfile temp
> save `temp', replace
>
> collapse (last) end, by(id)
> rename end begin
> gen end=.
> gen cens=1
>
> append using `temp'
>
> order id begin end cens
> sort id begin
> list, noobs sepby(id)
> *************
>
>
>
> HTH
> Martin
>
>

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   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