Announcement

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

  • More concise code?

    Dear All, According to the following data
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(id zzmagnitude) int zzinterval1
     1 0   .
     2 2 327
     3 0   .
     4 0   .
     5 0   .
     6 3 284
     7 0   .
     8 3 284
     9 0   .
    10 1 272
    end
    If original zzinterval = 327 and original zzmagnitude = 2, then re-set (change the variable input) the zzinterval to 30 and zzmagnitude = 0. A way to do this is
    Code:
    replace zzinterval1 = 30 if (zzinterval1==327)&(zzmagnitude==2)
    replace zzmagnitude = 0 if (zzinterval1==327)&(zzmagnitude==2)
    I wonder if there is a more concise way to do this. Thanks.
    Ho-Chuan (River) Huang
    Stata 17.0, MP(4)

  • #2
    Actually, the code you show will not do what you want. The second replace command will work incorrectly, because after the first command, zzinterval1 will no longer be 327 in the relevant observations. I think you actually need to be a bit more indirect:

    Code:
    gen byte to_change = (zzinterval1 == 327) & (zzmagnitud == 2)
    replace zzinterval1 = 30 if to_change
    replace zzmagnitude = 0 if to_change

    Comment


    • #3
      Dear Clyde, You got me (my bad). Thanks for the correct method.
      Ho-Chuan (River) Huang
      Stata 17.0, MP(4)

      Comment

      Working...
      X