楼主: jgx06gy2006
7691 7

如何用sas实现排列组合 [推广有奖]

  • 0关注
  • 0粉丝

本科生

21%

还不是VIP/贵宾

-

威望
0
论坛币
399 个
通用积分
0
学术水平
1 点
热心指数
2 点
信用等级
1 点
经验
1950 点
帖子
67
精华
0
在线时间
79 小时
注册时间
2011-9-3
最后登录
2018-10-15

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
从0-9这10个数字中选取任意的4个数字组成一个四位数,问这些四位数是哪些?哪位大侠帮忙写下程序,谢谢
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:如何用 如何

沙发
teqel 发表于 2014-12-19 16:18:46 |只看作者 |坛友微信交流群
Example
The following is an example of the ALLCOMB function.
data _null_;
   array x[5] $3 ('ant' 'bee' 'cat' 'dog' 'ewe');
   n=dim(x);
   k=3;
   ncomb=comb(n,k);
   do j=1 to ncomb+1;
      rc=allcomb(j, k, of x[*]);
      put j 5. +3 x1-x3 +3 rc=;
   end;
run;
SAS writes the following output to the log:
    1   ant bee cat    rc=0
    2   ant bee ewe    rc=3
    3   ant bee dog    rc=3
    4   ant cat dog    rc=2
    5   ant cat ewe    rc=3
    6   ant dog ewe    rc=2
    7   bee dog ewe    rc=1
    8   bee dog cat    rc=3
    9   bee ewe cat    rc=2
   10   dog ewe cat    rc=1
   11   dog ewe cat    rc=-1

使用道具

藤椅
teqel 发表于 2014-12-19 16:34:52 |只看作者 |坛友微信交流群

这个有遗漏,只是组合

  1. data test(keep=aa);
  2.         array x[10] $1 ('0' '1' '2' '3' '4' '5' '6' '7' '8' '9');
  3.         n=dim(x);
  4.         k=4;
  5.         ncomb=comb(n,k);

  6.         do j=1 to ncomb;
  7.                 rc=allcomb(j, k, of x[*]);

  8.                 if x1>0 then
  9.                         do;
  10.                                 aa=input(cats(x1,x2,x3,x4),4.);
  11.                                 output;
  12.                         end;
  13.         end;
  14. run;
复制代码

使用道具

板凳
teqel 发表于 2014-12-19 17:08:52 |只看作者 |坛友微信交流群
上面写得不对,改一下
  1. %let s=4;

  2. data test(keep=aa);
  3.         array x[10] $1 ('0' '1' '2' '3' '4' '5' '6' '7' '8' '9');
  4.         array y[&s] $1;
  5.         n=dim(x);
  6.         k=&s;
  7.         ncomb=comb(n,k);

  8.         do i=1 to ncomb;
  9.                 rc=allcomb(i, k, of x[*]);

  10.                 do j=1 to k;
  11.                         y[j]=x[j];
  12.                 end;

  13.                 nfact=fact(k);

  14.                 do j=1 to nfact;
  15.                         call allperm(j, of y[*]);

  16.                         if (y1>0) then
  17.                                 do;
  18.                                         aa=input(cats(of y1-y&s),10.);
  19.                                         output;
  20.                                 end;
  21.                 end;
  22.         end;
  23. run;

  24. proc sort data=test;
  25.         by aa;
  26. run;
复制代码

使用道具

报纸
mingfeng07 学生认证  发表于 2014-12-20 19:17:48 |只看作者 |坛友微信交流群
  1. data a(keep=value);
  2. do i=1 to 9;
  3.   do j=0 to 9;
  4.    do k=0 to 9;
  5.     do l=0 to 9;
  6.          if i^=j and i^=k and i^=l and j^=k and j^=l and k^=l then do;
  7.             value=i*1000+j*100+k*10+l;
  8.             output;
  9.          end;
  10.     end;
  11.    end;
  12.   end;
  13. end;
  14. run;
复制代码

使用道具

地板
yongyitian 发表于 2014-12-21 10:55:45 |只看作者 |坛友微信交流群
/* note: 10选4的组合应该有210种, ncomb(10,4) = 210 */

下面是不用 comb(), allcomb() functions 而生成 n 选4的全部组合的程序,其中n可以是大于4的任意数。

  1. data comb_n_4;
  2.   n=10; r=4;  
  3.   do k1 = 0 to (n-1) - (r-1);              
  4.    do k2 = k1+1 to (n-1) - (r-2);         
  5.      do k3 = k2+1 to (n-1) - (r-3);      
  6.        do k4 = k3+1 to (n-1) - (r-4);         
  7.        index=catx(',', k1, k2, k3, k4);
  8.    put index=;
  9.    output;  
  10.   end; end; end; end;
  11. run;
复制代码

使用道具

7
sushe1527 发表于 2014-12-22 15:58:41 |只看作者 |坛友微信交流群
mingfeng07 发表于 2014-12-20 19:17
要是可以重复取值
data a(keep=value);
do i=1000 to 9999;
            value=i;
            output;
         end;
run;

使用道具

8
wodematlab 发表于 2015-4-18 22:19:27 |只看作者 |坛友微信交流群
%macro test1();
data test2(keep=aa);
    array x[5] $1 ('age','wealth','sex','x1','x2');
    n=dim(x);
        %do k=1 %to 3;
    ncomb=comb(n,&k.);
       
    do j=1 to ncomb;
            
          rc=allcomb(j, &k., of x[*]);
          if x1>0 then
           do;
                aa=putn(cats(of x1-x&k.),10.);
                output;
            end;
     end;
         %end;
run;

%mend;
%test1();
连接字符有问题~~~~~~~~~~~~~~·

使用道具

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

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-5-3 08:58