Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, is already up and running.

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

st: difficulty recoding variable by referring to prior and subsequent lines in panel data

From   Alison El Ayadi <>
To   "" <>
Subject   st: difficulty recoding variable by referring to prior and subsequent lines in panel data
Date   Thu, 7 Mar 2013 19:27:14 -0800 (PST)

Hi everyone - 

I am having an issue where I am working with a binary variable in panel data and want to recode the variable at a particular time point to the opposite condition if the value at the prior and subsequent time points are equal to each other but not equal to the reference time point.  

I initially used this code:
  *tsset data
sort pt_id index_cl index_rh
         by pt_id: replace n_num = _n
         sort pt_id n_num
         tsset pt_id n_num

   *take care of 'random' differences
    gen low_index_recode_ind = 1 if (low_index!=low_index[_n+1]) & (low_index!=low_index[_n-1]) & (low_index[_n-1]==low_index[_ n+1]) & low_index!=. & low_index[_n-1]!=. 
gen low_index_new = low_index
         replace low_index_new = 1 if low_index_recode_ind==1 & low_index==0
         \replace low_index_new = 0 if low_index_recode_ind==1 & low_index==1

And I found that when recoding a number of first and last lines were recoded.  So I made a change to include conditions in bold:
 gen low_index_recode_ind = 1 if (low_index!=low_index[_n+1]) & (low_index!=low_index[_n-1]) & (low_index[_n-1]==low_index[_n+1]) & low_index!=. & low_index[_n-1]!=. & n_num!=1 & n_num!=_N     
However I am still finding that there are at least several final lines that have been recoded.

I understand by tsset-ing the data Stata would not be comparing the lines belonging to one pt_id to lines from the next pt_id in dataset sequence, am I correct in this assumption, and if so, does anyone see any mistakes in my code which will allow this change to operate successfully as intended for all lines, not just middle lines?

Thanks so much in advance - 
Alison El Ayadi                                               

*   For searches and help try:

© Copyright 1996–2016 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index