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 can I get the second last non-missing value?


From   Rebecca Pope <[email protected]>
To   [email protected]
Subject   Re: st: How can I get the second last non-missing value?
Date   Wed, 12 Jun 2013 16:01:02 -0500

Duygu,
I think this will be easier with your data in long form.

*** begin example ***
clear
input id v1 v2 v3 v4 v5
  1 1  2  3  .  .
  2 1  2  .  .  .
  3 1  2  3  4  .
 end

reshape long v, i(id) j(num)
keep if ! missing(v)
bys id (num): gen v6 = v[_N-1]
list, noobs clean
reshape wide v, i(id) j(num)
list, noobs clean

*** end ***

In this case, you lose v5, but presumably in your real data that
variable is not universally missing. If you don't have a constant
prefix for the variables in your dataset, you might want to check out
-rename- and look at the methods for renaming groups. Standing advice:
test this on a copy of your data rather than the original data.

Regards,
Rebecca

On Wed, Jun 12, 2013 at 3:27 PM, duygu yıldırım <[email protected]> wrote:
> Hello everyone,
>
> I use a cross-sectional individual level data set. I want to ask if there is a way that I can create a variable which consists the second last non-missing value within observations?
>
> for example;
>
> id v1 v2 v3 v4 v5
>  1 1  2  3  .  .
>  2 1  2  .  .  .
>  3 1  2  3  4  .
>
> So the variable that I want to create is;
>
> id  v6
> 1   2
> 2   1
> 3   3
>
> I can get the last non-missing value within observation by using the rowlast command, but I also need the one just before the last one.
>
> Thank you all very much for any help,
>
> Duygu.
>
> *
> *   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