Announcement

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

  • Creating dummy variable if this country is a member of APEC

    Hello,
    Everyone,
    I'm trying to find out a simple way to create a dummy variable in Stata. Normally, I just write this code: gen $a new variable name$=1 if($key indicator/variable$==&certain value&).

    However, this time, I'm using a panel data model. I have 141 sample countries. The sample period is 14 years.
    My research objective is to explore the determinants of inward foreign direct investment in China. One of the control variables is the economic association. I choose APEC and G-20 as dummy variables.

    I need your advice on how to create a dummy variable if the country is a member of APEC and G-20. Is there any easy way to do it?

    Currently, I only know the following way:

    gen APEC=1 if(country==Australia|country==Canada| and so on)
    replace APEC=0 if(country==I'm going to list all the non-APEC countries' name one by one).

    However, I realize that this method will create too much work and may cause a few mistakes. Do you know any easy way to create a dummy variable in panel data model?

    Thanks very much!

  • #2
    Chen:
    welcome to this forum.
    I have advice for the second line of your code:
    Code:
    replace APEC=0 if APEC==.
    Kind regards,
    Carlo
    (StataNow 18.5)

    Comment


    • #3
      The code should and could look more like

      Code:
      gen APEC = (country=="Australia")|(country=="Canada")
      as

      1. You don't need the generate, replace fandango. You can do it in one line. See 13.2.3 in https://www.stata.com/manuals/u.pdf or
      https://www.stata.com/support/faqs/d...rue-and-false/

      2. Literal names need double quotes.

      This is still tedious.

      See also

      Code:
      help inlist()
      but note limits on the number of its arguments.

      https://www.stata.com/support/faqs/d...s-for-subsets/ offers another way. Put the names of APEC countries as a single variable dataset, merge datasets and the intersection defines an indicator.

      I recommend the term indicator, not dummy, as other posts here explain.

      Comment


      • #4
        Originally posted by Carlo Lazzaro View Post
        Chen:
        welcome to this forum.
        I have advice for the second line of your code:
        Code:
        replace APEC=0 if APEC==.
        Thanks very much Carlo. Yes, it is really easy. Very good advice! I will do it!

        Comment


        • #5
          Originally posted by Nick Cox View Post
          The code should and could look more like

          Code:
          gen APEC = (country=="Australia")|(country=="Canada")
          as

          1. You don't need the generate, replace fandango. You can do it in one line. See 13.2.3 in https://www.stata.com/manuals/u.pdf or
          https://www.stata.com/support/faqs/d...rue-and-false/

          2. Literal names need double quotes.

          This is still tedious.

          See also

          Code:
          help inlist()
          but note limits on the number of its arguments.

          https://www.stata.com/support/faqs/d...s-for-subsets/ offers another way. Put the names of APEC countries as a single variable dataset, merge datasets and the intersection defines an indicator.

          I recommend the term indicator, not dummy, as other posts here explain.
          Thanks very much dear Nick! Thank you!

          Comment


          • #6
            Conclusion: I used idc as unique identifiers for countries to write codes. I also took advice from Carlo. Therefore, creating dummy variables has become easier.

            Comment

            Working...
            X