具有ID的同一列中的节和子节,如何更改结构

时间:2016-03-02 01:27:50

标签: sas structure

我有一个数据集,其中包含由ID标识的同一列中的部分和子部分,我需要将它放在按列分割的不同结构中以生成报告。 如果不这样做,proc报告会生成重复信息。 我尝试使用retain选项来保留每个子节的最后一个部分,但结果并不是预期的。

这是我拥有的数据集和我想要的数据集以及我想要的报告

有:

data have;
infile datalines
delimiter=','
dsd;
Input ID $ Concept : $15. Amount 15.;
datalines;
1,Store1,85.5
1.1,vend1,43
1.1.1,income,25
1.1.1.1,income 1,10
1.1.1.2,income 2,5
1.1.1.3,income 3,10
1.1.2,Sales,18
1.1.2.1,Sales 1,12
1.1.2.2,Sales 2,6
1.2,Vend2,42.5
1.2.1,income,2.5
1.2.1.1,Comission 1,2.5
1.2.2,Sales,40
1.2.2.1,Sale 1,15
1.2.2.2,Sale 2,15
1.2.2.3,Sale 3,10
2,Store 2,75.6
2.1,Vend 1,18.3
2.1.1,income,15
2.1.1.1,income 1,7
2.1.1.2,income 2,8
2.1.2,Sales,3.3
2.1.2.1,Sales 1,3.3
2.2,Vend 2,57.3
2.2.1,income,7.3
2.2.1.1,Comission 1,5
2.2.1.2,Comission 2,2.3
2.2.2,Sales,0
2.2.3,Others,50
;
run;

想要:

data want;
infile datalines 
delimiter=','
dsd;
input store $ Vend $ Type_1 : $15. Type_2 : $15. Amount 15.;
datalines;
Store 1,vend1,income,income 1,10
Store 1,vend1,income,income 2,5
Store 1,vend1,income,income 3,10
Store 1,vend1,Sales,Sales 1,12
Store 1,vend1,Sales,Sales 2,6
Store 1,Vend2,income,Comission 1,2.5
Store 1,Vend2,Sales,Sale 1,15
Store 1,Vend2,Sales,Sale 2,15
Store 1,Vend2,Sales,Sale 3,10
Store 2,Vend 1,income,income 1,7
Store 2,Vend 1,income,income 2,8
Store 2,Vend 1,Sales,Sales 1,3.3
Store 2,Vend 2,income,Comission 1,5
Store 2,Vend 2,income,Comission 2,2.3
Store 2,Vend 2,Sales,Sales,0
Store 2,Vend 2,Others ,Others ,50
;
run;

最终报告:

PROC REPORT DATA=want nowindows SPANROWS missing
style(report)=[rules=GROUPS frame=box];
column
store Vend Type_1 Type_2 (Amount);
define  store / group  order=data;
define  Vend / group  order=data;
define  Type_1 / group order=data;
define  Type_2 / group order=data;
define Amount /analysis SUM format=dollar15.2;
rbreak after/summarize;
run;

感谢。

1 个答案:

答案 0 :(得分:0)

data want;
    length store $7. Vend $6. Type_1 $15. Type_2 $15. Amount 8.;
    set have;
    length_id = length(ID);
    retain store Vend Type_1;
    if length_id = 1 then store = concept;
    if length_id = 3 then Vend = concept;
    if length_id = 5 then Type_1 = concept;
    if length_id = 7 then Type_2 = concept;
    if length_id = 7 or Amount = 0 or concept = 'Others';
    if Amount = 0 or concept = 'Others' then Type_2 = concept;
    drop ID concept length_id;
run;