Search code examples
stata

How to delete obs for a month


Here is an example of my data:

* Example generated by -dataex-. To install: ssc install dataex
clear
input str6 Name double Value long Date
"ABAD1" 1019 18856
"ABAD1" 1149 19430
"ABAD1" 1160 19423
"ABAD1" 1160 19388
"ABAD1" 1220 19380
"ABAD1" 1220 19380
"ABAD1" 1228 19377
"ABAD1" 1228 19377
"ABAD1" 1300 19373
"ABAD1" 1311 19294
"ABAD1" 1311 19252
"ABAD1" 1315 19212
"ABAD1" 1354 19359
"ABAD1" 1360 19357
"ABAD1" 1381 19351
"ABAD1" 1408 19352
"ABAD1" 1394 19191
"ABAD1" 1415 18986
"ABAD1" 1475 19332
"ABAD1" 1512 19030
"ABAD1" 1633 19101
"ABAD1" 1680 19063
"ABAD1" 1771 19128
end
format %tdD_m_Y Date

I want to remove observations of the first month for each Name value.

How can I do this?


Solution

  • In your (helpfully readable) example using dataex there is just one distinct Name and only one observation for the first month in the record, but the code here will work for several panels and multiple observations too. mofd() yields a monthly date from a daily date; if the monthly date is the same as the monthly date for the first observation in a panel, then necessarily it's in the same first month of record.

    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str6 Name double Value long Date
    "ABAD1" 1019 18856
    "ABAD1" 1149 19430
    "ABAD1" 1160 19423
    "ABAD1" 1160 19388
    "ABAD1" 1220 19380
    "ABAD1" 1220 19380
    "ABAD1" 1228 19377
    "ABAD1" 1228 19377
    "ABAD1" 1300 19373
    "ABAD1" 1311 19294
    "ABAD1" 1311 19252
    "ABAD1" 1315 19212
    "ABAD1" 1354 19359
    "ABAD1" 1360 19357
    "ABAD1" 1381 19351
    "ABAD1" 1408 19352
    "ABAD1" 1394 19191
    "ABAD1" 1415 18986
    "ABAD1" 1475 19332
    "ABAD1" 1512 19030
    "ABAD1" 1633 19101
    "ABAD1" 1680 19063
    "ABAD1" 1771 19128
    end
    format %tdD_m_Y Date
    
    sort Name Date 
    list Name Date in 1/5 
    
         +-------------------+
         |  Name        Date |
         |-------------------|
      1. | ABAD1   17 Aug 11 |
      2. | ABAD1   25 Dec 11 |
      3. | ABAD1   07 Feb 12 |
      4. | ABAD1   11 Mar 12 |
      5. | ABAD1   18 Apr 12 |
         +-------------------+
    
    bysort Name (Date) : drop if mofd(Date) == mofd(Date[1]) 
    
    list Name Date in 1/5 
    
         +-------------------+
         |  Name        Date |
         |-------------------|
      1. | ABAD1   25 Dec 11 |
      2. | ABAD1   07 Feb 12 |
      3. | ABAD1   11 Mar 12 |
      4. | ABAD1   18 Apr 12 |
      5. | ABAD1   15 May 12 |
         +-------------------+