楼主: zhangwq
3246 6

复杂的股票数据合并问题求助 [推广有奖]

  • 0关注
  • 1粉丝

大专生

63%

还不是VIP/贵宾

-

威望
0
论坛币
0 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
640 点
帖子
27
精华
0
在线时间
63 小时
注册时间
2007-1-30
最后登录
2025-1-2

楼主
zhangwq 发表于 2009-1-23 22:26:00 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

各位大侠,本人有如下问题求助:

(1)我有数据表1如下,记录了各个账户买卖股票的信息。对于每个账户下的每只股票,都只有买入和卖出时的日期及价格;

(2)我有数据表2,记录每只股票每个交易日的开盘、收盘、最高、最低等价格信息;

(3)我希望将表2的部分数据合并到表1中——每个账户每只股票(从买入到卖出后)的每个交易日的最高、最低价。

例如:对于账户“1001”,我希望得到合并后股票“600001”2007年1月5日至2007年4月12日间所有交易日的股票价格;

(4)困难1是连个表合并时需要两个by变量(股票代码、交易日期);困难2是账户“1001”和“1002”都有“600001”的交易,在按账户、股票代码排序时股票代码非自然排序

表1

账户 交易类型 股票代码 交易数量 交易日期 交易价格
1001  买入 600001 1000 2007年1月5日 5.00
1001  卖出 600001 1000 2007年4月12日 6.00
1001  买入 600002 500 2007年4月16日 4.00
1001  卖出 600002 500 2007年6月22日 3.00
1001  买入 600005 3000 2007年4月16日 5.00
1001  卖出 600005 3000 2007年4月24日 7.00
1002  买入 600001 500 2007年4月24日 3.00
1002  卖出 600001 500 2007年4月26日 4.00
1002  买入 600018 1500 2007年1月28日 5.00
1002  卖出 600018 1500 2007年6月13日 7.00
1002  买入 600039 5000 2007年1月4日 7.00
1002  买入 600039 2500 2007年2月27日 9.00
1003  买入 600002 600 2007年3月5日 7.00
1003  卖出 600002 600 2007年1月19日 6.00
1003  买入 600108 200 2007年1月31日 6.00
1003  卖出 600108 200 2007年3月27日 8.00

[此贴子已经被作者于2009-1-23 22:29:17编辑过]

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:股票数据 数据合并 股票代码 交易类型 部分数据 股票 数据合并

回帖推荐

yongyitian 发表于7楼  查看完整内容

Since you have a large number of data, I suggest to create other datasets that holds the data of different case. For exampe, I modified code to output the data you added for 'sell - buy' to a dataset OTHERSTOCK (see output). Then you can copy/paste/modify the code to fill the missing date values in OTHERSTOCK. To do so, you  need to change the 'b' to 's ...

yongyitian 发表于4楼  查看完整内容

The following code add the missing date observations for the stock that were 'bought and sold at the same amount'. You may need to modify the code for that were bought/sold only.data test;input account $ trade  $ code $ qtr Tdate : date9. price;datalines;1001  b 600001 1000 05JAN2007 5.00 1001  s 600001 1000 12APR2007 6.00 1001  b 600002 500 16APR2007 4.00 1001  s 600002 5 ...

本帖被以下文库推荐

沙发
zhangwq 发表于 2009-1-23 22:28:00

问题重新表述

我的问题等于是:

每个账户的每只股票交易的买入(A日)和卖出日(B日)间填充上“该股票交易日(从A+1到B-1日)”。

[此贴子已经被作者于2009-1-23 22:35:11编辑过]

藤椅
yongyitian 发表于 2009-1-24 00:18:00
表2在哪里?

板凳
yongyitian 发表于 2009-1-24 03:11:00

The following code add the missing date observations for the stock that were 'bought and sold at the same amount'. You may need to modify the code for that were bought/sold only.

data test;
input account $ trade  $ code $ qtr Tdate : date9. price;
datalines;
1001  b 600001 1000 05JAN2007 5.00
1001  s 600001 1000 12APR2007 6.00
1001  b 600002 500 16APR2007 4.00
1001  s 600002 500 22JUN2007 3.00
1001  b 600005 3000 16APR2007 5.00
1001  s 600005 3000 24APR2007 7.00
1002  b 600001 500 24APR2007 3.00
1002  s 600001 500 26APR2007 4.00
1002  b 600018 1500 28JAN2007 5.00
1002  s 600018 1500 13JUN2007 7.00
1002  b 600039 5000 4JAN2007 7.00
1002  b 600039 2500 27FEB2007 9.00
1003  b 600002 600 5MAR2007 7.00
1003  s 600002 600 19JAN2007 6.00
1003  b 600108 200 31JAN2007 6.00
1003  s 600108 200 27MAR2007 8.00
;
run;
proc print data=test;
format tdate mmddyy10.;
run;
proc sort data=test out=datasorted;
   by account code trade tdate;
run;
proc print data=datasorted;
    format tdate date9.;
run;

data test3 (drop= i);
   set datasorted;
   by account code;
   retain BuyDate;
   if first.code and trade ='b'  then
      do;
          BuyDate = tdate;
          output;
   end;
   if last.code and trade = 's' then
          do;  SoldDate = tdate;
             do i = BuyDate to SoldDate-1;
                tdate = i+1;
    trade = 'h/s';
    output;
             end;
       end;    
 else output;
