我正在从SAS Programming 2教科书中进行练习。
我正在尝试转换此数据:
窄数据集像这样的宽数据集:
宽数据集我还应该在我的数据步骤中有一个数组,并且只输出变量customer_id和month1到month12。
我的代码如下:
Data customer_orders(keep=Customer_ID month1-month12);
set orion.order_summary;
by customer_id;
array month{12} month1-month12;
do i= 1 to 12;
if order_month = i then
month{i}= sale_amt;
end;
run;
proc print data=customer_orders;
run;
当我运行此代码时,我的问题是观察值未在一个观察值中显示customer_id的所有sale_amt值,而是跳至下一行以显示在观察值中找到的第二个值。
任何帮助将不胜感激。
注意:不允许将其他链接发布到输出的外观。
如前所述,由于SAS在处理步骤中将值重置为丢失,因此您需要设置一条keep语句将您的值传递到下一行。然后,Last.cust_id仅接受每个客户ID的最后一行,并且该行应包含您对该客户的所有观察结果。
但是,此后将保留它们的所有值,直到另行指定为止。因此,使用first.cust_id可以将每个新客户ID的所有值都设置为missing。
data test;
input Cust_id Month Sale_Amt;
Datalines;
5 5 478
5 6 126.8
5 9 52.50
5 12 33.8
10 3 32.60
10 1 200
;
run;
proc sort data = test out = test_sort;
by cust_id month;
run;
data test2 ( drop = month sale_amt i );
set test_sort;
by cust_id;
array holder (*) Month1-Month12;
retain Month1-Month12;
do i = 1 to 12;
if first.cust_id then holder{i} = .;
if month = i then holder{i} = sale_amt;
end;
if last.cust_id;
run;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句