SAS中文论坛

标题: 如何求n个观测的组合? [打印本页]

作者: shiyiming    时间: 2012-6-21 19:25
标题: 如何求n个观测的组合?
数据集ads中有一个变量x,有n个观测。
如果要求出其中的两两组合,且考虑顺序,
可用如下程序,或用iml做两个循环,
[code:lypjddp8]

%let n=5;

data ads;
do x=1 to &n;
   output;
end;
run;

proc sql noprint;
  create table bds as
  select ads.x, ads_copy.x as y
  from ads, ads ads_copy
  where ads.x<ads_copy.x;
quit;

[/code:lypjddp8]

问题是如果n很大,例如n=100,我们要求出其中80个的组合
且考虑顺序,该如何求(sql最大能连接32张表,矩阵循环要做80次?)?
恳请各位sasor,指点一下。
作者: shiyiming    时间: 2012-6-22 14:58
标题: Re: 如何求n个观测的组合?
[code:3j6d41pr]data bds;
    set ads;
    do p=_n_+1 to n;
        set ads(rename=(x=y)) nobs=n point=p;
        output;
    end;
run;[/code:3j6d41pr]
作者: shiyiming    时间: 2012-7-6 15:12
标题: Re: 如何求n个观测的组合?
Merlin的意思应该是,C(100 80),100个值取80个值的组合方案吧?~
hopewell大大给出的是等价于sql笛卡尔乘积组合的data step方法,是 C(n 2) 的,如果是C(100 80),也需要内嵌80个循环吗?
作者: shiyiming    时间: 2012-7-10 03:34
标题: Re: 如何求n个观测的组合?
C(100, 80)==5.3598337E20
Are you sure you want all of them

nevertheless, you can use ALLCOMB() function in SAS to enumerate all combinations, up to COMB(100, 80);
here is a sample code from SAS Doc
[code:2z8wl18r]
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;
[/code:2z8wl18r]

you simply change x[5] to x[100] ;

[quote="MerlinZHOU":2z8wl18r]数据集ads中有一个变量x,有n个观测。
如果要求出其中的两两组合,且考虑顺序,
可用如下程序,或用iml做两个循环,
[code:2z8wl18r]

%let n=5;

data ads;
do x=1 to &n;
   output;
end;
run;

proc sql noprint;
  create table bds as
  select ads.x, ads_copy.x as y
  from ads, ads ads_copy
  where ads.x<ads_copy.x;
quit;

[/code:2z8wl18r]

问题是如果n很大,例如n=100,我们要求出其中80个的组合
且考虑顺序,该如何求(sql最大能连接32张表,矩阵循环要做80次?)?
恳请各位sasor,指点一下。[/quote:2z8wl18r]
作者: shiyiming    时间: 2012-7-17 21:50
标题: Re: 如何求n个观测的组合?
非常感谢oloolo和hopewell对这个问题的分析和解答。
我本意是以不同的组合进行抽样。无限大只是一种理论上的可能性。
再次感谢!




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