Announcement

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

  • Create a RANK variable by country, time and technological class

    Hello there,

    I need to create a rank variable that follows this logic "In this paper, the share of patents held by the four
    major innovators within each technological class is used (C4)" from Malerba and Orsenigo 1995. Thus I need to rank each country in each year by its number of patents in each technological class.
    I already looked all the threads concerning a similar problem, looked at the
    Code:
    -egen- rank
    function by it doesn't seem to be my case or maybe I am missing something here.

    I have 6 countries, so I need a rank that goes from 1 to 6, using other functions or codes like in
    HTML Code:
    https://www.statalist.org/forums/forum/general-stata-discussion/general/1349895-keeping-variables-with-the-highest-three-values
    I got rank up to 20 which evidently is not the case

    Can somebody tell me how can I rank countries by their number of patents in each technological class for each year? or at least rank each country by technological class.

    here a snapshot of my data sourced from OECD.

    Thanks a lot,
    Dalila Ribaudo


    Code:
    ----------------------- copy starting from the next line -----------------------
    
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str75 country int time str67 tech_class_1dig float nr_pat
    "United Kingdom" 2018 "IPC D: Textiles; Paper"                                                17.3366
    "United Kingdom" 2017 "IPC D: Textiles; Paper"                                                28.9404
    "United Kingdom" 2015 "IPC D: Textiles; Paper"                                                31.0263
    "France"         2018 "IPC D: Textiles; Paper"                                                31.8852
    "United Kingdom" 2016 "IPC D: Textiles; Paper"                                                35.9336
    "Italy"          2018 "IPC D: Textiles; Paper"                                                57.8329
    "France"         2015 "IPC D: Textiles; Paper"                                                 69.577
    "France"         2016 "IPC D: Textiles; Paper"                                                72.9793
    "France"         2017 "IPC D: Textiles; Paper"                                                86.7161
    "Italy"          2017 "IPC D: Textiles; Paper"                                               105.8811
    "Italy"          2016 "IPC D: Textiles; Paper"                                               111.6228
    "Italy"          2015 "IPC D: Textiles; Paper"                                                 122.51
    "Italy"          2018 "IPC C: Chemistry; Metallurgy"                                         123.3688
    "United Kingdom" 2018 "IPC E: Fixed Constructions"                                           124.5273
    "United States"  2018 "IPC D: Textiles; Paper"                                               137.4295
    "France"         2018 "IPC E: Fixed Constructions"                                           170.3693
    "Italy"          2018 "IPC E: Fixed Constructions"                                           176.7254
    "Italy"          2015 "IPC E: Fixed Constructions"                                           211.9131
    "United Kingdom" 2018 "IPC C: Chemistry; Metallurgy"                                         227.2309
    "Italy"          2017 "IPC E: Fixed Constructions"                                            230.902
    "Germany"        2018 "IPC D: Textiles; Paper"                                               242.0335
    "United States"  2015 "IPC D: Textiles; Paper"                                               246.1344
    "United Kingdom" 2017 "IPC E: Fixed Constructions"                                           248.3734
    "United States"  2017 "IPC D: Textiles; Paper"                                               260.7929
    "United Kingdom" 2016 "IPC E: Fixed Constructions"                                           261.4271
    "Italy"          2016 "IPC E: Fixed Constructions"                                           262.5896
    "United States"  2016 "IPC D: Textiles; Paper"                                                 266.32
    "France"         2016 "IPC E: Fixed Constructions"                                           278.8079
    "France"         2015 "IPC E: Fixed Constructions"                                           291.5966
    "United Kingdom" 2015 "IPC E: Fixed Constructions"                                           293.0544
    "France"         2017 "IPC E: Fixed Constructions"                                            301.555
    "Germany"        2017 "IPC D: Textiles; Paper"                                               313.9293
    "France"         2018 "IPC C: Chemistry; Metallurgy"                                         319.1765
    "Italy"          2018 "IPC H: Electricity"                                                   321.4232
    "Italy"          2015 "IPC C: Chemistry; Metallurgy"                                         329.0085
    "Italy"          2018 "IPC G: Physics"                                                       337.0337
    "Italy"          2017 "IPC C: Chemistry; Metallurgy"                                          341.176
    "Germany"        2016 "IPC D: Textiles; Paper"                                               343.5705
    "Italy"          2016 "IPC C: Chemistry; Metallurgy"                                         348.6883
    "Germany"        2015 "IPC D: Textiles; Paper"                                               352.5942
    "Italy"          2018 "IPC F: Mechanical Engineering; Lighting; Heating; Weapons; Blasting"  376.6061
    "Italy"          2015 "IPC H: Electricity"                                                   421.8856
    "Italy"          2017 "IPC H: Electricity"                                                   435.3698
    "Italy"          2015 "IPC G: Physics"                                                       438.8865
    "Japan"          2018 "IPC E: Fixed Constructions"                                           472.8951
    "Japan"          2018 "IPC D: Textiles; Paper"                                               475.1555
    "Italy"          2016 "IPC G: Physics"                                                       476.2838
    "Italy"          2018 "IPC A: Human Necessities"                                             476.5641
    "United Kingdom" 2018 "IPC F: Mechanical Engineering; Lighting; Heating; Weapons; Blasting"  497.4443
    "United Kingdom" 2018 "IPC A: Human Necessities"                                             507.8799
    "Italy"          2017 "IPC G: Physics"                                                       522.9263
    "Italy"          2016 "IPC H: Electricity"                                                    543.896
    "Japan"          2016 "IPC D: Textiles; Paper"                                               554.4405
    "Japan"          2015 "IPC D: Textiles; Paper"                                               571.3907
    "United Kingdom" 2018 "IPC B: Performing Operations; Transporting"                           574.3645
    "France"         2018 "IPC A: Human Necessities"                                             576.9227
    "United Kingdom" 2017 "IPC C: Chemistry; Metallurgy"                                         581.2652
    "Japan"          2016 "IPC E: Fixed Constructions"                                           591.1311
    "Japan"          2015 "IPC E: Fixed Constructions"                                           603.3491
    "Italy"          2017 "IPC F: Mechanical Engineering; Lighting; Heating; Weapons; Blasting"  618.1938
    "United Kingdom" 2018 "IPC H: Electricity"                                                   618.6688
    "Japan"          2017 "IPC D: Textiles; Paper"                                               627.3081
    "United Kingdom" 2016 "IPC C: Chemistry; Metallurgy"                                         629.2444
    "Japan"          2017 "IPC E: Fixed Constructions"                                           635.5335
    "Italy"          2016 "IPC F: Mechanical Engineering; Lighting; Heating; Weapons; Blasting"  664.6117
    "Italy"          2015 "IPC F: Mechanical Engineering; Lighting; Heating; Weapons; Blasting"  667.2909
    "United Kingdom" 2017 "IPC F: Mechanical Engineering; Lighting; Heating; Weapons; Blasting"  669.1438
    "United Kingdom" 2015 "IPC C: Chemistry; Metallurgy"                                         694.9429
    "Germany"        2018 "IPC E: Fixed Constructions"                                           696.0637
    "United Kingdom" 2015 "IPC F: Mechanical Engineering; Lighting; Heating; Weapons; Blasting"  707.9298
    "United Kingdom" 2016 "IPC F: Mechanical Engineering; Lighting; Heating; Weapons; Blasting"  718.7183
    "France"         2018 "IPC F: Mechanical Engineering; Lighting; Heating; Weapons; Blasting"  723.4854
    "Italy"          2018 "IPC B: Performing Operations; Transporting"                           774.9451
    "Italy"          2015 "IPC A: Human Necessities"                                             842.7225
    "France"         2016 "IPC C: Chemistry; Metallurgy"                                         852.4978
    "Germany"        2015 "IPC E: Fixed Constructions"                                           858.2121
    "United States"  2018 "IPC E: Fixed Constructions"                                           883.0867
    "United Kingdom" 2018 "IPC G: Physics"                                                       888.5177
    "Germany"        2018 "IPC C: Chemistry; Metallurgy"                                         888.6639
    "Italy"          2017 "IPC A: Human Necessities"                                             919.1489
    "Germany"        2017 "IPC E: Fixed Constructions"                                           946.7127
    "France"         2015 "IPC C: Chemistry; Metallurgy"                                         947.0167
    "France"         2017 "IPC C: Chemistry; Metallurgy"                                         947.9912
    "United Kingdom" 2016 "IPC B: Performing Operations; Transporting"                           962.3157
    "Italy"          2016 "IPC A: Human Necessities"                                             989.2037
    "Germany"        2016 "IPC E: Fixed Constructions"                                            991.714
    "United Kingdom" 2017 "IPC B: Performing Operations; Transporting"                          1002.6637
    "United Kingdom" 2015 "IPC B: Performing Operations; Transporting"                          1010.9802
    "France"         2018 "IPC G: Physics"                                                      1030.4623
    "Italy"          2015 "IPC B: Performing Operations; Transporting"                          1075.2632
    "United Kingdom" 2017 "IPC H: Electricity"                                                   1117.382
    "United Kingdom" 2016 "IPC H: Electricity"                                                  1133.6638
    "France"         2018 "IPC H: Electricity"                                                  1134.0883
    "France"         2018 "IPC B: Performing Operations; Transporting"                           1135.279
    "France"         2016 "IPC F: Mechanical Engineering; Lighting; Heating; Weapons; Blasting" 1207.3523
    "United Kingdom" 2015 "IPC H: Electricity"                                                  1213.8413
    "United Kingdom" 2017 "IPC A: Human Necessities"                                             1215.051
    "United Kingdom" 2015 "IPC A: Human Necessities"                                            1222.4565
    "United Kingdom" 2016 "IPC A: Human Necessities"                                            1238.7816
    "France"         2017 "IPC F: Mechanical Engineering; Lighting; Heating; Weapons; Blasting"  1257.149
    end

  • #2
    I have come up with these solutions -

    * Convert tech_class to numerical
    encode tech_class, g(class)

    * Ascending order
    capture drop rank
    bysort class time (nr_pat): g rank = _n

    * Discending order
    g ne_nrpat = -nr_pat
    bysort class time (ne_nrpat): g rank = _n

    Comment


    • #3
      Thanks for the data example. In practice the data appear to be singletons in respect of country, year and tech class, so I peppered up the example by adding some duplicates.

      Ranking does apply here -- but it's to the frequencies -- which must be calculated first -- and each country must be entered into the ranking just once, and then the rank must be spread to any other observations with the same time, country and tech class. A little tricky, I agree.

      If you want a different ranking rule, it's probably an option within egen.

      Code:
       
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input str75 country int time str67 tech_class_1dig float nr_pat
      "United Kingdom" 2018 "IPC D: Textiles; Paper"                                                17.3366
      "United Kingdom" 2017 "IPC D: Textiles; Paper"                                                28.9404
      "United Kingdom" 2015 "IPC D: Textiles; Paper"                                                31.0263
      "France"         2018 "IPC D: Textiles; Paper"                                                31.8852
      "United Kingdom" 2016 "IPC D: Textiles; Paper"                                                35.9336
      "Italy"          2018 "IPC D: Textiles; Paper"                                                57.8329
      "France"         2015 "IPC D: Textiles; Paper"                                                 69.577
      "France"         2016 "IPC D: Textiles; Paper"                                                72.9793
      "France"         2017 "IPC D: Textiles; Paper"                                                86.7161
      "Italy"          2017 "IPC D: Textiles; Paper"                                               105.8811
      "Italy"          2016 "IPC D: Textiles; Paper"                                               111.6228
      "Italy"          2015 "IPC D: Textiles; Paper"                                                 122.51
      "Italy"          2018 "IPC C: Chemistry; Metallurgy"                                         123.3688
      "United Kingdom" 2018 "IPC E: Fixed Constructions"                                           124.5273
      "United States"  2018 "IPC D: Textiles; Paper"                                               137.4295
      "France"         2018 "IPC E: Fixed Constructions"                                           170.3693
      "Italy"          2018 "IPC E: Fixed Constructions"                                           176.7254
      "Italy"          2015 "IPC E: Fixed Constructions"                                           211.9131
      "United Kingdom" 2018 "IPC C: Chemistry; Metallurgy"                                         227.2309
      "Italy"          2017 "IPC E: Fixed Constructions"                                            230.902
      "Germany"        2018 "IPC D: Textiles; Paper"                                               242.0335
      "United States"  2015 "IPC D: Textiles; Paper"                                               246.1344
      "United Kingdom" 2017 "IPC E: Fixed Constructions"                                           248.3734
      "United States"  2017 "IPC D: Textiles; Paper"                                               260.7929
      "United Kingdom" 2016 "IPC E: Fixed Constructions"                                           261.4271
      "Italy"          2016 "IPC E: Fixed Constructions"                                           262.5896
      "United States"  2016 "IPC D: Textiles; Paper"                                                 266.32
      "France"         2016 "IPC E: Fixed Constructions"                                           278.8079
      "France"         2015 "IPC E: Fixed Constructions"                                           291.5966
      "United Kingdom" 2015 "IPC E: Fixed Constructions"                                           293.0544
      "France"         2017 "IPC E: Fixed Constructions"                                            301.555
      "Germany"        2017 "IPC D: Textiles; Paper"                                               313.9293
      "France"         2018 "IPC C: Chemistry; Metallurgy"                                         319.1765
      "Italy"          2018 "IPC H: Electricity"                                                   321.4232
      "Italy"          2015 "IPC C: Chemistry; Metallurgy"                                         329.0085
      "Italy"          2018 "IPC G: Physics"                                                       337.0337
      "Italy"          2017 "IPC C: Chemistry; Metallurgy"                                          341.176
      "Germany"        2016 "IPC D: Textiles; Paper"                                               343.5705
      "Italy"          2016 "IPC C: Chemistry; Metallurgy"                                         348.6883
      "Germany"        2015 "IPC D: Textiles; Paper"                                               352.5942
      "Italy"          2018 "IPC F: Mechanical Engineering; Lighting; Heating; Weapons; Blasting"  376.6061
      "Italy"          2015 "IPC H: Electricity"                                                   421.8856
      "Italy"          2017 "IPC H: Electricity"                                                   435.3698
      "Italy"          2015 "IPC G: Physics"                                                       438.8865
      "Japan"          2018 "IPC E: Fixed Constructions"                                           472.8951
      "Japan"          2018 "IPC D: Textiles; Paper"                                               475.1555
      "Italy"          2016 "IPC G: Physics"                                                       476.2838
      "Italy"          2018 "IPC A: Human Necessities"                                             476.5641
      "United Kingdom" 2018 "IPC F: Mechanical Engineering; Lighting; Heating; Weapons; Blasting"  497.4443
      "United Kingdom" 2018 "IPC A: Human Necessities"                                             507.8799
      "Italy"          2017 "IPC G: Physics"                                                       522.9263
      "Italy"          2016 "IPC H: Electricity"                                                    543.896
      "Japan"          2016 "IPC D: Textiles; Paper"                                               554.4405
      "Japan"          2015 "IPC D: Textiles; Paper"                                               571.3907
      "United Kingdom" 2018 "IPC B: Performing Operations; Transporting"                           574.3645
      "France"         2018 "IPC A: Human Necessities"                                             576.9227
      "United Kingdom" 2017 "IPC C: Chemistry; Metallurgy"                                         581.2652
      "Japan"          2016 "IPC E: Fixed Constructions"                                           591.1311
      "Japan"          2015 "IPC E: Fixed Constructions"                                           603.3491
      "Italy"          2017 "IPC F: Mechanical Engineering; Lighting; Heating; Weapons; Blasting"  618.1938
      "United Kingdom" 2018 "IPC H: Electricity"                                                   618.6688
      "Japan"          2017 "IPC D: Textiles; Paper"                                               627.3081
      "United Kingdom" 2016 "IPC C: Chemistry; Metallurgy"                                         629.2444
      "Japan"          2017 "IPC E: Fixed Constructions"                                           635.5335
      "Italy"          2016 "IPC F: Mechanical Engineering; Lighting; Heating; Weapons; Blasting"  664.6117
      "Italy"          2015 "IPC F: Mechanical Engineering; Lighting; Heating; Weapons; Blasting"  667.2909
      "United Kingdom" 2017 "IPC F: Mechanical Engineering; Lighting; Heating; Weapons; Blasting"  669.1438
      "United Kingdom" 2015 "IPC C: Chemistry; Metallurgy"                                         694.9429
      "Germany"        2018 "IPC E: Fixed Constructions"                                           696.0637
      "United Kingdom" 2015 "IPC F: Mechanical Engineering; Lighting; Heating; Weapons; Blasting"  707.9298
      "United Kingdom" 2016 "IPC F: Mechanical Engineering; Lighting; Heating; Weapons; Blasting"  718.7183
      "France"         2018 "IPC F: Mechanical Engineering; Lighting; Heating; Weapons; Blasting"  723.4854
      "Italy"          2018 "IPC B: Performing Operations; Transporting"                           774.9451
      "Italy"          2015 "IPC A: Human Necessities"                                             842.7225
      "France"         2016 "IPC C: Chemistry; Metallurgy"                                         852.4978
      "Germany"        2015 "IPC E: Fixed Constructions"                                           858.2121
      "United States"  2018 "IPC E: Fixed Constructions"                                           883.0867
      "United Kingdom" 2018 "IPC G: Physics"                                                       888.5177
      "Germany"        2018 "IPC C: Chemistry; Metallurgy"                                         888.6639
      "Italy"          2017 "IPC A: Human Necessities"                                             919.1489
      "Germany"        2017 "IPC E: Fixed Constructions"                                           946.7127
      "France"         2015 "IPC C: Chemistry; Metallurgy"                                         947.0167
      "France"         2017 "IPC C: Chemistry; Metallurgy"                                         947.9912
      "United Kingdom" 2016 "IPC B: Performing Operations; Transporting"                           962.3157
      "Italy"          2016 "IPC A: Human Necessities"                                             989.2037
      "Germany"        2016 "IPC E: Fixed Constructions"                                            991.714
      "United Kingdom" 2017 "IPC B: Performing Operations; Transporting"                          1002.6637
      "United Kingdom" 2015 "IPC B: Performing Operations; Transporting"                          1010.9802
      "France"         2018 "IPC G: Physics"                                                      1030.4623
      "Italy"          2015 "IPC B: Performing Operations; Transporting"                          1075.2632
      "United Kingdom" 2017 "IPC H: Electricity"                                                   1117.382
      "United Kingdom" 2016 "IPC H: Electricity"                                                  1133.6638
      "France"         2018 "IPC H: Electricity"                                                  1134.0883
      "France"         2018 "IPC B: Performing Operations; Transporting"                           1135.279
      "France"         2016 "IPC F: Mechanical Engineering; Lighting; Heating; Weapons; Blasting" 1207.3523
      "United Kingdom" 2015 "IPC H: Electricity"                                                  1213.8413
      "United Kingdom" 2017 "IPC A: Human Necessities"                                             1215.051
      "United Kingdom" 2015 "IPC A: Human Necessities"                                            1222.4565
      "United Kingdom" 2016 "IPC A: Human Necessities"                                            1238.7816
      "France"         2017 "IPC F: Mechanical Engineering; Lighting; Heating; Weapons; Blasting"  1257.149
      end
      
      * just to add a little excitement 
      expand 2 if mod(_n, 7) <= 1 
      
      bysort time tech_class country: gen freq = _N 
      egen tag = tag(time tech_class country)
      egen rank = rank(-freq) if tag, by(time tech_class)
      bysort time tech_class country (rank) : replace rank = rank[1] 
      
      * -groups- is from the Stata Journal and must be installed first 
      groups time tech_class rank country freq, show(none) sepby(time tech_class)
      
       +-----------------------------------------------------------------------------------------------------------+
        | time                                                       tech_class_1dig   rank          country   freq |
        |-----------------------------------------------------------------------------------------------------------|
        | 2015                                              IPC A: Human Necessities      1   United Kingdom      2 |
        | 2015                                              IPC A: Human Necessities      2            Italy      1 |
        |-----------------------------------------------------------------------------------------------------------|
        | 2015                            IPC B: Performing Operations; Transporting    1.5            Italy      1 |
        | 2015                            IPC B: Performing Operations; Transporting    1.5   United Kingdom      1 |
        |-----------------------------------------------------------------------------------------------------------|
        | 2015                                          IPC C: Chemistry; Metallurgy      1            Italy      2 |
        | 2015                                          IPC C: Chemistry; Metallurgy    2.5           France      1 |
        | 2015                                          IPC C: Chemistry; Metallurgy    2.5   United Kingdom      1 |
        |-----------------------------------------------------------------------------------------------------------|
        | 2015                                                IPC D: Textiles; Paper    1.5           France      2 |
        | 2015                                                IPC D: Textiles; Paper    1.5    United States      2 |
        | 2015                                                IPC D: Textiles; Paper    4.5          Germany      1 |
        | 2015                                                IPC D: Textiles; Paper    4.5            Italy      1 |
        | 2015                                                IPC D: Textiles; Paper    4.5            Japan      1 |
        | 2015                                                IPC D: Textiles; Paper    4.5   United Kingdom      1 |
        |-----------------------------------------------------------------------------------------------------------|
        | 2015                                            IPC E: Fixed Constructions      1           France      2 |
        | 2015                                            IPC E: Fixed Constructions    3.5          Germany      1 |
        | 2015                                            IPC E: Fixed Constructions    3.5            Italy      1 |
        | 2015                                            IPC E: Fixed Constructions    3.5            Japan      1 |
        | 2015                                            IPC E: Fixed Constructions    3.5   United Kingdom      1 |
        |-----------------------------------------------------------------------------------------------------------|
        | 2015   IPC F: Mechanical Engineering; Lighting; Heating; Weapons; Blasting      1   United Kingdom      2 |
        | 2015   IPC F: Mechanical Engineering; Lighting; Heating; Weapons; Blasting      2            Italy      1 |
        |-----------------------------------------------------------------------------------------------------------|
        | 2015                                                        IPC G: Physics      1            Italy      1 |
        |-----------------------------------------------------------------------------------------------------------|
        | 2015                                                    IPC H: Electricity      1            Italy      2 |
        | 2015                                                    IPC H: Electricity      2   United Kingdom      1 |
        |-----------------------------------------------------------------------------------------------------------|
        | 2016                                              IPC A: Human Necessities    1.5            Italy      2 |
        | 2016                                              IPC A: Human Necessities    1.5   United Kingdom      2 |
        |-----------------------------------------------------------------------------------------------------------|
        | 2016                            IPC B: Performing Operations; Transporting      1   United Kingdom      2 |
        |-----------------------------------------------------------------------------------------------------------|
        | 2016                                          IPC C: Chemistry; Metallurgy      1   United Kingdom      2 |
        | 2016                                          IPC C: Chemistry; Metallurgy    2.5           France      1 |
        | 2016                                          IPC C: Chemistry; Metallurgy    2.5            Italy      1 |
        |-----------------------------------------------------------------------------------------------------------|
        | 2016                                                IPC D: Textiles; Paper      1           France      2 |
        | 2016                                                IPC D: Textiles; Paper      4          Germany      1 |
        | 2016                                                IPC D: Textiles; Paper      4            Italy      1 |
        | 2016                                                IPC D: Textiles; Paper      4            Japan      1 |
        | 2016                                                IPC D: Textiles; Paper      4   United Kingdom      1 |
        | 2016                                                IPC D: Textiles; Paper      4    United States      1 |
        |-----------------------------------------------------------------------------------------------------------|
        | 2016                                            IPC E: Fixed Constructions      1           France      2 |
        | 2016                                            IPC E: Fixed Constructions    3.5          Germany      1 |
        | 2016                                            IPC E: Fixed Constructions    3.5            Italy      1 |
        | 2016                                            IPC E: Fixed Constructions    3.5            Japan      1 |
        | 2016                                            IPC E: Fixed Constructions    3.5   United Kingdom      1 |
        |-----------------------------------------------------------------------------------------------------------|
        | 2016   IPC F: Mechanical Engineering; Lighting; Heating; Weapons; Blasting      1   United Kingdom      2 |
        | 2016   IPC F: Mechanical Engineering; Lighting; Heating; Weapons; Blasting    2.5           France      1 |
        | 2016   IPC F: Mechanical Engineering; Lighting; Heating; Weapons; Blasting    2.5            Italy      1 |
        |-----------------------------------------------------------------------------------------------------------|
        | 2016                                                        IPC G: Physics      1            Italy      1 |
        |-----------------------------------------------------------------------------------------------------------|
        | 2016                                                    IPC H: Electricity      1   United Kingdom      2 |
        | 2016                                                    IPC H: Electricity      2            Italy      1 |
        |-----------------------------------------------------------------------------------------------------------|
        | 2017                                              IPC A: Human Necessities    1.5            Italy      1 |
        | 2017                                              IPC A: Human Necessities    1.5   United Kingdom      1 |
        |-----------------------------------------------------------------------------------------------------------|
        | 2017                            IPC B: Performing Operations; Transporting      1   United Kingdom      1 |
        |-----------------------------------------------------------------------------------------------------------|
        | 2017                                          IPC C: Chemistry; Metallurgy      1   United Kingdom      2 |
        | 2017                                          IPC C: Chemistry; Metallurgy    2.5           France      1 |
        | 2017                                          IPC C: Chemistry; Metallurgy    2.5            Italy      1 |
        |-----------------------------------------------------------------------------------------------------------|
        | 2017                                                IPC D: Textiles; Paper    3.5           France      1 |
        | 2017                                                IPC D: Textiles; Paper    3.5          Germany      1 |
        | 2017                                                IPC D: Textiles; Paper    3.5            Italy      1 |
        | 2017                                                IPC D: Textiles; Paper    3.5            Japan      1 |
        | 2017                                                IPC D: Textiles; Paper    3.5   United Kingdom      1 |
        | 2017                                                IPC D: Textiles; Paper    3.5    United States      1 |
        |-----------------------------------------------------------------------------------------------------------|
        | 2017                                            IPC E: Fixed Constructions      1            Japan      2 |
        | 2017                                            IPC E: Fixed Constructions    3.5           France      1 |
        | 2017                                            IPC E: Fixed Constructions    3.5          Germany      1 |
        | 2017                                            IPC E: Fixed Constructions    3.5            Italy      1 |
        | 2017                                            IPC E: Fixed Constructions    3.5   United Kingdom      1 |
        |-----------------------------------------------------------------------------------------------------------|
        | 2017   IPC F: Mechanical Engineering; Lighting; Heating; Weapons; Blasting      2           France      1 |
        | 2017   IPC F: Mechanical Engineering; Lighting; Heating; Weapons; Blasting      2            Italy      1 |
        | 2017   IPC F: Mechanical Engineering; Lighting; Heating; Weapons; Blasting      2   United Kingdom      1 |
        |-----------------------------------------------------------------------------------------------------------|
        | 2017                                                        IPC G: Physics      1            Italy      1 |
        |-----------------------------------------------------------------------------------------------------------|
        | 2017                                                    IPC H: Electricity    1.5            Italy      2 |
        | 2017                                                    IPC H: Electricity    1.5   United Kingdom      2 |
        |-----------------------------------------------------------------------------------------------------------|
        | 2018                                              IPC A: Human Necessities    1.5           France      2 |
        | 2018                                              IPC A: Human Necessities    1.5   United Kingdom      2 |
        | 2018                                              IPC A: Human Necessities      3            Italy      1 |
        |-----------------------------------------------------------------------------------------------------------|
        | 2018                            IPC B: Performing Operations; Transporting      2           France      1 |
        | 2018                            IPC B: Performing Operations; Transporting      2            Italy      1 |
        | 2018                            IPC B: Performing Operations; Transporting      2   United Kingdom      1 |
        |-----------------------------------------------------------------------------------------------------------|
        | 2018                                          IPC C: Chemistry; Metallurgy    2.5           France      1 |
        | 2018                                          IPC C: Chemistry; Metallurgy    2.5          Germany      1 |
        | 2018                                          IPC C: Chemistry; Metallurgy    2.5            Italy      1 |
        | 2018                                          IPC C: Chemistry; Metallurgy    2.5   United Kingdom      1 |
        |-----------------------------------------------------------------------------------------------------------|
        | 2018                                                IPC D: Textiles; Paper      2          Germany      2 |
        | 2018                                                IPC D: Textiles; Paper      2   United Kingdom      2 |
        | 2018                                                IPC D: Textiles; Paper      2    United States      2 |
        | 2018                                                IPC D: Textiles; Paper      5           France      1 |
        | 2018                                                IPC D: Textiles; Paper      5            Italy      1 |
        | 2018                                                IPC D: Textiles; Paper      5            Japan      1 |
        |-----------------------------------------------------------------------------------------------------------|
        | 2018                                            IPC E: Fixed Constructions    1.5   United Kingdom      2 |
        | 2018                                            IPC E: Fixed Constructions    1.5    United States      2 |
        | 2018                                            IPC E: Fixed Constructions    4.5           France      1 |
        | 2018                                            IPC E: Fixed Constructions    4.5          Germany      1 |
        | 2018                                            IPC E: Fixed Constructions    4.5            Italy      1 |
        | 2018                                            IPC E: Fixed Constructions    4.5            Japan      1 |
        |-----------------------------------------------------------------------------------------------------------|
        | 2018   IPC F: Mechanical Engineering; Lighting; Heating; Weapons; Blasting      1   United Kingdom      2 |
        | 2018   IPC F: Mechanical Engineering; Lighting; Heating; Weapons; Blasting    2.5           France      1 |
        | 2018   IPC F: Mechanical Engineering; Lighting; Heating; Weapons; Blasting    2.5            Italy      1 |
        |-----------------------------------------------------------------------------------------------------------|
        | 2018                                                        IPC G: Physics    1.5            Italy      2 |
        | 2018                                                        IPC G: Physics    1.5   United Kingdom      2 |
        | 2018                                                        IPC G: Physics      3           France      1 |
        |-----------------------------------------------------------------------------------------------------------|
        | 2018                                                    IPC H: Electricity      2           France      1 |
        | 2018                                                    IPC H: Electricity      2            Italy      1 |
        | 2018                                                    IPC H: Electricity      2   United Kingdom      1 |
        +-----------------------------------------------------------------------------------------------------------+

      Comment


      • #4
        Comment on #2, which I didn't see while I was writing #3:

        I ignored nr_pat --which looks like an identifier to me. I didn't interpret e.g. 1257.149 as a number of patents.

        Comment


        • #5
          Dear Nick and Laura, thank you so much for your help.

          I tried the solutions kindly provdided by Nick in #2, but I have to add that nr_pat is the number of patents, not an identifier. The "identifier" is the country. So countries should be ranked based on the number of patents they have filed per tech class and year. Maybe I could have been clearer on the meaning of each variable.
          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input str75 country int time str5 ipc float(nr_pat rank)
          "France"         1990 "A"  726.4143 3.5
          "Germany"        1990 "A"  1334.046 3.5
          "Italy"          1990 "A"  408.5171 3.5
          "Japan"          1990 "A" 1480.6796 3.5
          "United Kingdom" 1990 "A"  612.9993 3.5
          "United States"  1990 "A"  3390.473 3.5
          "France"         1990 "B" 1062.9788 3.5
          "Germany"        1990 "B"  3372.826 3.5
          "Italy"          1990 "B"  682.6773 3.5
          "Japan"          1990 "B" 4060.4905 3.5
          "United Kingdom" 1990 "B"  695.3236 3.5
          "United States"  1990 "B"  3714.282 3.5
          "France"         1990 "C"  686.1815 3.5
          "Germany"        1990 "C" 2119.2722 3.5
          "Italy"          1990 "C"  310.5116 3.5
          "Japan"          1990 "C"  3171.057 3.5
          "United Kingdom" 1990 "C"  638.4941 3.5
          "United States"  1990 "C" 4240.3574 3.5
          "France"         1990 "D"  114.7263 3.5
          "Germany"        1990 "D"  394.7477 3.5
          end

          Following on this, although the code worked per sè, it does not give me a rank based on the number of patents. As I can't be possible that 6 countries ranks the same each year within the same technological class, when they clearly have a different number of patents file.d
          Can you help? In any case, thanks a lot, I did not know the command-
          Code:
          -groups-
          and either way it seems very useful!
          Dalila

          Comment


          • #6
            Forgot to add that Laura's code - discending- is exactly what I was looking for as for each year, country and technological class I have the rank.

            Code:
            * Example generated by -dataex-. For more info, type help dataex
            clear
            input str75 country int time str5 ipc float(rank2 nr_pat)
            "United States"  1990 "A" 1  3390.473
            "Japan"          1990 "A" 2 1480.6796
            "Germany"        1990 "A" 3  1334.046
            "France"         1990 "A" 4  726.4143
            "United Kingdom" 1990 "A" 5  612.9993
            "Italy"          1990 "A" 6  408.5171
            "United States"  1991 "A" 1  3521.659
            "Germany"        1991 "A" 2 1358.8596
            "Japan"          1991 "A" 3   1332.09
            "France"         1991 "A" 4  783.2529
            "United Kingdom" 1991 "A" 5  630.9383
            "Italy"          1991 "A" 6  481.4201
            "United States"  1992 "A" 1  3806.447
            "Japan"          1992 "A" 2 1450.6074
            "Germany"        1992 "A" 3 1414.6165
            "France"         1992 "A" 4  752.0453
            "United Kingdom" 1992 "A" 5  647.2571
            "Italy"          1992 "A" 6   427.382
            "United States"  1993 "A" 1  3941.743
            "Japan"          1993 "A" 2  1611.059
            "Germany"        1993 "A" 3  1460.632
            "France"         1993 "A" 4  857.5566
            "United Kingdom" 1993 "A" 5  626.0405
            "Italy"          1993 "A" 6  428.2008
            "United States"  1994 "A" 1  4107.575
            end
            Thanks a lot for you help!!!

            Comment


            • #7
              Well I guessed quite wrongly there, not knowing how 1257.149 could be a number of patents. In that case, ties are probably unlikely, but you use nothing by using egen, rank() directly and it is, if I am now understanding correctly, just one line to get the ranks you want. Note that a minus sign is sufficient to get descending order. There is no need to encode.

              Code:
              . egen rank = rank(-nr_pat), by(time tech_class)
              
              . gen tech = substr(tech_class, 1, 5)
              
              . sort tech rank
              
              . list country tech nr_pat rank if time == 2017, sepby(tech_class)
              
                   +------------------------------------------+
                   |        country    tech     nr_pat   rank |
                   |------------------------------------------|
                2. | United Kingdom   IPC A   1215.051      1 |
                8. |          Italy   IPC A   919.1489      2 |
                   |------------------------------------------|
               13. | United Kingdom   IPC B   1002.664      1 |
                   |------------------------------------------|
               19. |         France   IPC C   947.9912      1 |
               21. | United Kingdom   IPC C   581.2652      2 |
               27. |          Italy   IPC C    341.176      3 |
                   |------------------------------------------|
               32. |          Japan   IPC D   627.3081      1 |
               34. |        Germany   IPC D   313.9293      2 |
               39. |  United States   IPC D   260.7929      3 |
               44. |          Italy   IPC D   105.8811      4 |
               47. |         France   IPC D    86.7161      5 |
               53. | United Kingdom   IPC D    28.9404      6 |
                   |------------------------------------------|
               56. |        Germany   IPC E   946.7127      1 |
               59. |          Japan   IPC E   635.5335      2 |
               64. |         France   IPC E    301.555      3 |
               66. | United Kingdom   IPC E   248.3734      4 |
               73. |          Italy   IPC E    230.902      5 |
                   |------------------------------------------|
               77. |         France   IPC F   1257.149      1 |
               80. | United Kingdom   IPC F   669.1438      2 |
               83. |          Italy   IPC F   618.1938      3 |
                   |------------------------------------------|
               88. |          Italy   IPC G   522.9263      1 |
                   |------------------------------------------|
               92. | United Kingdom   IPC H   1117.382      1 |
               99. |          Italy   IPC H   435.3698      2 |
                   +------------------------------------------+
              Backing up to #1 your C4 measure is a ratio,

              Code:
              .  egen double numer = total((rank <= 4) * nr_pat), by(time tech_class)
              
              .  egen double denom = total(nr_pat), by(time tech_class)
              
              . gen double C4 = numer / denom
              with scaling by 100 optional. This will automatically be 1 or 100% unless 5 or 6 countries feature in each subset. Make sure you use each result just once to avoid multiple counting by using

              Code:
               
               egen tag = tag(time tech_class) 
              Last edited by Nick Cox; 18 Feb 2022, 05:02.

              Comment


              • #8
                Thanks a lot Nick, the last solution worked perfectly, I wonder why the -egen rank (- nr pat) yesterday gave me completely wrong results: for sure I missed something.
                Thanks a lot again!

                Dalila

                Comment


                • #9
                  Indeed, but without seeing your code, we can only guess. The answer probably lies in the by() argument (or what was supplied to a by: prefix).

                  If you had shown the code that didn't work in #1, the thread might have converged more quickly!

                  Comment

                  Working...
                  X