楼主: windlove
4204 8

求助:combine two data sets horizontally and duplicatedly [推广有奖]

  • 3关注
  • 2粉丝

已卖:1195份资源

副教授

37%

还不是VIP/贵宾

-

威望
0
论坛币
1562 个
通用积分
55.4072
学术水平
21 点
热心指数
12 点
信用等级
6 点
经验
972 点
帖子
305
精华
0
在线时间
1275 小时
注册时间
2006-3-15
最后登录
2025-6-10

楼主
windlove 发表于 2009-1-17 16:22:00 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
又有问题了。。希望有朋友帮帮忙:
我有2组SAS 数据。比如说
第一组是
1 a
2 b
3 c
4 d
第二组数据有2个group 即:
2 1 x
3 1 y
1 2 u
3 2 v
4 2 w
我想要的结果是把2组数据合并在一起得到:
1 a .
2 b x
3 c y
4 d .
1 a u
2 b  .
3 c v
4 d w
实在想不出。。希望能有高人帮忙 -_-!!!.. 谢谢!!!

[此贴子已经被作者于2009-1-17 17:47:46编辑过]

二维码

扫码加我 拉你入群

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

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

关键词:duplicatedly horizontally horizontal duplicate Horizon Data two combine duplicatedly horizontally

沙发
456852 发表于 2009-1-19 13:24:00
LZ说下你是怎么合并的?合并的原则是什么?

藤椅
456852 发表于 2009-1-19 13:56:00
楼主试试看这个,是不是你要的结果?
data tt;
input x1 x2 $ @@;
cards;
1 a 2 b 3 c 4 d
;
run;
data tt2;
input x1 x3 x4 $ @@;
cards;
1 2 u 2 1 x 3 1 y 3 2 v 4 2 w
;
run;
data tt1;
set tt tt;
run;
proc sort data=tt1;by x1;run;
proc sort data=tt2;by x1;run;
data ttx;
merge tt1 tt2;
by x1;
run;
proc print;run;
data ttx2;
set ttx;
by x1 x3;
if first.x1=0 and first.x3=0 then x4=.;
drop x3;
run;
proc print;run;

板凳
windlove 发表于 2009-1-19 14:14:00
我已经弄出来这个了。找了半天,,最后用了"proc surveyselect". Bootstrap从来没在sas里面做过。。所以从来没用过这个。

我目前在做event study 的工作,学econometrics的人应该知道。
我的真实数据是market index 和 stock price 两个file, market index是每天都有的,而stock price则是,如果哪天没有trading,则data不记录在file里。所以在merge这个两个data的时候一般的方法只会选择存在于stock price file里的日子,而不是"everyday match"。如果stock price里面只有一个stock还好说,问题是一个stock price file 里面有几十个不同的stock。 

so i used "proc surveyselect". maybe not that efficient, but jst got the results anyways.. So if anyone knows anything about merge these two better, please let me know. 

So the "everyday match" problem resovled, the new problem coming along
Once i got the new file with everyday match, the days originally not recorded in the file appear missing values such as. 
day   $
0     2
1     3
2     .
3     .
4     5
5     .
6     4

the stock return  is calculated by ln(P(t)/P(t-n)). 
If you know about trade-to-trade return calculation procedure, then u know I have to count each return interval n. 
so from day 1 to day 4, the return interval is n=3, and from day4 to day6, the return interval n=2. 

Missing Value 的问题也解决了。。我的方法是 n = _n_; if missing($) then missing = 1; if missing = . then lagn = lag(n);
day   $   n missing   lagn          n-lagn
0     2     1     .           .                 .
1     3     2     .           1                1
2     .      3    1           .                 .
3     .      4    1           .                 .
4     5     5     .           2                3
5     .      6    1           .                  .
6     4     7    .           5                  2

感觉上是对的。。不过还请大家帮忙看看。。谢谢。 


[此贴子已经被作者于2009-1-19 15:59:33编辑过]

报纸
456852 发表于 2009-1-19 14:19:00
楼主能否把code贴一下?谢谢了

