Announcement

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

  • if two string variables are different


    Hi,

    I have variables "Dummy", "ctry1" and "ctry2" and expect to get "DifferentDummy" (table like below).
    Dummy ctry1 ctry2 DifferentDummy
    0 FR FR 0
    0 FR IE 0
    1 FR FR 0
    1 FR IE 1
    If Dummy=1, and "ctry1" does not equal "ctry2", then DifferentDummy=1,

    I used the following code,
    Code:
    gen DifferentDummy =0
    replace DifferentDummy =1 if (Dummy ==1) & ("`ctry1'"!=="` ctry2'")
    but failed(get the table likes below),
    Dummy ctry1 ctry2 DifferentDummy
    0 FR FR 0
    0 FR IE 0
    1 FR FR 1
    1 FR IE 1
    Could you please give me some advice about this?
    Many thanks in advance.

  • #2
    Code:
    gen DifferentDummy = (Dummy == 1) & (ctry1 != ctry2)
    There were two mistakes, using !== and using local macros where variable names give you a direct solution.

    See also

    https://www.stata.com/support/faqs/d...rue-and-false/

    https://www.stata-journal.com/articl...article=dm0099

    for more on why

    generate wanted = true or false condition

    helps you avoid that awkward two-step.


    Comment


    • #3
      Hi Nick Cox , Many thanks for your reply, it is a great help.

      Comment

      Working...
      X