第一。和最后。在sas

时间:2015-05-31 10:22:31

标签: sas

我的数据表如下:

Name Amount Status
a 5000 Debit
a 2000 Credit
a 1000 Credit
b 2000 Debit
b 1000 Debit
b 1000 Credit

我希望我的输出为:

Name net_amount status
a 2000 Debit
b 2000 Debit

如何使用first.last.变量实现目标? 提前致谢

1 个答案:

答案 0 :(得分:0)

data a;
input Name $ Amount Status $;
cards;
a 5000 Debit
a 2000 Credit
a 1000 Credit
b 2000 Debit
b 1000 Debit
b 1000 Credit
;
run;

proc sort data=a; 
by name status amount; 
run;

data by_name_first_obs by_name_and_status_first_obs
     by_name_last_obs by_name_and_status_last_obs;
set a;
by name status;
if first.name then output by_name_first_obs;
if last.name then output by_name_last_obs;
if first.status then output by_name_and_status_first_obs;
if last.status then output by_name_and_status_last_obs;
run;