全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
1707 2
2020-03-27
悬赏 10 个论坛币 已解决
SAS

已知数据如下,当Namet和Stockcode两个变量都相同时进行合并:
Name       Stockcode     Quantity     Price
a                1                    100           5
a                2                    200           6

a                2                    100           6

b                3                    300           7

b                3                    100           7

c                1                    500           5

想要效果如下:
Name       Stockcode     Quantity     Price
a                1                    100           5
a                2                    300           6
b                3                    400           7
c                1                    500           5

最佳答案

Rock2000 查看完整内容

data a; input Name $ Stockcode Quantity Price; cards; a 1 100 5 a 2 200 6 a 2 100 6 b 3 300 7 b 3 100 7 c 1 500 5 ; run; ...
二维码

扫码加我 拉你入群

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

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

全部回复
2020-3-27 21:31:28
data a;
input Name $  Stockcode Quantity Price;
cards;
a                1                    100           5
a                2                    200           6
a                2                    100           6
b                3                    300           7
b                3                    100           7
c                1                    500           5
;
run;

data b; set a;
id=cats(name,Stockcode);
run;

proc sql;
create table test as
select Name,Stockcode, Price, id,
sum(Quantity) as cum_Quantity  
from b
group by id;
quit;

proc sort data=test nodupkeys out=c;by id;run;

data result;set c;Quantity=cum_Quantity;  drop id cum_Quantity; run;
二维码

扫码加我 拉你入群

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

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

2020-3-28 02:36:53
Here is another way, which use retain statement:

data a;
input Name $  Stockcode Quantity Price;
cards;
a                1                    100           5
a                2                    200           6
a                2                    100           6
b                3                    300           7
b                3                    100           7
c                1                    500           5
;
run;

proc print data=a;
run;

data b;
set a;
Name_stockcode=compress(name||Stockcode);
run;
proc print data=b;
run;

proc sort data=b out=c;
by Name_stockcode;
run;

data d;
set c;
by Name_stockcode;
retain cn_Quantity;
if first.Name_stockcode then do;
cn_Quantity=.;
end;
cn_Quantity+Quantity;
if last.Name_stockcode then do;
keep
name
stockcode
cn_Quantity
price;
output;
end;
run;

proc print data=d;
run;
二维码

扫码加我 拉你入群

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

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

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群