Announcement

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

  • Creating a Variable that Returns the Value of Other Variable(s) if Conditions are met

    Hello again,

    I am facing this issue in my research project. I need to add some data to my base data set, which will be used as an instrumental variable in my regression. Basically, I have annual bilateral import-export data. My IV data identifies bilateral currency pegging: that is, there are a few countries (in my example below, just the US and Great Britain) which are considered currency "leaders", while the rest of the countries are potentially currency "followers"; they peg their currency to the leader's. If there is a follower-leader relationship, the "peg" variable = 1, else = 0.

    In my main data set though, since trade flows both ways, there are two entries per country pair (i.e. USA -> ARG, then ARG -> USA). I am trying to create the "leader" variable, which will equal the value of the leader country, only if the pair contains a potential leader country (USA or GBR), then the "follower" variable will just be equal to the other country in the pair. I have tried using inlist and cond to generate this variable, but I don't think they allow for these types of conditions. See the sample data below:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int year str3(country_exp country_imp) str7 countries str1(leader follower)
    2000 "USA" "ARG" "USA_ARG" "." "."
    2000 "ARG" "USA" "ARG_USA" "." "."
    2000 "GBR" "CHN" "GBR_CHN" "." "."
    2000 "CHN" "GBR" "CHN_GBR" "." "."
    end
    In my IV data, in the year 2000, Argentina pegged their currency to the US dollar. In order to merge the data though, I believe I need these "follower" and "leader" variables in my master data. I am also not restricting myself to only use this method; I am open to changing my IV data format if creating this variable in my master data is impossible. Below is a very crude attempt I made at making these variables, which of course, does not work.

    Code:
    gen leader = cond(country_exp == "USA" | "GBR" | country_imp == "USA" | "GBR", `country_exp' | `country_imp')

  • #2
    How about this?
    Code:
    replace leader = cond(inlist(country_exp,"USA","GBR"), country_exp, cond(inlist(country_imp,"USA","GBR"), country_imp, ""))
    replace follower = cond(leader == country_exp, country_imp, cond(leader == country_imp, country_exp, ""))
    Last edited by Hemanshu Kumar; 03 Sep 2022, 15:11.

    Comment


    • #3
      Originally posted by Hemanshu Kumar View Post
      How about this?
      Code:
      replace leader = cond(inlist(country_exp,"USA","GBR"), country_exp, cond(inlist(country_imp,"USA","GBR"), country_imp, ""))
      replace follower = cond(leader == country_exp, country_imp, cond(leader == country_imp, country_exp, ""))
      Thanks for your help Hemanshu; greatly appreciated. Unfortunately, I receive an type mismatch error, since I believe the cond command expects numerical, not string values. Correct me if I am wrong, as I am still relatively new to STATA.
      Last edited by Andrew Bernal; 06 Sep 2022, 11:41.

      Comment


      • #4
        Two notes -- first, Hemanshu's code works fine for me. I believe your type mismatch error is a result of your leader and follower variables having mistakenly been created as numeric rather than string. Try using the following to convert the variables to string first:

        Code:
        tostring leader follower, replace
        Second, here is an alternative regex solution which is robust to a higher number of leader countries (inlist() is capped at 10 string matches). To add more countries, simply expand the list in the first line (currently USA|GBR) using the pipe character (|) as a delimiter:

        Code:
        replace leader = ustrregexra(countries,"^(.*?_)?(USA|GBR)(_.*?)?$","$2")
        replace follower = ustrregexra(countries,"_?"+leader+"_?","")

        Comment


        • #5
          Wow, very clever! I stand corrected, thank you both very much.

          Comment

          Working...
          X