我正试着把这张桌子转过来:
YearMonth ID Purchase Purchase Value
201912 1 Laptop 1000
202012 1 Computer 2000
202112 1 Phone 1000
201912 2 Stereo 500
202012 2 Headset 200要像这样使用PROC转置:
ID Purchase_201912 Purchase_202012 Purchase_202112 PV_201912 PV_202012 PV_202112
1 Laptop Computer Phone 1000 2000 1000
2 Stereo Headset - 500 200 -我想我得转几次才能做到这一点。我试过的第一个转位是:
proc transpose data=query_final out=transpose_1 let;
by yearmonth agent_number;
run;但我一直搞错了
ERROR: Data set WORK.QUERY_FINAL is not sorted in ascending sequence. The current BY group has YearMonth = 202112
and the next BY group has YearMonth = 201912.我已经检查了我从表中提取的数据,这些数据确实是按YearMonth按升序排序的,然后按代理号分组,所以我不知道这个错误指的是什么。可能不是所有的ID都有与它们相关联的YearMonths (例如,上面的ID 2在2021年没有购买任何东西)。
发布于 2022-01-07 12:33:42
假设agent_number与您的示例中的id等效,我复制了这些数据:
data have;
infile datalines4 delimiter="|";
input yearmonth id purchase :$8. PV;
datalines4;
201912|1|Laptop|1000
202012|1|Computer|2000
202112|1|Phone|1000
201912|2|Stereo|500
202012|2|Headset|200
;;;;
quit;您可以使用两个proc transpose,然后合并后者
proc transpose data=have out=stage1(drop=_name_) prefix=Purchase_;
by id;
var purchase;
id yearmonth;
run;
proc transpose data=have out=stage2(drop=_name_) prefix=PV_;
by id;
var PV;
id yearmonth;
run;
data want;
merge stage1 stage2;
run;产生所需的输出
id purchase_201912 purchase_202012 purchase_202112 PV_201912 PV_202012 PV_202112
1 Laptop Computer Phone 1000 2000 1000
2 Stereo Headset 500 200 .PS:为了避免您报告的错误,首先对数据进行排序,方式与proc transpose中的proc transpose语句相同。但是,这里不需要它,因为它已经按照id排序了。
https://stackoverflow.com/questions/70609161
复制相似问题