Announcement

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

  • unintended behavior of -ereplace- when combined with an if statement

    I was using -ereplace- today with if statements. (See also its introduction on Statalist here.)

    I noticed that erplace was not only replacing values for observations that meet the conditions of the if statement (what I wanted); it was also changing all values of the variable in question to missing for all observations that did not meet the conditions of the if statement (not what I wanted!). This seems like unintended behavior, as -replace- doesn't do this and -ereplace- is intended to be identical to -replace- except that it works for egen commands.

    Here is a minimum working example loosely based on my dataset:

    Code:
    clear
    input float var1 str1 var2 float var3
    1 "A" 0
    2 "A" 0
    3 "A" 0
    4 "B" 0
    5 "B" 0
    6 "B" 0
    7 "C" 1
    8 "C" 1
    9 "C" 1
    10 "D" 1
    11 "D" 1
    12 "D" 1
    end
    Suppose I want to generate a new variable that follows one rule for those observations which take the value 0 on var3 and different rule for those observations that take the value 1.

    Code:
    egen var4=concat(var1 var2) if var3==0, punct("-")
    (6 missing values generated)
    My data now look like:
    var1 var2 var3 var4
    1 A 0 1-A
    2 A 0 2-A
    3 A 0 3-A
    4 B 0 4-A
    5 B 0 5-A
    6 B 0 6-A
    7 C 1
    8 C 1
    9 C 1
    10 D 1
    11 D 1
    12 D 1
    Now to replace the missing values of var4 with what I want for those observations which take on the value 1 for var3:

    Code:
    ssc install ereplace
    ereplace var4=concat(var1 var2) if var3==1, punct("-")
    (6 missing values generated)
    (12 real changes made)
    My data now look like:
    var1 var2 var3 var4
    1 A 0
    2 A 0
    3 A 0
    4 B 0
    5 B 0
    6 B 0
    7 C 1 7-C-1
    8 C 1 8-C-1
    9 C 1 9-C-1
    10 D 1 10-D-1
    11 D 1 11-D-1
    12 D 1 12-D-1
    Why did -ereplace- modify var4 for obs. 1 through 6? Is this intended behavior of -ereplace- or a bug?

    I am assuming only the authors will be able to answer this question, but if anyone knows if I'm using -ereplace- wrong, your advice is appreciated.

    (I have already found an alternate approach that solves my problem, so no need to suggest alternate solutions.)
Working...
X