0
votes

I have data in the following format:

Month       Date        Time1 Time2 Time3
  Jan      01/01/2018   1234  2345   3456
  Jan      02/01/2018   1234  2346   3457
  Jan      03/02/2018   1234  2346   3145
  Feb      03/02/2018   1234  2347   3458
  Feb      04/02/2018   1235  2398   3456
March      04/03/2018   1235  2347   3456
March      01/03/2018   1236  2345   3456
March      02/03/2018   1234  2567   3897
  Feb      02/02/2018   1236  2346   3478

I want to randomly select observations so that I get two sample dates and numbers from each month and time slot. For example, for Jan, Time 1 I would like to get: 01/01/2018, 1234 and 02/01/2018, 1234.

How can I do this in Stata?

1

1 Answers

2
votes

You can randomly select observations as follows:

clear

input str5 Month str10 Date Time1 Time2 Time3
Jan      "01/01/2018"   1234  2345   3456
Jan      "02/01/2018"   1234  2346   3457
Jan      "03/02/2018"   1234  2346   3145
Feb      "03/02/2018"   1234  2347   3458
Feb      "04/02/2018"   1235  2398   3456
March    "04/03/2018"   1235  2347   3456
March    "01/03/2018"   1236  2345   3456
March    "02/03/2018"   1234  2567   3897
Feb      "02/02/2018"   1236  2346   3478
end

bysort Month (Time1): generate random = runiform()
bysort Month (Time1 random): keep if _n < 3

list, sepby(Month)

     +-------------------------------------------------------+
     | Month         Date   Time1   Time2   Time3     random |
     |-------------------------------------------------------|
  1. |   Feb   03/02/2018    1234    2347    3458   .3576297 |
  2. |   Feb   04/02/2018    1235    2398    3456   .4004426 |
     |-------------------------------------------------------|
  3. |   Jan   02/01/2018    1234    2346    3457   .2076905 |
  4. |   Jan   01/01/2018    1234    2345    3456   .5597356 |
     |-------------------------------------------------------|
  5. | March   02/03/2018    1234    2567    3897   .0286627 |
  6. | March   04/03/2018    1235    2347    3456   .6889245 |
     +-------------------------------------------------------+