您的位置:首页 > 其它

【学习总结】merge的用法

2013-05-07 22:55 495 查看
/*一对一合并,不需要by语句*/
data a;
merge resdat.class resdat.stk000001;
run;

(朱世武,126)

 

/*匹配合并必须有by语句*/
data a;
merge resdat.stk000001 (keep=date clpr rename=(clpr=clpr000001))
resdat.stk000002 (keep=date clpr rename=(clpr=clpr000002));
by date;
run;

(朱世武,126)

/*使用数据集选项(in=)*/
data a5;
input x y$ @@;
cards;
1 a 1 b 1 c 2 x 3 y
;
run;

data a6;
input x y$ @@;
cards;
1 aa 2 xx 4 yy
;
run;

data a56;
merge a5(in=ina) a6(in=inb);
by x;
in_a=ina;
in_b=inb;
run;
/*The IN=data set option is a flag to the variable of the BY group instead of any other variables.*/
/*Because the variable x(not y) is in the BY group,so although the values(b and c) of y in table5 do not exist in table6, the flags do not change.*/
/*如果merge的数据集中有相同变量名,那么merge后的变量值取merge语句中最右边数据集的那一个*/

(人大经济论坛,http://t.cn/zTHMUJM

/*小训练,举一反三*/

/*建立测试数据集*/
data test;
input num $ age salary;
datalines;
01 28 6200
02 30 8000
01 29 7500
03 31 9000
05 32 10000
08 26 7000
;
proc sort;
by num;
run;

/*用merge语句取出id为01、03、08的记录*/
data tmp1;
input num$@@;
datalines;
01 03 08
;
proc sort;
by num;
run;

data select;
merge test(in=ind1) tmp1(in=ind2);
in_a=ind1;
in_b=ind2;
by num;
if in_a & in_b;
run;

(自编)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  sas merge