Announcement

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

  • creating a variable number of suppliers

    Hello everyone,
    I have a dataset with France as an importer and the EU countries as exporters. My dataset is at product level hs6 and covers 13 years from 2003 to 2015. Finally, I have v the value of imported product of a given year from a given country.
    I want to create a variable a variable: number of suppliers
    for a given product i.e the number of countries from which France has imported a product in a given year.
    I'd really appreciate your help for the code
    Thank you very much

  • #2
    It is much easier to give concrete advice when you give an example of your data. You have left it to me to imagine what your variable names are and how they are organized.

    I'm going to assume that you have a variable called country which gives the name of the exporter, a variable called product that names or encodes the product, and another variable called year. I also assume that if in a given year a particular country did not export any of a given product to France, then there is no observation in the data set for that country-year-product combination. I also assume that you never have two entries for the same combination of exporter, product, and year: it is all summed up in a single observation.

    Code:
    //    VERIFY ONLY ONE OBSERVATION PER PRODUCT
    //    YEAR COUNTRY COMBINATION
    isid product year country, sort
    by product year: gen n_suppliers = _N
    If these assumptions do not adequately describe your data and you do not know how to transform your data so that they do, and you don't know how to modify the code so that it conforms to the data you have, feel free to post back. But in doing so, don't give us descriptions: show a sample of your data, and do it using the -dataex- command. Please read FAQ #12 for complete instructions on how to do that.

    Comment


    • #3
      Hello Dorra,

      Welcome to the Stata Forum / Statalist.

      Unfortunately, you didn't present a display of the dataset (as recommend in the FAQ), which is the best way to get a helpful advice.

      That said, you may wish to fiddle with something like:

      Code:
      . egen myvar = total(suppliers), by(country year)
      P.S: crossed with Clyde's reply.
      Best regards,

      Marcos

      Comment


      • #4
        Originally posted by Clyde Schechter View Post
        It is much easier to give concrete advice when you give an example of your data. You have left it to me to imagine what your variable names are and how they are organized.

        I'm going to assume that you have a variable called country which gives the name of the exporter, a variable called product that names or encodes the product, and another variable called year. I also assume that if in a given year a particular country did not export any of a given product to France, then there is no observation in the data set for that country-year-product combination. I also assume that you never have two entries for the same combination of exporter, product, and year: it is all summed up in a single observation.

        Code:
        // VERIFY ONLY ONE OBSERVATION PER PRODUCT
        // YEAR COUNTRY COMBINATION
        isid product year country, sort
        by product year: gen n_suppliers = _N
        If these assumptions do not adequately describe your data and you do not know how to transform your data so that they do, and you don't know how to modify the code so that it conforms to the data you have, feel free to post back. But in doing so, don't give us descriptions: show a sample of your data, and do it using the -dataex- command. Please read FAQ #12 for complete instructions on how to do that.
        Hello, thank you for your answer. I am a new user of Stata. I am sorry if it's not clear. This is an example of my dataset.
        i is the exporter (EU country)
        j is France the importer
        hs6: is the product code at 6 digit level
        q: is the quantity
        As i said before,I want to create a variable a variable: number of suppliers for a given product i.e the number of countries from which France has imported a product in a given year
        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input int(t i j) str6 hs6 float q
        2014 12 251 "100110" 8.982372
        2014 12 251 "100190"   17.368
        2009 12 251 "100630"       20
        2013 12 251 "100630"       52
        2010 12 251 "100640"        5
        end
        Last edited by DORRA SELLAMI; 26 Mar 2017, 16:52.

        Comment


        • #5
          So your data match what I described, except that you use different names. All you have to do is replace my variable names by the corresponding variables in your data set:

          Code:
          isid hs6 t i, sort
          by hs6 t: gen n_suppliers = _N

          Comment


          • #6
            Originally posted by Clyde Schechter View Post
            So your data match what I described, except that you use different names. All you have to do is replace my variable names by the corresponding variables in your data set:

            Code:
            isid hs6 t i, sort
            by hs6 t: gen n_suppliers = _N
            Hello Mr Clyde,
            Thank you again for your answer. I run the code and it worked. Thank you very very much :D
            Last edited by DORRA SELLAMI; 27 Mar 2017, 03:09.

            Comment


            • #7
              Originally posted by Marcos Almeida View Post
              Hello Dorra,

              Welcome to the Stata Forum / Statalist.

              Unfortunately, you didn't present a display of the dataset (as recommend in the FAQ), which is the best way to get a helpful advice.

              That said, you may wish to fiddle with something like:

              Code:
              . egen myvar = total(suppliers), by(country year)
              P.S: crossed with Clyde's reply.
              Hello, thank you for your answer. it worked :D thank you very much

              Comment


              • #8
                Thank you for informing your query reached it closure satisfactorily.
                Best regards,

                Marcos

                Comment

                Working...
                X