Announcement

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

  • calculating frequency of events by age-class

    Hello stata-users,

    I need to create a two column dataset by age-class and number of events. I can do so manually by using
    Code:
    tab age events
    . However, I am sure there's perhaps a better way to do it.
    Here's sample dataset which should give you something like age: 18; events: 12 (here event is codified by 0) , age 19; events: counts of 0s, and so on.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int age byte event
    18 0
    18 0
    28 0
    18 0
    38 1
    18 0
    48 0
    18 0
    58 0
    18 0
    68 1
    18 0
    38 0
    18 1
    18 0
    68 0
    18 0
    18 0
    18 0
    18 0
    end
    Thank you.
    Last edited by Pakeezah Saadat; 08 Mar 2019, 14:30.

  • #2
    contract or collapse will do what you want.

    Comment


    • #3
      Thank you, Nick. That worked beautifully.

      I am wondering now if I want to assign that new value "number of events" to each observation (going reverse), how would I do that?

      Essentially, if I find that for age 18, there's is 90 events, I want to assign the number 90 to all observations at age 18.

      Comment


      • #4
        If that's what you want then contract or collapse are not needed at all.

        Code:
        bysort age : gen freq = _N

        Comment


        • #5
          This is perhaps counting the frequency of each age-class which is not what I need. If I have an event (0,1), I would like to know how many 0s and 1s are in each class and then have it listed beside it in columns.

          Comment


          • #6
            Sorry, but I have to think that #4 is a fair answer to #3. For the different question in #5 consider

            Code:
            tab age event
            which is just what you mention in #1, so I am lost on what else you seek.

            Comment


            • #7
              Could this be what Pakeezah wants?

              Code:
              bysort age : egen tot_events = total(event)
              bysort age : egen tot_nonevents = total(1-event)

              Comment


              • #8
                Andrea Discacciati You may be right. Smart guess.

                When Pakeezah Saadat asks for columns, I assume that columns in a table are meant, but Pakeezah may be thinking spreadsheet-style in terms of new variables.

                Note that

                Code:
                contract age events
                puts out new variables too.

                Comment


                • #9
                  Originally posted by Andrea Discacciati View Post
                  Could this be what Pakeezah wants?

                  Code:
                  bysort age : egen tot_events = total(event)
                  bysort age : egen tot_nonevents = total(1-event)
                  Perfect, thanks Andrea Discacciati. I wonder now though, if a variable has 0s and 1s as values, how does it add up the 1s first? No such command was given. This is in relation to
                  Code:
                  bysort age:egen tot_events=total(events)

                  Comment

                  Working...
                  X