Announcement

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

  • Counting Observations with two IDs

    This is what I currently have:

    id_1 id_2 x1 x2
    1 2 10 100000
    1 4 15 100000
    1 17 3 100000
    2 3 2 150000
    2 4 20 150000
    2 10 6 150000
    2 11 9 150000

    I am wondering if it is possible to count observations given two IDs.
    I would like to create the variable "count" below


    id_1 id_2 x1 x2 count
    1 2 10 100000 1
    1 4 15 100000 2
    1 17 3 100000 3
    2 3 2 150000 1
    2 4 20 150000 2
    2 10 6 150000 3
    2 11 9 150000 4


    Thank you

  • #2
    I don't for a minute understand your description of what you want the variable "count" to be, but this code generates a variable that duplicates what you want, at least for the sample data.
    Code:
    cls
    * Example generated by -dataex-. To install: ssc install dat1ex
    clear
    input byte(id_1 id_2 x1) long x2 byte count
    1  2 10 100000 1
    1  4 15 100000 2
    1 17  3 100000 3
    2  3  2 150000 1
    2  4 20 150000 2
    2 10  6 150000 3
    2 11  9 150000 4
    end
    generate seq = _n
    bysort id_1 (seq): generate mycount = _n
    list, sepby(id_1)
    Code:
    . list, sepby(id_1)
    
         +---------------------------------------------------+
         | id_1   id_2   x1       x2   count   seq   mycount |
         |---------------------------------------------------|
      1. |    1      2   10   100000       1     1         1 |
      2. |    1      4   15   100000       2     2         2 |
      3. |    1     17    3   100000       3     3         3 |
         |---------------------------------------------------|
      4. |    2      3    2   150000       1     4         1 |
      5. |    2      4   20   150000       2     5         2 |
      6. |    2     10    6   150000       3     6         3 |
      7. |    2     11    9   150000       4     7         4 |
         +---------------------------------------------------+
    With that said, two pieces of advice based on your first two posts.

    First, some advice on using Stata.

    I'm sympathetic to you as a new user of Stata - it's a lot to absorb. And even worse if perhaps you are under pressure to produce some output quickly. Nevertheless, I'd like to encourage you to take a step back from your immediate tasks.

    When I began using Stata in a serious way, I started, as have others here, by reading my way through the Getting Started with Stata manual relevant to my setup. Chapter 18 then gives suggested further reading, much of which is in the Stata User's Guide, and I worked my way through much of that reading as well. There are a lot of examples to copy and paste into Stata's do-file editor to run yourself, and better yet, to experiment with changing the options to see how the results change.

    All of these manuals are included as PDFs in the Stata installation (since version 11) and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu. The objective in doing the reading was not so much to master Stata as to be sure I'd become familiar with a wide variety of important basic techniques, so that when the time came that I needed them, I might recall their existence, if not the full syntax, and know how to find out more about them in the help files and PDF manuals.

    Stata supplies exceptionally good documentation that amply repays the time spent studying it - there's just a lot of it. The path I followed surfaces the things you need to know to get started in a hurry and to work effectively.

    Second, some advice on using Statalist.

    Please review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question. It's particularly helpful to copy commands and output from your Stata Results window and paste them into your Statalist post using CODE delimiters, and to use the dataex command to provide sample data, as described in section 12 of the FAQ.

    The more you help others understand your problem, the more likely others are to be able to help you solve your problem.

    Comment


    • #3
      Thank you, thank you, thank you for input.

      You are correct, I am somewhat new to Stata and haven't explored the documentation as much as I should have.

      The information and resources provided are greatly appreciated.

      Comment

      Working...
      X