Announcement

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

  • Generating groups within groups

    Dear Statalist,

    I would like to generate a variable which indicates groups within groups. Consider the following data:

    ID INFO DESIRED_VARIABLE

    1 A 1
    1 A 1
    1 B 2
    1 B 2
    2 C 1
    2 C 1
    2 D 2
    2 D 2

    Is there any way to do this? I tried egen-group with "by", but I get an error that "by" is not allowed there.

    Thanks for any helpful comments!
    Juliane

  • #2
    Jane***/Juliane: Please note our preference for full real names, typically given name and family name. More at

    http://www.statalist.org/forums/help#realnames

    http://www.statalist.org/forums/help#adviceextras #3

    Here's one way to do it:

    Code:
    clear 
    input ID str1 INFO DESIRED_VARIABLE
    1 A 1 
    1 A 1
    1 B 2
    1 B 2
    2 C 1 
    2 C 1
    2 D 2
    2 D 2
    end
    egen tag = tag(ID INFO)
    bysort ID (INFO) : gen wanted = sum(tag) 
    
    assert wanted == DESIRED 
    
    list, sepby(ID INFO) 
    
         +-------------------------------------+
         | ID   INFO   DESIRE~E   tag   wanted |
         |-------------------------------------|
      1. |  1      A          1     1        1 |
      2. |  1      A          1     0        1 |
         |-------------------------------------|
      3. |  1      B          2     1        2 |
      4. |  1      B          2     0        2 |
         |-------------------------------------|
      5. |  2      C          1     1        1 |
      6. |  2      C          1     0        1 |
         |-------------------------------------|
      7. |  2      D          2     1        2 |
      8. |  2      D          2     0        2 |
         +-------------------------------------+

    Comment


    • #3
      Hi Nick, thanks a lot for your quick reply!

      I have been searching for a way to change my user name to my full name but could not find out how to do it. Is there a way to edit the username?

      Thanks much!
      Juliane

      Comment


      • #4
        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input float id str1 info
        1 "A"
        1 "A"
        1 "B"
        1 "B"
        2 "C"
        2 "C"
        2 "D"
        2 "D"
        end
        
        by id info, sort: gen desired = 1 if _n == 1
        by id: replace desired = sum(desired)
        list, noobs sepby(id)
        will get you what you want.

        Now, I have some requests of you. The norm in the Statalist community is that we use our real given and surnames as our username. Jane*** clearly doesn't conform to that, and, from your signature line, I infer that Jane isn't even your real given name. Unfortunately, the Forum software does not let you edit your user name in your profile, so to change this you will have to click on contact us and ask the forum administrators to make the change. Using our real full names encourages collegiality and professionalism on the site.

        In addition, saying that you "tried egen-group with by" but "got an error" isn't all that helpful. There are lots of things you could have done that would fit that description. And there are lots of error messages Stata can give. More useful is to post the exact command you gave and the exact response you got from Stata. And to make it readable, you should do that in a code block.

        Finally, when posting example data, please use the -dataex- command, as I have done in my example. Run -ssc install dataex- to get the command. The simple instructions for using it are in -help dataex-. By using it, you will provide those who want to help you with a completely faithful replica of your example data that they can then quickly read into Stata with no glitches.

        Thanks.

        Added: Crossed with Nick's response which is a slightly different approach.

        Comment


        • #5
          Clyde has already mentioned how to change name information.

          In fact, the FAQ section linked in #2 explains how to change name information, namely use the CONTACT US button at bottom right to send an email to the forum administrators.

          Note that Clyde's code

          Code:
          by id info, sort: gen desired = 1 if _n == 1
          is essentially equivalent to

          Code:
          egen tag = tag(ID INFO)
          The latter translates to

          Code:
          by ID INFO, sort: gen tag = _n == 1
          but the difference disappears with sum()

          Last edited by Nick Cox; 10 Aug 2016, 11:15.

          Comment


          • #6
            Dear Nick and Clyde,

            thanks for your quick replies and advise.

            Juliane

            Comment

            Working...
            X