全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
3634 6
2013-05-05
在A数据集中,我有x1-x3三个不同的变量,我想要做的是从A数据集中抽出不同变量组合,然后生成新的数据集。因为x1-x3一共有2的3次方种组合,除去没有任何一个变量进入新数据集的情况,所以我最后生成的一共有8-1=7个新的数据集,每个数据集分别包含了A数据集中变量的各种组合。
比如说:
从A中抽取x1这个变量,放入B1数据集;
从A中抽取x2这个变量,放入B2数据集;
从A中抽取x3这个变量,放入B3数据集;
从A中抽取x1,x2这2个变量变量,放入B4数据集;
从A中抽取x1,x3这2个变量变量,放入B5数据集;
从A中抽取x2,x3这2个变量变量,放入B6数据集;
从A中抽取x1,x2,x3这3个变量变量,放入B7数据集;
这里举的是3个变量的情况,所以如果用比较傻的方法应该不用花很长时间,但是如果当A数据集中有10个变量的话,就有1024-1=1023种情况,这个时候就得用一个比较简单的方法。
因为本人SAS编程水平尚浅,还希望各位高手多多帮忙,不吝赐教~~因为最近在写论文,所以比较急,拜托各位啦~~
二维码

扫码加我 拉你入群

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

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

全部回复
2013-5-5 23:18:41
you don't need to generate 2^n-1 datasets, just let PROC MEANS takes care of it.
复制代码
in the output dataset, the combination of x_i1, xi_2, ..., and x_ik will be identified by _TYPE_=2^{i1-1}+2^{i2-1}+...+2^{x_ik-1} in the output dataset xxx, where 1<=i1<i2<...<ik<=8.
二维码

扫码加我 拉你入群

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

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

2013-5-6 10:00:31
%let libname=sashelp;
%let dsname=class;

data test;
    set sashelp.vcolumn end=last;
        where upcase(libname)=upcase("&libname") and upcase(memname)=upcase("&dsname");
        call symputx(cats("var",_n_),name);
        if last then call symputx("num",_n_);
run;

data test1;
    length code $1000;
    do i=1 to &num;
           code=catx(" ", code, "do var"||strip(i)||"= '           ', "||'"&&var'||strip(i)||'";');
    end;
        code="data test2; "||strip(code)|| "output;"||repeat("end;",&num-1)||"run;";
        call execute(code);
    put code=;
run;

data test3;
    set test2 end=last;;
        keepvar=catx(" ",of var1-var&num);
        length code $100;
        retain n 0;
        if ^missing(keepvar) then do;
            n+1;
           code="&dsname"||strip(n)||"(keep="||strip(keepvar)||")";
        end;
        if _n_=1 then call execute("data ");
        call execute(code);
        if last then call execute("; set &libname..&dsname; run;");
run;
二维码

扫码加我 拉你入群

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

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

2013-5-6 10:10:40
/* try this */
data test;                     /*  note: if you have large number of observations */
input x1-x4 var1;        /*  just keep one observation in this dataset */
datalines;
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
;
run;

proc summary data=test;
   class x1-x4;
   var var1;
   output out=test1(where=(_type_>0) drop= _freq_)
                       max(var1) = max_value;
run;

data _null_;
   length outvar $30. outname $8.;
   array x x1-x4;
    set test1;
    outvar=' ';         
    outname='out';
    do i = 1 to dim(x);
      if x(i)^=. then do;  outvar = catx(' ', outvar, vname(x(i)));
                                outname = catx('', outname, i);   
                     end;    outname = compress(outname);
   end;  
   call execute('data '||outname||'; set test (keep='||outvar||'); run;');
run;
二维码

扫码加我 拉你入群

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

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

2013-5-7 10:58:30
邓贵大 发表于 2013-5-5 23:18
you don't need to generate 2^n-1 datasets, just let PROC MEANS takes care of it.in the output datase ...
Thanks a lot for your help~~~
二维码

扫码加我 拉你入群

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

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

2013-5-7 10:59:36
yongyitian 发表于 2013-5-6 10:10
/* try this */
data test;                     /*  note: if you have large number of observations */ ...
Thank you very much. I will try it!! Thanks again!
二维码

扫码加我 拉你入群

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

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

点击查看更多内容…
相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群