proc sql noprint;
select nobs into : nobs from sashelp.vtable where libname='WORK' and memname='A' and memtype='DATA';
quit;
data b;
array temp(&nobs) $ 20 ;
do i=1 to total;
set a point=i;
temp(i)=x;
end;
set a nobs=total;
n=0;
do i=1 to total;
if temp(i)=x then n+1;
cnt=count(temp(i),',');
if cnt>0 and temp(i) ne x then do;
do j=1 to cnt+1;
if x=scan(temp(i),j,',') then delete;
end;
end;
end;
keep x y;
run;作者: shiyiming 时间: 2010-12-16 10:27 标题: Re: 求助,数据合并 to jimmy782
非常感谢这位高手,这段程序写得很好,
这段程序之做到了将aaa、bbb、ddd三条记录删除,
还没完全满足我的需求,
还要将aaa和bbb的y变量值累加到aaa,bbb,ccc中,ddd的y变量值累加到ddd,eee中
结果应该是
aaa,bbb,ccc 6
ddd,eee 9
fff 6
望高手再来指导下,谢谢各位了。作者: shiyiming 时间: 2010-12-16 14:37 标题: Re: 求助,数据合并 <!-- s:D --><img src="{SMILIES_PATH}/icon_biggrin.gif" alt=":D" title="Very Happy" /><!-- s:D -->
帮顶!作者: Qiong 时间: 2010-12-16 16:06 标题: Re: 求助,数据合并 data a;
input x : $20. y;
cards;
aaa 1
bbb 2
aaa,bbb,ccc 3
aaa,bbb,ccc,fff 3
ddd 4
ddd,eee 5
fff 6
;
run;
proc sql;
create table exclu as select b.*
from a as a
inner join a as b
on index(trim(a.x),trim(b.x))and length(b.x)<length(a.x) ;
create table vicky as select distinct a.x,sum(b.y) as y_sum
from a as a
left join a as b
on index(trim(a.x),trim(b.x))>0
group by a.x;
create table vicky as select *
from vicky
where x not in (select x from exclu);
quit;
proc print data=vicky;
run;作者: shiyiming 时间: 2010-12-16 16:56 标题: Re: 求助,数据合并 简单修改了下,以前没细看需求,不好意思。
proc sql noprint;
select nobs into : nobs from sashelp.vtable where libname='WORK' and memname='A' and memtype='DATA';
quit;
data b;
array tx(&nobs) $ 20 ;
do i=1 to total;
set a point=i;
tx(i)=x;
end;
array ty(&nobs) $ 20 ;
do i=1 to total;
set a point=i;
ty(i)=y;
end;
set a nobs=total;
n=0;
do i=1 to total;
if tx(i)=x then n+1;
if tx(i) ne x then do;
if index(trim(tx(i)),trim(x))>0 then n+1;
else if index(trim(x),trim(tx(i)))>0 then y+ty(i);
end;
if n>1 then delete;
end;
keep x y;
run;作者: shiyiming 时间: 2010-12-16 21:19 标题: Re: 求助,数据合并 谢谢各位了,这里高手太多了。作者: shiyiming 时间: 2010-12-16 22:35 标题: Re: 求助,数据合并 [code:2hzclyw0]data a;
input x : $20. y;
cards;
aaa 1
bbb 2
aaa,bbb,ccc 3
ddd 4
ddd,eee 5
ccc,eee 6
;[/code:2hzclyw0]
原始数据会出现这种情况吗?作者: shiyiming 时间: 2010-12-17 05:01 标题: Re: 求助,数据合并 [code:3kleg128]data have;
keep x s1; rename s1 =y;
set a;
do point =1 to nobs;
set a (rename =(x =bx y =by)) point =point nobs =nobs;
f1=find(bx, x, 't'); f2 =find(x, bx, 't');
s1 =sum(s1, by*(f2>0));
s2 =sum(s2, (f1>0));
end;
if s2 =1 then output;
run;[/code:3kleg128]作者: shiyiming 时间: 2010-12-17 10:36 标题: Re: 求助,数据合并 楼上的是纵向遍历,如果数据量大的话,效率比较高。原理就是找只出现过一次的记录。
另外问下楼主:如果你数据是这样的话,可能就存在问题了,最长的那条记录出现两条哦,就输出不出来了。需要再做修改。
aaa 1
bbb 2
aaa,bbb,ccc 3
fff,aaa,bbb,ccc 4
fff,aaa,bbb,ccc 4
ddd 4
ddd,eee 5
fff 6作者: shiyiming 时间: 2010-12-19 00:51 标题: Re: 求助,数据合并 很对。我编程没有基础,思虑也欠周详。歉意。我的宗旨是:如果不是好的解答,那就作个反面的教材吧。无论是什么,我倒不介意。
另外,遍历是效率高还是不高的体现?我原以为遍历不可取呢。
京剧作者: shiyiming 时间: 2010-12-19 11:06 标题: Re: 求助,数据合并 哈哈,如果能有适时停止的语句可以提高点效率。
比如,如果该记录在后面遍历的时候遇到第一条包含自己且不等于自己的就停止。
类似的。作者: shiyiming 时间: 2010-12-19 15:12 标题: Re: 求助,数据合并 鄙人觉得这种操作比较SASsy
[code:reu9h6oj]
data a;
input x :$20. val;
cards;
aaa 1
bbb 2
aaa,bbb,ccc 3
fff,aaa,bbb,ccc 4
fff,aaa,bbb,ccc 4
ddd 4
ddd,eee 5
fff 6
;
run;
data master sat(index=(x1) rename=(x=x1 val=val1));
set a;
if find(x, ',')>0 then output master;
else output sat;
run;
data master;
modify master;
i=1;
x1=scan(x, i, ',');
do while (x1^='');
do until (_iorc_=%sysrc(_dsenom));
set sat key=x1;
select (_iorc_);
when(%sysrc(_sok)) val=val+val1;
when(%sysrc(_dsenom)) _error_=0;
otherwise put "ERROR: I/O MALFUNCTION";
end;
end;
i=i+1;
x1=scan(x, i, ',');
end;
run;
[/code:reu9h6oj]