run;
proc sort data=test3 nodup;
by account;
run;

proc print data=test3 ;
     format tdate BuyDate SoldDate date9.;
  where account = '1001' and code = '600005';
run;

                                        The SAS System        10:53 Friday, January 23, 2009 231

    Obs    account    trade     code      qtr        Tdate    price      BuyDate     SoldDate

    167     1001       b       600005    3000    16APR2007      5      16APR2007            .
    168     1001       h/s     600005    3000    17APR2007      7      16APR2007    24APR2007
    169     1001       h/s     600005    3000    18APR2007      7      16APR2007    24APR2007
    170     1001       h/s     600005    3000    19APR2007      7      16APR2007    24APR2007
    171     1001       h/s     600005    3000    20APR2007      7      16APR2007    24APR2007
    172     1001       h/s     600005    3000    21APR2007      7      16APR2007    24APR2007
    173     1001       h/s     600005    3000    22APR2007      7      16APR2007    24APR2007
    174     1001       h/s     600005    3000    23APR2007      7      16APR2007    24APR2007
    175     1001       h/s     600005    3000    24APR2007      7      16APR2007    24APR2007

已有 1 人评分经验 论坛币 收起 理由
bakoll + 3 + 3 精彩帖子

总评分: 经验 + 3  论坛币 + 3   查看全部评分

报纸
zhangwq 发表于 2009-1-24 21:31:00

非常好的方案

方案简单易行,这就是我想要的。非常感谢yongyitian!

地板
zhangwq 发表于 2009-1-31 09:25:00

合并日期新问题求助

在实际应用中我又发现问题:

(1)如果一只股票被多次买卖,其日期如何填充。

举例如下:先卖,后买,我需要填充的是这四个时点中间的日期;。

1001  s 600001 1000 05JAN2007 5.00
1001  b 600001 1000 12APR2007 6.00
1001  s 600002 500 16APR2007 4.00
1001  b 600002 500 22JUN2007 3.00

2)如何处理一日内多次交易问题

举例如下,一天内有多次交易,在填充时如何处理

1001  s 600001 1000 05JAN2007 5.00
1001  b 600001 1000 05JAN2007 5.50
1001  s 600002 500 05JAN2007 5.50
1001  b 600002 500 05JAN2007 5.00

由于数据量较大,无法一一判别

7
yongyitian 发表于 2009-2-1 02:48:00

Since you have a large number of data, I suggest to create other datasets that holds the data of different case. 

For exampe, I modified code to output the data you added for 'sell - buy' to a dataset OTHERSTOCK (see output).

Then you can copy/paste/modify the code to fill the missing date values in OTHERSTOCK. To do so, you  need to change the 'b' to 's' and 's' to 'b' in the if condition statement. Please note that the observation of Code=600039 is still in the dataset BuySell.  Hope this would help.

modified code
data test;
input account $ trade  $ code $ qtr Tdate : date9. price;
datalines;
1001  b 600001 1000 05JAN2007 5.00
1001  s 600001 1000 12APR2007 6.00
1001  b 600002 500 16APR2007 4.00
1001  s 600002 500 22JUN2007 3.00
1001  b 600005 3000 16APR2007 5.00
1001  s 600005 3000 24APR2007 7.00
1002  b 600001 500 24APR2007 3.00
1002  s 600001 500 26APR2007 4.00
1002  b 600018 1500 28JAN2007 5.00
1002  s 600018 1500 13JUN2007 7.00
1002  b 600039 5000 4JAN2007 7.00
1002  b 600039 2500 27FEB2007 9.00
1003  b 600002 600 5MAR2007 7.00
1003  s 600002 600 19JAN2007 6.00
1003  b 600108 200 31JAN2007 6.00
1003  s 600108 200 27MAR2007 8.00
1001  s 600001 1000 05JAN2007 5.0   
1001  b 600001 1000 12APR2007 6.0
1001  s 600002 500 16APR2007 4.0
1001  b 600002 500 22JUN2007 3.0
;
run;
proc print data=test;
format tdate mmddyy10.;
run;
proc sort data=test out=datasorted;
   by account code trade tdate;
run;
proc print data=datasorted;
    format tdate date9.;
run;

data Buy_Sell (drop= i) OtherStock (drop=i SellDate BuyDate);
   set datasorted;
   by account code;
   retain BuyDate;
   if first.code and trade ='b' then
      do;
      if first.code ne last.code then
          BuyDate = tdate;
          output Buy_Sell;
   end;
   else if last.code and trade = 's' then
          do;  SellDate = tdate;
             do i = BuyDate to SellDate-1;
                tdate = i+1;
    trade = 'h/s';
    output Buy_Sell;
             end;
       end;    
 else output OtherStock;
run;
proc sort data=Buy_Sell nodup;
by account;
run;

proc print data=otherstock;
   format tdate date9.;
run;

     data of OtherStock
    Obs    account    trade     code      qtr        Tdate    price

     1      1001        b      600001    1000    12APR2007      6
     2      1001        s      600001    1000    05JAN2007      5
     3      1001        b      600002     500    22JUN2007      3
     4      1001        s      600002     500    16APR2007      4
     5      1002        b      600039    2500    27FEB2007      9    

已有 1 人评分经验 论坛币 收起 理由
bakoll + 3 + 3 精彩帖子

总评分: 经验 + 3  论坛币 + 3   查看全部评分

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-29 01:10