Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Generate flag variable

    Dear all,

    I've replaced the missing values on a variable using the Lag-2 as follows:

    Code:
    tsset idauniq year
    
    sort idauniq year
    
    replace eqtotinc_CPI18 = L2.eqtotinc_bu_s / CPI_base_2018  if missing(eqtotinc_CPI18)

    I would now like to generate a flag variable for when eqtotinc_CPI18 has been replaced using the lag.

    Thank you very much,
    Diana

  • #2
    Hi Diana,

    It would be easier to help if you shared a snippet of the dataset using dataex. Please read the FAQ (section 12) for more info on that.

    One option would be to clone eqtotinc_CPI18 before replacing it and them comparing the old and new eqtotinc_CPI18: whatever is different was replaced by your code. Something like:

    Code:
    clonevar eqtotinc_REF = eqtotinc_CPI18
    replace eqtotinc_CPI18 = L2.eqtotinc_bu_s / CPI_base_2018  if missing(eqtotinc_CPI18)
    gen flag = (eqtotinc_REF != eqtotinc_CPI18)
    Best;

    Comment


    • #3
      Generate the flag first, then replace the value.
      Code:
      generate flag = missing(eqtotinc_CPI18)
      replace eqtotinc_CPI18 = L2.eqtotinc_bu_s / CPI_base_2018  if flag

      Comment

      Working...
      X