PROC 按 YearMonth 和 ID 转置重复值
PROC Transpose by YearMonth and ID for duplicate values
我正在尝试转置这个 table:
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 Transpose 看起来像这样:
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.
我检查了我从 table 中提取的数据确实按 YearMonth 升序排序,然后按代理编号分组,所以我不确定这个错误指的是什么.难道不是所有 ID 都具有相同的 YearMonths 与之关联(即在上面的示例中,ID 2 在 2021 年没有购买任何东西)。
假设 agent_number
等同于 id
在你的例子中,我转载了数据:
data have;
infile datalines4 delimiter="|";
input yearmonth id purchase :. 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
中的by
语句相同。但是,这里不需要它,因为它已经按 id
.
排序了
我正在尝试转置这个 table:
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 Transpose 看起来像这样:
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.
我检查了我从 table 中提取的数据确实按 YearMonth 升序排序,然后按代理编号分组,所以我不确定这个错误指的是什么.难道不是所有 ID 都具有相同的 YearMonths 与之关联(即在上面的示例中,ID 2 在 2021 年没有购买任何东西)。
假设 agent_number
等同于 id
在你的例子中,我转载了数据:
data have;
infile datalines4 delimiter="|";
input yearmonth id purchase :. 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
中的by
语句相同。但是,这里不需要它,因为它已经按 id
.