楼主: ausser
3904 12

[问答] [求助] PROC SQL 生成关于同一变量各观测的排列组合 [推广有奖]

11
bobguy 发表于 2014-4-6 10:11:26
if you use SAS 9.4,  call lexperk routine will make it easy.

data a;
   length new_var $3;
   array x[3] $3 ('a' 'b' 'c' );
   n=dim(x);

   k=1;
   nperm=perm(n, k);
   do j=1 to nperm;
      call lexperk(j, k, of x[*]);
      new_var=x[1];
          output;
   end;

   k=2;
   nperm=perm(n, k);
   do j=1 to nperm;
      call lexperk(j, k, of x[*]);
      new_var=catt(x[1],x[2]);
          output;
   end;

   k=3;
   nperm=perm(n, k);
   do j=1 to nperm;
      call lexperk(j, k, of x[*]);
      new_var=catt(x[1],x[2],x[3]);
          output;
   end;
   
   keep new_var;
run;

proc print;run;

12
ausser 发表于 2014-4-6 10:21:02
bobguy 发表于 2014-4-6 10:11
if you use SAS 9.4,  call lexperk routine will make it easy.

data a;
Sorry, but I'm currently using SAS 9.3 ^^

13
zhengbo8 发表于 2014-4-6 11:45:01
我重新了写了一下,改成通用版本了。例如:计算a、b、c、d。
你仔细看看递归,找到规律。
看明白了,就好写宏了。
  1. data a;
  2.         input x $;
  3. datalines;
  4. a
  5. b
  6. c
  7. d
  8. ;

  9. proc sql noprint;

  10.         create table b as

  11.           select catt(x1,x2,x3,x4) as x  from (

  12.                 select x as x1 from a

  13.                 outer union
  14.                                 
  15.                 /* 第一次递归*/
  16.                 select catt(l.x,r.x) as x2 from (
  17.                         select *  
  18.                                 from a as l, a as r
  19.                                 where r.x not like catt("%",l.x,"%")
  20.                 )

  21.                 outer union

  22.                 /* 第二次递归*/         
  23.                 select catt(l.x,r.x) as x3 from (
  24.                         select * from a as l, (
  25.                                 select catt(l.x,r.x) as x from a as l,a as r
  26.                                 where r.x not like catt("%",l.x,"%")
  27.                         ) as r
  28.                         where r.x not like catt("%",l.x,"%")
  29.                  )

  30.                 outer union

  31.                 /* 第三次递归*/         
  32.                 select catt(l.x,r.x) as x4 from (
  33.                         select * from a as l, (

  34.                                         select catt(l.x,r.x) as x from (
  35.                                                 select * from a as l, (
  36.                                                         select catt(l.x,r.x) as x from a as l,a as r
  37.                                                         where r.x not like catt("%",l.x,"%")
  38.                                                 ) as r
  39.                                                 where r.x not like catt("%",l.x,"%")
  40.                                           )

  41.                         ) as r
  42.                         where r.x not like catt("%",l.x,"%")
  43.                    )

  44.                );

  45. quit;
复制代码

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-31 21:51