Announcement

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

  • How to create a dummy variable for countries with X characteristic in panel data

    Hello there, I am a beginner in Stata and I working with panel data for a gravity equation.

    I want to create a landlocked dummy variable so I know that I should generate a new variable and replace the values to 1= if the country is landlocked, but I don't know how to specify this in Stata, that those countries should take the value of 1.
    gen landlocked = 0
    replace landlocked=1 if ?????

    For instance, I am using iso3 code names
    AFG ARM AUT AZE BFA BDI BOL BTN BWA CAF CHE CZE ETH HUN KGZ KAZ LAO LSO LUX MKD MLI MNG MWI NER NPL PRY RWA SVK SWZ TCD TJK TKM UGA UZB ZMB ZWE

    Any ideas of what should I do? Thanks!

  • #2
    The same question was asked just yesterday! https://www.statalist.org/forums/for...ame-conditions

    See Andrew Musau's nice answer and FAQ: https://www.stata.com/support/faqs/d...s-for-subsets/
    Last edited by Nick Cox; 17 Apr 2018, 08:18.

    Comment


    • #3
      Welcome to Statalist.

      How do you know which ISO3 country codes are for landlocked countries? Do you have a list?

      If we suppose that you have a variable in your data named Country containing the ISO3 country code for the observation, and if we suppose that HUN and LUX and SWZ are landlocked countries (I know nothing about ISO3 country codes and am just guessing), then
      Code:
      replace landlocked=1 if inlist(Country,"HUN","LUX","SWZ")
      will take care of those three codes. You may need multiple replace commands because the inlist function is limited to 10 arguments when they are strings.

      For your future posts, please review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question.

      The more you help others understand your problem, the more likely others are to be able to help you solve your problem.

      Comment


      • #4
        William Lisowski Yeah I have a list, actually, I have another dataset with a dummy variable for countries that are landlocked and I tried to merge it but I had an error message:
        merge m:1 iso_o using landlock_origin
        variable iso_o does not uniquely identify observations in the using data
        r(459);
        I think it's because the second dataset doesn't include a year variable, Thank you so much, I didn't find a lot of information about dealing with string variables.

        Nick Cox Thanks, but I think my problem was that I didn't know how to deal with string variables, sorry I will be more specific next time

        Comment


        • #5
          As Nick suggests, using merge is the best approach when you already have a dataset with the landlocked countries. I would not have suggested my approach if post #1 had presented the problem more fully.

          The merge m:1 output is telling you that your landlock_origin dataset has more than one observation for some countries (that is, some values of iso_o). This has nothing to do with whether or not it has a year variable.

          If you were to better explain what the landlock_origin dataset is like, and provide a sample using dataex (see the FAQ I referred to in post #3) we could perhaps explain what you need to do for merge to work.

          You can find the duplicated observations using the duplicates command, see
          Code:
          help duplicates
          for details, and try
          Code:
          duplicates list
          hoping that maybe just a few countries are duplicated so the listing is not too long.

          Comment


          • #6
            This just a preview how my main dataset looks like (I am working with bilateral trade flows)


            [CODE]
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input str3 iso str10 importer int year float(XPTOT_o XPTOT_d)
            "ABW" "GRL" 1990 13532721 362018496
            "ABW" "CAN" 1990 13532721 72905940992
            "ABW" "STP" 1990 13532721 16378729
            "ABW" "GUY" 1990 13532721 149123632
            "ABW" "LCA" 1990 13532721 90291760
            "ABW" "CMR" 1990 13532721 1319049728
            "ABW" "BHS" 1990 13532721 614887104
            "ABW" "SWE" 1990 13532721 32862951424
            "ABW" "FRA" 1990 13532721 121157386240
            "ABW" "GLP" 1990 13532721 74624672
            "ABW" "TON" 1990 13532721 7207089
            "ABW" "SOM" 1990 13532721 .
            "ABW" "SLB" 1990 13532721 44938696
            "ABW" "AUT" 1990 13532721 23695460352
            "ABW" "ARG" 1990 13532721 7494021120
            "ABW" "GBR" 1990 13532721 104247443456

            this is the dataset I want to merge

            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input float landlocked str3 iso
            0 "AND"
            0 "ARE"
            1 "AFG"
            0 "ATG"
            0 "AIA"
            0 "ALB"
            1 "ARM"
            0 "ANT"
            0 "AGO"
            0 "ARG"
            1 "AUT"
            0 "AUS"
            0 "AUS"
            0 "ABW"
            1 "AZE"
            0 "BIH"

            I used the duplicates command and I got this outcome, but some countries are duplicated because I am using data panel.

            . duplicates list

            Duplicates in terms of all variables

            (0 observations are duplicates)

            Thanks.

            Comment


            • #7
              It doesn't even take the duplicates command to see that you have a duplicated observation in your using (landlock) data.

              Here is some code that detects and discards duplicated observations, and then merges the landlock data onto the master data. I have added one extra observation to the master data to demonstrate what happens if there is an iso in the master data that is not in the landlock data.
              Code:
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input str3 iso str10 importer int year float(XPTOT_o XPTOT_d)
              "ABW" "GRL" 1990 13532721 362018496
              "ABW" "CAN" 1990 13532721 72905940992
              "ABW" "STP" 1990 13532721 16378729
              "ABW" "GUY" 1990 13532721 149123632
              "ABW" "LCA" 1990 13532721 90291760
              "ABW" "CMR" 1990 13532721 1319049728
              "ABW" "BHS" 1990 13532721 614887104
              "ABW" "SWE" 1990 13532721 32862951424
              "ABW" "FRA" 1990 13532721 121157386240
              "ABW" "GLP" 1990 13532721 74624672
              "ABW" "TON" 1990 13532721 7207089
              "ABW" "SOM" 1990 13532721 .
              "ABW" "SLB" 1990 13532721 44938696
              "ABW" "AUT" 1990 13532721 23695460352
              "ABW" "ARG" 1990 13532721 7494021120
              "ABW" "GBR" 1990 13532721 104247443456
              "ZZZ" "YYY" 1990 1        1
              end
              tempfile master
              save `master'
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input float landlocked str3 iso
              0 "AND"
              0 "ARE"
              1 "AFG"
              0 "ATG"
              0 "AIA"
              0 "ALB"
              1 "ARM"
              0 "ANT"
              0 "AGO"
              0 "ARG"
              1 "AUT"
              0 "AUS"
              0 "AUS"
              0 "ABW"
              1 "AZE"
              0 "BIH"
              end
              duplicates list iso landlocked
              duplicates drop iso landlocked, force
              // confirm that there is at most one observation per iso
              // this will fail if the same iso appears more than once
              //    with different values of landlocked
              // if that happens you have to decide which is correct
              isid iso
              tempfile llo
              save `llo'
              use `master', clear
              merge m:1 iso using `llo', keep(master match)
              list, clean
              Here is the interesting part of the results.
              Code:
              . duplicates list iso landlocked
              
              Duplicates in terms of iso landlocked
              
                +-----------------------+
                | obs:   iso   landlo~d |
                |-----------------------|
                |   12   AUS          0 |
                |   13   AUS          0 |
                +-----------------------+
              
              . duplicates drop iso landlocked, force
              
              Duplicates in terms of iso landlocked
              
              (1 observation deleted)
              
              . // confirm that there is at most one observation per iso
              . // this will fail if the same iso appears more than once
              . //    with different values of landlocked
              . // if that happens you have to decide which is correct
              . isid iso
              
              . tempfile llo
              
              . save `llo'
              file /var/folders/xr/lm5ccr996k7dspxs35yqzyt80000gp/T//S_09584.000002 saved
              
              . use `master', clear
              
              . merge m:1 iso using `llo', keep(master match)
              
                  Result                           # of obs.
                  -----------------------------------------
                  not matched                             1
                      from master                         1  (_merge==1)
                      from using                          0  (_merge==2)
              
                  matched                                16  (_merge==3)
                  -----------------------------------------
              
              . list, clean
              
                     iso   importer   year    XPTOT_o    XPTOT_d   landlo~d            _merge  
                1.   ABW        GRL   1990   1.35e+07   3.62e+08          0       matched (3)  
                2.   ABW        CAN   1990   1.35e+07   7.29e+10          0       matched (3)  
                3.   ABW        STP   1990   1.35e+07   1.64e+07          0       matched (3)  
                4.   ABW        GUY   1990   1.35e+07   1.49e+08          0       matched (3)  
                5.   ABW        LCA   1990   1.35e+07   9.03e+07          0       matched (3)  
                6.   ABW        CMR   1990   1.35e+07   1.32e+09          0       matched (3)  
                7.   ABW        BHS   1990   1.35e+07   6.15e+08          0       matched (3)  
                8.   ABW        SWE   1990   1.35e+07   3.29e+10          0       matched (3)  
                9.   ABW        FRA   1990   1.35e+07   1.21e+11          0       matched (3)  
               10.   ABW        GLP   1990   1.35e+07   7.46e+07          0       matched (3)  
               11.   ABW        TON   1990   1.35e+07    7207089          0       matched (3)  
               12.   ABW        SOM   1990   1.35e+07          .          0       matched (3)  
               13.   ABW        SLB   1990   1.35e+07   4.49e+07          0       matched (3)  
               14.   ABW        AUT   1990   1.35e+07   2.37e+10          0       matched (3)  
               15.   ABW        ARG   1990   1.35e+07   7.49e+09          0       matched (3)  
               16.   ABW        GBR   1990   1.35e+07   1.04e+11          0       matched (3)  
               17.   ZZZ        YYY   1990          1          1          .   master only (1)

              Comment

              Working...
              X