我是SAS新手并拥有一个名为ORIG_DATA的简单数据集,我需要根据该数据集创建一个新的数据集摘要,按照Day_ID显示按Salesman_ID计算的总数
本质上,SUMMARY输出应该如下所示,其中数字是总数的总和。
Salesman_ID|Day_1|Day_2
A |30 |40
B |60 |0
C |20 |70
在SQL中,我做
Select salesman_id,
sum(case when day_id=1 then total else 0 end) as day_1,
sum(case when day_id=2 then total else 0 end) as day_2
from ORIG_DATA group by salesman_id
但是对于这个问题,我不允许使用proc sql。我怎么能在SAS中做到这一点?哈文目前最模糊。 对不可格式的道歉
ORIG_DATA如下
Day_ID|Salesman_ID|Other_field|total
1 |A |R000 |10
1 |A |R002 |20
2 |A |R000 |10
2 |A |R004 |30
1 |B |R002 |20
1 |B |R000 |40
1 |B |R004 |0
2 |C |R003 |40
2 |C |R004 |10
1 |C |R002 |20
2 |C |R002 |20
答案 0 :(得分:1)
您可以通过简单的数据步骤解决问题,请参阅下面的代码。 您需要先对数据进行排序,然后指示数据与您在新组开始时将day_1和day_2重置为零的组一起使用,并输出到上次观察时的数据集。
如果您有任何疑问,请与我们联系。
<a style="color:white" id="randomNo">Hello</a>;
答案 1 :(得分:0)
这个怎么样?我不知道每other_field
每salesman_id
只有day_id
个data ORIG_DATA ;
input Day_ID Salesman_ID $ Other_field $ total ;
cards ;
1 A R000 10
1 A R002 20
2 A R000 10
2 A R004 30
1 B R002 20
1 B R000 40
1 B R004 0
2 C R003 40
2 C R004 10
1 C R002 20
2 C R002 20
;run;
个记录。以下内容适用于1到n条记录:
输入数据:
proc sort data=ORIG_DATA ;
by salesman_id day_id ;
proc transpose data=ORIG_DATA out=D1 ;
by salesman_id day_id ;
var total ;
run ;
data D2 ;
set D1 ;
array D(*) col: ;
_name_=cats('day_',day_id) ;
by salesman_id day_id;
total=sum(of D(*)) ;
run ;
proc transpose data=D2 out=SUMMARY(drop=_name_) name=_name_;
by salesman_id ;
var total ;
run ;
*Add zeros for missing values ;
data SUMMARY ;
set SUMMARY ;
array days day_: ;
do over days ;
if missing(days) then days=0;
end ;
run ;
转置,求和并转置回来:
{{1}}
答案 2 :(得分:0)
其他方法:
proc summary data=orig_data nway;
class day_id salesman_id;
var total;
output out=sum(drop=_:) sum=;
run;
proc sort data=sum;
by salesman_id day_id;
run;
proc transpose data=sum out=want(drop=_name_) prefix=day_;
by salesman_id;
var total;
run;
答案 3 :(得分:0)
类似的一个:
proc sort data = orig_data(drop = Other_field);
by salesman_id day_id;
run;
data test (drop = total);
retain salesman_id day_id;
set orig_data ;
by salesman_id day_id notsorted;
if first.day_id then sum = total;
else sum + total;
if last.day_id then output;
run;
proc transpose data = test out = t(drop=_:) prefix = day_id_;
by salesman_id;
id day_id;
var sum;
run;