Announcement

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

  • Count number of different observations per group

    Hi together,

    after recieving very good help I would like to ask you again for your help.

    I have two variables (ArticleNumber and Price) and I want to calculate how often prices changed within each article. I have scanner data so I have many different cases per each article with different prices that have been paid. What I would like to do is to add a new variable each case that indicates the number of different prices for the corresponding ArticleNumber.

    I used "bysort Article_Nu: distinct Price" . I got the result, but I have no idea how to generate a new variable.

    Thank you in advance for your help!

  • #2
    See

    SJ-8-4 dm0042 . . . . . . . . . . . . Speaking Stata: Distinct observations
    (help distinct if installed) . . . . . . N. J. Cox and G. M. Longton
    Q4/08 SJ 8(4):557--568
    shows how to answer questions about distinct observations
    from first principles; provides a convenience command

    http://www.stata-journal.com/sjpdf.h...iclenum=dm0042 for discussion of principles.

    It's the original write-up of distinct (a user-written program, as you are asked to explain).

    If prices only ever change by an increase then

    Code:
    egen tag = tag(Article_Nu Price)
    egen total = total(tag), by(Article_Nu)
    is what you want.

    Comment


    • #3
      Hello,

      thank you for your quick response. I got a new variable, but you are right I only got the changes if there is an increase. I just want to know how often prices chanced. Has somebody an idea?

      For example:
      Article Number 1 -> price: 2 // 3
      Article Number 2 -> price 3 // 2
      Article Number 1-> price: 2 // 3
      Article Number 3 ->price: 4 // 1
      Article Number 1-> price: 3 // 3
      Article Number 1-> price: 3 // 3
      Article Number 2 -> price: 5 // 2
      Article Number 1-> price: 1 // 3
      ....

      The number after the // indicates the number of prices for each article number. The new variable should tell me that there are 3 different prices for article 1 (no matter if in- or decrease).

      Thank!!

      Comment


      • #4
        Code:
        bysort Article_Nu (Date): gen change = price != price[_n-1] & _n > 1  
        egen nchanges = total(change), by(Article_Nu)

        Comment

        Working...
        X