Announcement

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

  • Adding a numerical variable for country dyads

    Dear all,

    I have a small issue I cannot seem to figure out.
    I have a panel dataset with country-level variables (e.g., exports in services).

    I am attaching a screenshot below.

    I am trying to add ISO numerical codes for each country (dyad) that I will need later on to match values with other datasets.

    How can I recode or add these codes as separate variables? In other words, for each country I would like to have a separate variable called "iso_num_a" that gives the ISO number of the reporting country and the "iso_num_b" that gives the ISO number of the partner country. I have the country identifiers but I would rather not do this manually...

    If you see the dataset below, essentially I would like to have a variable where AUT (Austria) will be equal to 40 and BEL (Belgium) will be equal to 56.

    The country variable (iso3ca) and the partner variable (iso3cb) are both string variables.

    I hope I explained this well...

    Any suggestions?
    Thanks,

    Aydin
    Click image for larger version

Name:	Screenshot 2022-03-09 at 14.07.09.png
Views:	1
Size:	163.8 KB
ID:	1653641


  • #2
    If you see the dataset below, essentially I would like to have a variable where AUT (Austria) will be equal to 40 and BEL (Belgium) will be equal to 56.
    If you have a list of numerical codes, then just merge this list with the dataset. See

    Code:
    help merge
    Otherwise, encode will assign numerical values to a string variable.

    I am trying to add ISO numerical codes for each country (dyad) that I will need later on to match values with other datasets.
    You can merge on string variables. Otherwise, as encode assigns numbers following alphabetical ordering, you will do more harm than good trying to merge on the encoded variable as the values may not match across datasets. In any case, here is how you do the encoding in a single dataset that guarantees that both variables will assign the same value to the same country.

    Code:
    expand 2, g(new)
    gen other= iso3cb
    replace iso3cb= iso3ca if new
    replace iso3ca= other if new
    encode iso3ca, g(iso_num_a)
    encode iso3cb, g(iso_num_b)
    drop if new
    drop new other
    Last edited by Andrew Musau; 09 Mar 2022, 08:21.

    Comment


    • #3
      See also multencode from SSC, which guarantees consistent coding. https://www.stata.com/statalist/arch.../msg00729.html

      Comment


      • #4
        Thank you so much!!

        Comment

        Working...
        X