地板
windlove 发表于 2009-1-19 14:28:00
%macro transform(method='lumped');
data &libref..ASXreturn;
  set &libref..ASXINDEX;
      LastValue = lag(IndexValue);
   IndexReturn = log(IndexValue/LastValue);
 if _n_ in (1) then delete;
   keep DATE IndexReturn;
 if '01JUL1991'd<=DATE<= '08NOV2005'd;
run;
proc surveyselect data=&libref..asxreturn rep=243 n=3644
                    out=IndexReturn;
proc surveyselect data=&libref..firmdata(keep=firmcode EDATE) rep=3644 n=243
                    out=dupfirmdata;
proc sort data=dupfirmdata;
  by firmcode edate;
data repeat;
  merge IndexReturn(keep=DATE IndexReturn) dupfirmdata(keep=firmcode EDATE);
run;

 proc sql;
create table prepared1 as
  select c.*,
   f.EDATE
    from &libref..combine as c
      left outer join
     &libref..firmdata as f
     on c.firmcode = f.firmcode
  where c.firmcode = f.firmcode and volume~=0
 order by FIRMCODE,DATE;
quit;
data prepared2;
 merge &libref..combine  (in = c drop=firmid)
       &libref..firmdata (in = f drop=firmid EDATE);
 by FIRMCODE;
 if c and f;
 if volume=0 then delete;
    lagCODE = lag(FIRMCODE);
     lagclprice = lag(LastTrade);
    if FIRMCODE = lagCODE then do;
      stockreturn = log(LastTrade/lagclprice);
    end;
  else do;
     stockreturn = .;
  end;
  if stockreturn = . then delete;
  drop lagcode lagclprice;
run;
proc sql;
  create table prepared as
   select p1.*,
          p2.stockreturn
   from prepared1 as p1
        left outer join
        prepared2 as p2
   on p1.FIRMCODE = p2.FIRMCODE and p1.DATE = p2.DATE;
proc sort data= prepared;
 by FIRMCODE EDATE;
run;  

proc sql;
 create table output as
   select *
     from repeat as r
          left outer join
          prepared as p
  on r.date = p.date and r.edate=p.edate and r.firmcode=p.firmcode;
data output;
 set output;
 if DATE < EDATE then evndum = 1;
   else evndum = 0;
proc sort data=output;
 by firmcode edate;
 run;

data ready;
  set prepared(keep=date firmcode edate);
  by firmcode;
    if last.firmcode then lobs=1;
    if first.firmcode then fobs=1;
    if fobs=1 then obs=1;
    if lobs=1 then obs=2;
 DATE2=DATE;format DATE2 yymmddn8.;
    if fobs=1 or lobs=1 then output ready;
 drop lobs fobs;
 run;

 proc sql;
 create table output2 as
  select *
   from output
       left join
       ready
 on  output.date=ready.date and output.firmcode=ready.firmcode;
 proc sort data=output2;
 by firmcode edate;
 run;
 data output3;
    set output2;
    if date=date2 and obs=1 then do;
       retain x;
       if ~missing(obs) then x=obs;
         else x=x; 
    end;
  if obs=2 and x=1 then x=2;
data output4;
  set output3;
  where x=1 or (obs=2 and x=2); 
  if (~missing(volume) and missing(StockReturn)) then delete;
  drop x obs date2;
  run;
%if &method = 'lumped' %then %do;
  data &libref..output;
    set output4;
 if Volume = . then Volume = 0;
 if StockReturn = . then StockReturn = 0;
  run;
%end;
%mend;

谢谢你了456852 ,请问能不能帮我顺便看一下新post的问题,关于count missing value的。 谢谢了
其实有的地方还是有问题 。。不过暂时也只能样了。。明天还有report要交。。汗。。。
code看上去很乱。。因为要几个file是同时整合的。。


[此贴子已经被作者于2009-1-19 14:29:55编辑过]

7
456852 发表于 2009-1-19 14:54:00
今天可能不行了,我在美国,现在已经是深夜2点了。。。

8
windlove 发表于 2009-1-19 14:56:00
如果有时间或有兴趣的话,帮我看一下了。。谢谢了。。
我在澳洲,马上也要下班了。。kakaka....

9
zhangwq 发表于 2009-1-21 08:30:00

楼主搞定了不?

麻烦把code贴出来

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

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