Announcement

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

  • Making groups

    Hi,


    I have a dataset with several trade data over a number of countries. These countries all have a certain water scarcity index for example 1.25 or 3.25. I want to make three equally large groups of countries with low, medium and high water scarcity. So basically the indexes have to be divided into three groups. The command I used no was

    egen HWSgroupAg2 = cut(WSAgricultural), group(3)
    label define HWSgrouplabel 0 "Low" 1 "Medium" 2 "High"
    label values HWSgroupAg2 HWSgrouplabel

    The problem with this, however, is because the dataset consists out of trade transactions is that some countries are repeated in the dataset and, as a result, the amount of countries is not divided into three groups but the trade transactions of countries are divided into three groups. Does anybody have an idea how I should solve this?

    Kind regards


  • #2
    If you want to only apply the "cut" to the number of countries, then "tag" (select one observation per country--assuming that water scarcity is constant within countries) country, cut the countries into groups, then fill in the group number:

    Code:
    egen tag=tag(country)
    egen HWSgroupAg2=cut(WSAgricultural if tag==1, group(3)
    bysort country: replace HWSgroupAg2=HWSgroupAg2[1]
    StataNow/MP 19.5 (64-bit x86-64)
    Win 11

    Comment


    • #3
      Hi,


      I ran this but the problem is when I group ;

      egen HWSgroupAg2=cut(WSAgricultural if tag==1), group(3)

      I get this error ;

      WSAgriculturaliftag not found
      r(111);

      So I think it is not possible to use "if" while grouping.


      Comment


      • #4
        Sorry, typo on my part. Code should read (close the parentheses before the -if- qualifier):

        Code:
        egen tag=tag(country)
        egen HWSgroupAg2=cut(WSAgricultural) if tag==1, group(3)
        bysort country: replace HWSgroupAg2=HWSgroupAg2[1]
        StataNow/MP 19.5 (64-bit x86-64)
        Win 11

        Comment


        • #5
          There's a typo which is biting you. Code should be

          Code:
          egen HWSgroupAg2=cut(WSAgricultural) if tag==1, group(3)

          Comment


          • #6
            Hi,


            The cut is working fine now thanks a lot already. For every country, the first value of HWSgroupAg2 is indeed 1,2 or 3.
            The bysort command does however only replace these 80 values ( 80 countries ) to missing, I have looked at how I could change the bysort command to make it work but can't find it.

            bysort country: replace HWSgroupAg2=HWSgroupAg2[1] This code gives :
            (80 real changes made, 80 to missing)

            Comment


            • #7
              Off my game today!

              Try:

              Code:
              bysort country (HWSgroupAg2): replace HWSgroupAg2=HWSgroupAg2[1]
              StataNow/MP 19.5 (64-bit x86-64)
              Win 11

              Comment


              • #8
                It works perfectly now, Thanks a lot!

                Comment

                Working...
                X