0

在Stata中,我想有效地从[[其他观察]]分配[[bid_price]]的最大值,条件如下:

  1. [[bid_time]] < [time] < min( [[bid_timelimit]] , [[bid_timecanceled]] )
  2. [[库存]] = [库存]
  3. [[bid_price]]已设置

在上面,[[]]表示来自其他观察的变量, []表示来自该观察的变量)

这是我的代码:

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
}

我认为我的代码效率很低。数据集的大小超过 30k,这段代码的运行时间是几个小时。它似乎有效,但我认为应该有更有效的代码。

而且我不应该破坏原始表,而只是在所有观察结果中再添加一个符合某些标准的变量 maxbidprice。关键是从与某些条件匹配的其他观察中分配一个值。

任何人都可以提出替代方案吗?

样本数据:

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
4

1 回答 1

3

以下应该工作。关键是使用collapseif找到bid_price符合您标准的最大值。

/* 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   
于 2013-07-12T20:53:56.607 回答