0
votes

In Stata, I want to efficiently assign a maximum value of [[bid_price]] from [[other observation]] with these conditions:

  1. [[bid_time]] < [time] < min([[bid_timelimit]], [[bid_timecanceled]])
  2. [[stock]] = [stock]
  3. [[bid_price]] is set

In the above, [[]] denote variables that are from other observations and [] denote variables that are from this observation)

Here is my code:

gen maxbidprice=.

su no

forvalues i = `r(min)'/`r(max)'{
    disp `i'
    gen double current = time[`i']
    egen bidtag=tag(bid_price) if stock==stock[`i'] & bid_price!=. & current>bid_time & current<bid_timelimit & current<=bid_timecanceled
    quietly su bid_price if bidtag
    replace maxbidprice = r(max) if no==`i'
    drop bidtag current
}

I think my code is very inefficient. The size of the data set is over 30k and running time of this code is several hours. It seems to work but I think there should be more efficient code.

And I should not destroy original table and just add one more variable maxbidprice meets certain criteria to all of the observations. The point is to assign a value from other observations matching with some conditions.

Can anyone suggest an alternative?

Sample data:

no,time,price,quantity,seller_pid,buyer_pid,bid_no,bid_price,bid_quantity,bid_time,bid_timelimit,bid_timecanceled,bid_pid,pid,action,stock
300,31oct2012 13:42:03,10000,10,1919,1545,,,,,,,,1919,3,3
301,31oct2012 13:42:03,10000,30,1919,454,,,,,,,,1919,3,3
302,31oct2012 13:42:05,1000,10,,,152,1000,10,31oct2012 13:42:05,04nov2012 00:00:00,31oct2012 13:48:27,2450,2450,1,1
303,31oct2012 13:42:06,10000,10,1919,1545,,,,,,,,1919,3,3
304,31oct2012 13:42:06,10000,20,1919,1252,,,,,,,,1919,3,3
305,31oct2012 13:42:08,10000,18,1919,1648,,,,,,,,1919,3,3
306,31oct2012 13:42:15,10000,4,1919,2151,,,,,,,,2151,4,1
307,31oct2012 13:42:15,10000,10,2450,2151,,,,,,,,2151,4,1
308,31oct2012 13:42:23,6500,15,1919,655,,,,,,,,1919,3,1
309,31oct2012 13:43:58,6000,10,1919,1127,,,,,,,,1919,3,1
310,31oct2012 13:44:15,5000,82,1919,1842,,,,,,,,1919,3,1
311,31oct2012 13:44:41,5000,10,,,153,5000,10,31oct2012 13:44:41,04nov2012 00:00:00,31oct2012 23:36:58,2450,2450,1,1
312,31oct2012 13:46:21,5000,100,,,154,5000,100,31oct2012 13:46:21,16nov2012 00:00:00,01nov2012 00:18:04,1919,1919,1,1
313,31oct2012 13:46:25,5000,3,733,1842,,,,,,,,733,3,1
314,31oct2012 13:46:28,5000,20,,,155,5000,20,31oct2012 13:46:28,02nov2012 00:00:00,31oct2012 14:14:54,1721,1721,1,1
315,31oct2012 13:46:54,7000,10,,,156,7000,10,31oct2012 13:46:54,06nov2012 00:00:00,31oct2012 20:36:08,209,209,1,3
316,31oct2012 13:48:11,9700,10,,,,,,,,,,1373,2,2
317,31oct2012 13:48:14,6000,10,,,157,6000,10,31oct2012 13:48:14,06nov2012 00:00:00,31oct2012 13:55:07,209,209,1,1
318,31oct2012 13:48:55,10000,10,,,,,,,,,,1373,2,3
319,31oct2012 13:49:53,10000,30,,,,,,,,,,1919,2,1
320,31oct2012 13:50:24,9000,50,,,158,9000,50,31oct2012 13:50:24,04nov2012 00:00:00,31oct2012 17:15:46,1919,1919,1,2
321,31oct2012 13:50:29,10000,10,1919,1725,,,,,,,,1725,4,1
322,31oct2012 13:50:42,9000,40,,,159,9000,40,31oct2012 13:50:42,04nov2012 00:00:00,31oct2012 17:15:48,1919,1919,1,3
323,31oct2012 13:51:10,6000,10,,,160,6000,10,31oct2012 13:51:10,04nov2012 00:00:00,31oct2012 14:42:27,2450,2450,1,1
324,31oct2012 13:51:14,10000,20,,,,,,,,,,1919,2,2
325,31oct2012 13:51:23,10000,20,,,,,,,,,,1919,2,2
326,31oct2012 13:51:54,9000,20,,,161,9000,20,31oct2012 13:51:54,04nov2012 00:00:00,31oct2012 17:15:50,1919,1919,1,3
327,31oct2012 13:52:05,10000,8,1725,1648,,,,,,,,1725,3,3
328,31oct2012 13:52:05,10000,2,1725,1648,,,,,,,,1725,3,3
329,31oct2012 13:52:39,9900,10,,,162,9900,10,31oct2012 13:52:39,04nov2012 00:00:00,31oct2012 13:53:16,277,277,1,1
330,31oct2012 13:53:12,9700,10,,,163,9700,10,31oct2012 13:53:12,04nov2012 00:00:00,31oct2012 14:31:31,277,277,1,2
1
So you want to find the maximum bid at any given time for each stock? Can you provide a few rows of data, please? Bonus points for code that generates test data. :) - Richard Herron
@RichardHerron Thank you for your interest, I have just added the sample data - z0nam
Then merge it back. - Richard Herron

1 Answers

3
votes

The following should work. The key is to use collapse and if to find the max bid_price that meets your criteria.

/* make some data */
clear
set seed 2001
set obs 10
generate stock = _n  
expand 100
bysort stock: generate time = _n
expand 100
generate bid_time = time + 10*uniform() - 5
generate bid_timelimit = time + 100*uniform() - 50
generate bid_timecancelled = time + 100*uniform() - 50
generate bid_price = 100 + 50*uniform() - 25

/* find max active bid */
tempfile original_data
save `original_data'
collapse (max) bid_price ///
    if (time > bid_time) & (time < min(bid_timelimit, bid_timecancelled)), ///
    by(stock time)  
merge 1:m stock time using `original_data'

/* check results */    
list in 1/10