Announcement

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

  • How to give id to observations with the same price?

    Hi Folks,

    I have a easy question that i hope people can help.

    Here is what I have

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte id str9 companny
    . "Google "  
    . "Google "  
    . "Google "  
    . "Amazon"   
    . "Facebook "
    . "Facebook "
    . "Facebook "
    end
    Basically, I have a bunch of company names, and I want to assign an id to companies with the same name. like all Google with id of 1, amazon with id of 2, facebook with id of 3.

    The important part is, I do not want to change the order of this dataset. I tried several strategies, but they would sort my companies and list amazon at the first, because it has to sort it using alphabetic order.

    Can anyone help me with that? thanks.

  • #2
    Code:
    drop id
    gen `c(obs_t)' id = sum(companny != companny[_n-1])
    will do it.

    By the way, company is spelled with only one n.
    Last edited by Clyde Schechter; 07 Jul 2023, 18:33.

    Comment


    • #3
      Thanks! It's very helpful.

      Comment

      Working...
      X