Announcement

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

  • Create rank depending on 2 variables

    Hey Stata Community!

    I have a question concerning generating a new variable.

    My dataset looks like this:
    Product Date
    A 05/2017
    A 05/2017
    A 08/2017
    A 08/2017
    A 12/2018
    A 03/2019
    B 01/2014
    B 02/2014
    C 03/2012
    C 03/2012
    C 03/2012
    C 08/2012
    D 01/2017
    D 05/2017
    What I would like to do is generate a new variable, that tells me the date rank order per product, like this:
    Product Date Rank
    A 05/2017 1
    A 05/2017 1
    A 08/2017 2
    A 08/2017 2
    A 12/2018 3
    A 03/2019 4
    B 01/2014 1
    B 02/2014 2
    C 03/2012 1
    C 03/2012 1
    C 03/2012 1
    C 08/2012 2
    D 01/2017 1
    D 05/2017 2

    I already tried "egen" and "rank" as commands, but I never got the numbers I wanted in the rank variable.
    I think the problem is that I want to create a rank that is based on 2 prior variables (product AND date). (btw: I have my date variable as numeric and as well as string, if I need to take the other one. So far I worked with the numeric one.)

    I would be very happy if someone could help!


  • #2
    you don't show your code so we can't tell what you did and why it is giving the incorrect results; also, you don't show your data using -dataex- (please read the FAQ) which would make it easy to test new code; but here is a guess
    Code:
    egen rank=rank(Product Date)
    if your date variable is not a Stata internal format date, this will not work; see
    Code:
    help datetime
    by the way, I have never heard of command called "rank" - where did you find it?

    Comment


    • #3
      Thanks for your response, Rich!

      I tried your code:
      egen rank=rank(product date)

      but unfortunately it says:
      "productdate not found" (I don't know why stata assumes this is one variable, when I clearly don't write them together. I tried to seperate them with a comma as well, but then stata says "invalid name".

      My date variable is as well coded in a Stata date format.

      Comment


      • #4
        Here is another way. Note that Stata variable names are case-sensitive.

        Code:
        bys Product (Date): gen wanted= sum(Date!= Date[_n-1])

        Comment


        • #5
          Thank you so much, Andrew! It worked!

          Comment

          Working...
          X