Announcement

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

  • Dropping the previous values and keeping only the latest value

    I want to drop all the values having the same co_code, except the co_code having the latest nic_prod_code.

    co_code coprd_date nic_prod_code
    289 1990331 5224
    289 1990930 5224
    289 1990931 5224
    . . .
    . . .
    . . .
    . . .
    . . .
    . . .

    289 20160630 46909
    289 20160930 46909
    . . .
    . . .
    . .
    289 20201231 594
    289 20210331 594
    . . .
    . . .
    289 20211231 594

    I only want to keep the co_code which has the latest nic_prod_code. I want to keep the latest value of co_code (i.e. on 2021/1/31).

    The command used by me- "duplicates drop co_code nic_prod_code, force" (this command dropped all the co_codes having the same nic_prod_code.)

    Then I used- "duplicates drop co_code, force" (this command dropped all the duplicates of a particular co_code, it kept the co_code with the first nic_prod_code and dropped the rest of them. Eg- if co_code was the same from 1990 to 1996 and then changed in 1997, then the command kept the co_code of 1990 and dropped the co_code for the rest of the years. But I want that stata keeps the co_code of 1997 and drop the co_code which were before 1997.)
    Last edited by Nihar Singh; 06 Jun 2022, 05:17.

  • #2
    You drop observations in Stata, not distinct values of variables.

    Code:
    bys co_code (nic_prod_code): keep if _n==_N

    Comment


    • #3
      Okay noted.

      Thankyou Andrew.

      Comment

      Working...
      X