Announcement

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

  • Creating Binary variable when two variables are the same

    Hello all,

    I would like to create a binary variable that indicates if any particular observation is a subsidiary. When this is not the case, Ticker = ParentTicker.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str26 Ticker str32 ParentTicker
    ""                 ""                
    ""                 ""                
    "000660 KS EQUITY" "000660 KS EQUITY"
    "000660 KS EQUITY" "000660 KS EQUITY"
    "000660 KS EQUITY" "000660 KS EQUITY"
    "HNSZ CH EQUITY"   "000660 KS EQUITY"
    "HNSZ CH EQUITY"   "000660 KS EQUITY"
    "HNSZ CH EQUITY"   "000660 KS EQUITY"
    end
    My attempt was simple, and unsurprisingly did not work

    gen subsidiary = 1

    replace subsidiary = 0 if ParentTicker = Ticker


    Let me know if there is an easy workaround for this.

    Thanks,
    Kayleigh

  • #2
    *EDIT
    I fixed the code for this, as below.

    gen subsidiary = 1

    replace subsidiary = 0 if ParentTicker == Ticker

    Comment


    • #3
      replace subsidiary = 0 if ParentTicker = Ticker
      Here, you need a double equal sign "==", but directly, to create a binary variable, you can use


      Code:
      gen wanted= ParentTicker! = Ticker & !missing(Ticker) & !missing(ParentTicker)
      The negation operator is "!" or "~". See

      Code:
      help operators

      Comment


      • #4
        Andrew Musau got to the nub of the matter. Note that only one line is needed.

        Here I add some links spelling out some context and further principles and example:

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

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

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

        Comment

        Working...
        X