SAS中文论坛

标题: 求助宏变量中如何去除重复的 [打印本页]

作者: shiyiming    时间: 2009-10-19 16:45
标题: 求助宏变量中如何去除重复的
%let aaa=a b c a c d f;
如何去除重复的,得到a b c d f!
作者: shiyiming    时间: 2009-10-19 23:58
标题: Re: 求助宏变量中如何去除重复的
[code:138d25jz]%let aaa=a b c a c d f a d d f;
%macro nodup(var);
%local i temp;
%let i=1;
%let temp=%scan(&&&var,&i);
%do %while(&temp ne);
        %let &var=&temp %sysfunc(tranwrd(&&&var,&temp,));
        %let i=%eval(&i+1);
        %let temp=%scan(&&&var,&i);
%end;
%let &var=%sysfunc(reverse(&&&var));
%mend;

%nodup(aaa);

%put &aaa;[/code:138d25jz]
作者: shiyiming    时间: 2009-10-20 01:50
标题: Re: 求助宏变量中如何去除重复的
[code:3mzh4916]%let aaa=a b c a c d f d d1;
%macro removeMcr;
   %local f1 f2 aaa1 aa;
        %let f1 =;        %let aaa1 =&aaa; %let i =1;
        %do%while(%scan(&aaa, &i) ^=%str( ));
                %let f =%scan(&aaa, &i);
                %let f1 =&f1 &f; %let aa =; %let j =1;
                %do%while(%scan(&aaa1, &j) ^=%str( ));
                        %let f2 =%scan(&aaa1, &j);
                        %if  &f2 =&f  %then %let f2=;
                        %let aa =&aa &f2; %let j =%eval(&j+1);
                %end;
                %let aaa1 =&aa; %let aaa =&f1 &aaa1; %let i =%eval(&i+1);
                %put **&i **f=&f  **f1=&f1 **aaa1=&aaa1 **aaa=&aaa;
        %end;
%Mend removeMcr;

%removeMcr
;
%put &aaa
;
[/code:3mzh4916]
不知道为什么tranwrd在macro里改变功能?所以只好用循环去替代
作者: shiyiming    时间: 2009-10-20 09:54
标题: Re: 求助宏变量中如何去除重复的
[code:3ok8u4pd]%macro nodup(var);
    data temp;
        do i=1 to length(compbl("&var"))-Length(compress("&var"))+1 ;
        word=put(scan("&var",i), $10.);
        output;
        end;
     run;
     proc sql;
        select distinct word into :var separated by ' '
        from temp;
    %put var=&var;
%mend;

%nodup(a b c a c d f a d d f);[/code:3ok8u4pd]
作者: shiyiming    时间: 2009-10-20 10:28
标题: Re: 求助宏变量中如何去除重复的
谢谢jingju11的建议,tranwrd 很难用。。。想办法用上去了。。。还是ahuige的思想厉害。。。
[code:2nplq44m]%let aaa=a b c a c d f a d d f1 a _F1 abc;
%macro nodup(var);
%local temp left;
%let temp=%scan(&&&var,1);
%let left=%sysfunc(tranwrd(&&&var,%str( ),%str(  )));
%let &var=;
%do %until(&temp eq );
        %let left=%sysfunc(tranwrd(%str( )&left%str( ),%str( )&temp%str( ),%str( )));
        %let &var=&&&var &temp ;
        %let temp=%scan(&left,1);
%end;
%mend;

%nodup(aaa);

%put &aaa; [/code:2nplq44m]
作者: shiyiming    时间: 2009-10-20 20:35
标题: Re: 求助宏变量中如何去除重复的
to Ahuige:
运行 %nodup(a c b c a c d f a d d f);
结果仍然是a b c d e f,也就是sql 对结果排序。如何保留 a c b d e f 呢?
作者: shiyiming    时间: 2009-10-20 22:28
标题: Re: 求助宏变量中如何去除重复的
[code:3850i3n8]%macro nodup(var);
    data temp;
        do i=1 to length(compbl("&var"))-Length(compress("&var"))+1 ;
        word=put(scan("&var",i), $10.);
        output;
        end;
     run;
     proc sql;
        select word into :var separated by ' '
        from temp
        group by word
        having i=min(i)
        order by i;
    %put var=&var;
%mend;

