SAS中文论坛

 找回密码
 立即注册

扫一扫,访问微社区

查看: 768|回复: 4
打印 上一主题 下一主题

如何在一个string里挑选distinct值?

[复制链接]

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
楼主
 楼主| 发表于 2011-12-31 11:28:08 | 只看该作者

如何在一个string里挑选distinct值?

I.有一段string:  list1=(a 7 a 7 3 3 )
要求去除list1中重复的值,得到list2=(a 7 3 ),以及list1或list2里数值的个数?
如果能排个序就更好了,比如list2=(3 7 a) .
(就好奇一下,会不会有这样的一个function,能直接解决问题?)

II.有一段string:  list2=%str(&x1. &x2. )是在一个do loop过程中产生的,&x1和&x2也都是一个string,他们所含的值的大小和个数也在不断随着do loop而变化.
那么有什么办法能得到list2里数值的个数?

谢谢!!!
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
沙发
 楼主| 发表于 2012-1-4 03:39:25 | 只看该作者

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]
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
板凳
 楼主| 发表于 2012-1-5 14:07:13 | 只看该作者

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).
请帮忙,谢谢!!!
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
地板
 楼主| 发表于 2012-1-6 21:40:15 | 只看该作者

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]
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
5#
 楼主| 发表于 2012-1-7 02:05:24 | 只看该作者

Re: 如何在一个string里挑选distinct值?

thx a lot.
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|手机版|Archiver|SAS中文论坛  

GMT+8, 2025-5-7 02:50 , Processed in 0.069131 second(s), 21 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表