谢谢!!!作者: shiyiming 时间: 2012-1-4 03:39 标题: Re: 如何在一个string里挑选distinct值? Only for your question I (not understanding II)
[code:1y8nmm75]data a;
str='a 3 3 b a c 1 w w 00 1';
run;
data b(keep=str str_1);
set a;
array tmp{100} $1. _temporary_;
len=length(compress(str));
do k=1 to len;
tmp(k)=substr(compress(str),k,1);
end;
c='';
do i=1 to len-1;
do j=i+1 to len;
if tmp(i) > tmp(j) then do;
c=tmp(i);
tmp(i)=tmp(j);
tmp(j)=c;
end;
end;
end;
do i=1 to len-1;
do j=i+1 to len;
if tmp(i)=tmp(j) then tmp(j)='';
else leave;
end;
end;
length str_1 $100.;
do i=1 to len;
str_1=cats(str_1,tmp(i));
end;
str_1=compress(str_1);
run;[/code:1y8nmm75]作者: shiyiming 时间: 2012-1-5 14:07 标题: Re: 如何在一个string里挑选distinct值? 这个string是不规则的(对不起,给的例子太典型了).
以这个为准:
list1=(0 aaaa 3 th 1234 0 a3b4 3 aa 1234 0 aa)
想得到list2=(0 aaaa 3 th 1234 a3b4 aa).
请帮忙,谢谢!!!作者: shiyiming 时间: 2012-1-6 21:40 标题: Re: 如何在一个string里挑选distinct值? the sub-string in the list is not sorted. If you want so, please refer to the method of bubble sorting above.
[code:1u06edel]data a;
raw_str=' 000 aaaa 3 th 1234 000 a3b4 3 aa 1234 0 aa ';
str=compbl(left(trim(raw_str)));
run;
data b(drop=i);
set a;
sub_str_cnt=0;
do i=1 to length(str);
if substr(str,i,1)=' ' then sub_str_cnt=sub_str_cnt+1;
end;
sub_str_cnt=sub_str_cnt+1;
run;
data _null_;
set b end=last_rec;
retain array_size .;
if array_size<sub_str_cnt then array_size=sub_str_cnt;
if last_rec then call symput('n', array_size);
run;
%put &n;
data c(drop=i j k sub:);
set b;
array sub_str{&n} $10. /*_temporary_*/;
i=1;
j=1;
do k=1 to sub_str_cnt-1;
do while (i<length(str));
if substr(str,i,1)=' ' then do;
sub_str(k)=substr(str,j,i-j);
i=i+1;
j=i;
leave;
end;
i=i+1;
end;
end;
sub_str(sub_str_cnt)=substr(str,j,length(str)-j+1);
do i=1 to &n.-1;
do j=i+1 to &n;
if sub_str(i)=sub_str(j) then sub_str(j)='';
end;
end;
length mod_str $100.;
mod_str='';
do i=1 to &n;
mod_str=compbl(left(trim(mod_str))||' '||compress(sub_str(i)));
end;
run;
[/code:1u06edel]作者: shiyiming 时间: 2012-1-7 02:05 标题: Re: 如何在一个string里挑选distinct值? thx a lot.