%nodup(a c b c a c d f a d d f);[/code:3850i3n8]
作者: shiyiming    时间: 2009-10-20 22:44
标题: Re: 求助宏变量中如何去除重复的
还需要保持原顺序啊 <!-- s:shock: --><img src="{SMILIES_PATH}/icon_eek.gif" alt=":shock:" title="Shocked" /><!-- s:shock: -->
试试这样行么...
[code:2fl66mq2]%let aaa=a c a b c   d f;
%macro nodup(&amp;aaa);
data aa;
  do i=1 to count(&quot;&amp;aaa&quot;,&quot; &quot;)+1;
    i=i;
    x=scan(&quot;&amp;aaa&quot;,i,&quot; &quot;);
    output;
  end;
run;
proc sort nodupkey;
  by x;
run;
proc sort;
  by i;
run;
data _null_;
  set aa end=last;
  retain xx;
  format xx $250&#46;;
  xx=compbl(xx||&quot; &quot;||x);
  if last then call symput(&quot;var&quot;,xx);
run;
%mend;
%put &amp;var;[/code:2fl66mq2]
作者: shiyiming    时间: 2009-10-21 07:06
标题: Re: 求助宏变量中如何去除重复的
[code:6yrru0qn]%macro nodup(var);
    %let shortvar=;
    %do %while (%scan(&amp;var,1) ne );
    %if ( %index(&amp;shortvar,%str( )%scan(&amp;var,1)%str( ))=0)%then %let shortvar=&amp;shortvar%str( )%scan(&amp;var,1)%str( );
    %if %index(&amp;var,%str( ))&gt;0 %then %let var=%substr(&amp;var,%index(&amp;var,%str( )))%Str( );
    %end;
    %put shortvar=%sysfunc(compbl(&amp;shortvar));
%mend;

%nodup(a c b c a c d f a d d f);[/code:6yrru0qn]
作者: shiyiming    时间: 2009-10-21 12:00
标题: Re: 求助宏变量中如何去除重复的
%macro distinctlist(list);
  %local dlist i;
  %let dlist=%scan(&amp;list,1,%str( ));
  %let i=2;
  %do %while(%scan(&amp;list,&amp;i,%str( )) ne %str( ));
         %if %sysfunc(indexw(&amp;dlist,%scan(&amp;list,&amp;i,%str( ))))=0 %then %do;
                 %let dlist= &amp;dlist %scan(&amp;list,&amp;i,%str( ));
             %end;
                 %let i=%eval(&amp;i+1);
%end;
%put &amp;dlist;
%mend;
%distinctlist(%str(ab c d e a a ));
作者: shiyiming    时间: 2009-10-21 12:32
标题: Re: 求助宏变量中如何去除重复的
受启发,D版一个
还是喜欢弄到data步中弄..
[code:23ou655x]%let var=a c b c a c d f a d d f;
%macro nodup(var);
data _null_;
  lst=scan(&quot;&amp;var&quot;,1);
  do i=2 to count(&quot;&amp;var&quot;,&quot; &quot;)+1;
    if index(lst,scan(&quot;&amp;var&quot;,i,&quot;&quot;))=0 then lst=compbl(lst||&quot; &quot;||scan(&quot;&amp;var&quot;,i,&quot; &quot;));
  end;
  call symput(&quot;lst&quot;,lst);
run;
%mend nodup;
%put &amp;lst;[/code:23ou655x]
作者: shiyiming    时间: 2009-10-27 12:29
标题: Re: 求助宏变量中如何去除重复的
[code:3nys9kfe]%macro avg/parmbuff;
%let num=1;
%let newchar=;
%let char=%scan(&amp;syspbuff,&amp;num);
%do %while(&amp;char ne);
   %if &amp;num=1 %then %let newchar=&amp;char;
   %else %do;
      %if  %index(&amp;newchar,&amp;char) %then %let newchar=&amp;newchar;
      %else %let newchar=%bquote(&amp;newchar &amp;char);
          %end;
   %let num=%eval(&amp;num+1);
   %let char=%scan(&amp;syspbuff,&amp;num);
%end;
%put &amp;newchar;
%mend;
%avg(a b d c a d e f e bd e eoi)
[/code:3nys9kfe]
作者: shiyiming    时间: 2009-10-27 19:07
标题: Re: 求助宏变量中如何去除重复的
与smand008 英雄所见略同 可能还是要用indexw




欢迎光临 SAS中文论坛 (https://mysas.net/forum/) Powered by Discuz! X3.2