Announcement

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

  • creating a variable identyfying belonging to 2 other variables

    Dear All,

    I got stuck with my stata work. I have data on sectors (HSspecific) and countries (reporter). I wanna create a variable indicating that it is for a given country in a given sector, so that I have for example for Australia in sector 0 : AUS0. Here is a sample of my data:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double tradevalue_ex str3 reporter long HSspecific
      315057867 "AUS" 0
       99401267 "AUS" 0
       30544335 "AUS" 0
       61603138 "AUS" 0
     2635123555 "AUS" 0
      132330437 "AUS" 0
     5267085798 "AUS" 0
      760438676 "AUS" 0
       49257876 "AUS" 0
       76051716 "AUS" 0
      457469316 "AUS" 0
       96004848 "AUS" 0
     1353869319 "AUS" 0
    31251833517 "AUS" 0
      287704907 "AUS" 0
      160930750 "AUS" 0
      117388387 "AUS" 0
       87805534 "AUS" 0
     6158398175 "AUS" 0
       30460372 "AUS" 0
       52773899 "AUS" 0
       69735614 "AUS" 0
     3834472665 "AUS" 0
      831655308 "AUS" 0
      534397254 "AUS" 0
              0 "AUS" 0
    15356473026 "AUS" 0
      248284111 "AUS" 0
       27206046 "AUS" 0
     1294341301 "AUS" 0
      828494978 "AUS" 0
      800932944 "AUS" 0
      197823700 "AUS" 0
     1031543916 "AUS" 0
    17710695419 "AUS" 0
              0 "AUS" 0
     9814552469 "AUS" 0
     6114960461 "AUS" 0
      421347631 "AUS" 0
      277438187 "AUS" 0
       41767722 "AUS" 0
     5431902055 "AUS" 0
      129208194 "AUS" 0
     7069127457 "AUS" 0
       15777475 "AUS" 0
      313441983 "AUS" 0
       12640587 "AUS" 0
      707285627 "AUS" 0
     1420528572 "AUS" 0
      647406302 "AUS" 0
      752961734 "AUS" 0
              0 "AUS" 0
      704865737 "AUS" 0
       41337976 "AUS" 0
      557247237 "AUS" 0
       74196834 "AUS" 0
     1787465424 "AUS" 0
       59750768 "AUS" 0
     2382657934 "AUS" 0
       82395005 "AUS" 0
      375886871 "AUS" 0
       40981612 "AUS" 0
     1556555985 "AUS" 0
      529222435 "AUS" 0
      129210047 "AUS" 0
              0 "AUS" 0
     1798172822 "AUS" 0
     4925112283 "AUS" 0
      451818896 "AUS" 0
     4415983763 "AUS" 0
      666176798 "AUS" 0
      224357654 "AUS" 0
      303970724 "AUS" 0
       89367652 "AUS" 0
     5307657643 "AUS" 0
      100021130 "AUS" 0
      867382268 "AUS" 0
    73804136765 "AUS" 0
     2980450888 "AUS" 0
     1029633820 "AUS" 0
     2447608055 "AUS" 0
     3687916642 "AUS" 0
     1126056378 "AUS" 0
      587539406 "AUS" 0
       23610568 "AUS" 0
       42009484 "AUS" 0
      366882895 "AUS" 0
     1429329689 "AUS" 0
     7356810011 "AUS" 0
    10184268115 "AUS" 0
      519814575 "AUS" 0
      318655944 "AUS" 0
       33176840 "AUS" 0
      847104126 "AUS" 0
       11142131 "AUS" 0
      579277761 "AUS" 0
     2778133661 "AUS" 0
      134253261 "AUS" 0
      203037891 "AUS" 0
      135200284 "AUS" 0
    end



    I was just trying to multiply the variables like:

    gen country_sector=HSspecific*reporter

    but it didn't yield the expected results. Then I tried to destring, encode etc, but wasn't successful. I've just run out of ideas and hope You could help me

  • #2
    Code:
    egen wanted= group(reporter HSspecific), label
    tab wanted

    Comment


    • #3
      Thank you for your answer. I tried it, but unfortunately I got an error message: too many values for label define. I have panel data for 26 countries from 1990 till 2018 on export and import for almost 900 sectors.I needed to calculate aggregate trade weights for every country in a given sector, where weight is a country share in trade in a given sector. I calculated the sum of export for a given sector by applying:

      Code:
      bysort HSspecific: egen sum_export=total(tradevalue_ex)
      The sum for a given country by:

      Code:
      bysort HSspecific reporter: egen sum_export_reporter=total(tradevalue_ex)
      Then I generated the variable weight:
      Code:
      gen weight=sum_export_reporter/sum_export
      My idea was to have a column with the variables identyfying the country and sector at once, like AUS0 e.g.

      Comment


      • #4
        You can have a string-identifier then:

        Code:
        gen wanted= reporter +"-"+HSspecific
        tab wanted in 1/200

        Comment


        • #5
          Stata showed me an error: type mismatch. When I clicked in the error I saw that I wanted to combine a string and numeric subexpression. I didn't want to try your patience since you helped me a lot before. After some struggles I got the command:
          Code:
          egen countrysector = concat(reporter HSspecific)
          It worked! But thank you for your answer anyway

          Comment


          • #6
            Sorry, should be

            Code:
            gen wanted= reporter +"-"+string(HSspecific)

            It is no bother. Always express any difficulties that you may have.

            Comment

            Working...
            X