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]

Re: st: How to fill in the missing data


From   Alexis Penot <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: How to fill in the missing data
Date   Mon, 10 Jun 2013 07:01:21 +0200

You can try this
sort id age
gen weight2 = weight
replace weight2 = weight2[_n-1] if missing(weight2)

Alexis

Le 10 juin 2013 à 06:45, Ching Wong <[email protected]> a écrit :

> Hi,
> 
> I have a dataset as following:
> 
> id age weight
> 1   21   50.2
> 1   22
> 1   23   52.9
> 1   24   51.0
> 1   25
> 2   22
> 2   23
> 2   25   60.2
> 3   21
> 
> And I would like to create a new variable "weight2" and fill in the
> missing data based on the previous value
> 
> My expected output value should be as follows:
> 
> id age weight weight2
> 1   21   50.2     50.2
> 1   22       .       50.2
> 1   23   52.9     52.9
> 1   24   51.0     51.0
> 1   25      .        51.0
> 2   22      .            .
> 2   23      .            .
> 2   25   60.2     60.2
> 3   21      .            .
> 
> I have tried the command below but that cannot produce what I expected.
> 
> - bysort id (age): gen weight_hat = weight[_n-1]
> 
> It is very obvious that command is missing something. So what will be
> the correct command in this case?
> 
> Cheers,
> 
> Wong
> *
> *   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/

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