楼主: akalius
6388 20

请教大家一个遍历变量选数据集的问题 [推广有奖]

11
freerunning_sky 在职认证  发表于 2014-8-4 08:40:28
akalius 发表于 2014-8-2 20:23
还有怎么把他们的变量名也保持原来的
到底要的是什么。。
不是说变量名要a1-a6么?怎么又要原来的,保持原来的变量名那就简单多了

12
bobguy 发表于 2014-8-4 08:49:48
Here i give a simpler solution which uses comb and allcomb routine to generate all data sets with keep option.

278  data _null_;
279    file 'c:\temp\8s6.sas';
280    array x(8) $1 ('a' 'b' 'c' 'd' 'e' 'f' 'g' 'h');
281     k=6;
282     n=dim(x);
283     nsets=comb(n, k);
284     do j=1 to nsets;
285        call allcomb(j, k, of x[*]);
286        put 'temp'  j '(keep=' x1-x6 ')';
287     end;
288  run;

NOTE: The file 'c:\temp\8s6.sas' is:
      Filename=c:\temp\8s6.sas,
      RECFM=V,LRECL=32767,File Size (bytes)=0,
      Last Modified=03Aug2014:20:47:08,
      Create Time=03Aug2014:20:45:47

NOTE: 28 records were written to the file 'c:\temp\8s6.sas'.
      The minimum record length was 25.
      The maximum record length was 26.
NOTE: DATA statement used (Total process time):
      real time           0.02 seconds
      cpu time            0.03 seconds


289
290  options source2;
291
292  data %include 'c:\temp\8s6.sas';
NOTE: %INCLUDE (level 1) file c:\temp\8s6.sas is file c:\temp\8s6.sas.
293 +temp1 (keep=a b c d e f )
294 +temp2 (keep=a b c d e h )
295 +temp3 (keep=a b c d e g )
296 +temp4 (keep=a b c d f g )
297 +temp5 (keep=a b c d f h )
298 +temp6 (keep=a b c d g h )
299 +temp7 (keep=a b c e g h )
300 +temp8 (keep=a b c e g f )
301 +temp9 (keep=a b c e h f )
302 +temp10 (keep=a b c g h f )
303 +temp11 (keep=a b d g h f )
304 +temp12 (keep=a b d g h e )
305 +temp13 (keep=a b d g f e )
306 +temp14 (keep=a b d h f e )
307 +temp15 (keep=a b g h f e )
308 +temp16 (keep=a c g h f e )
309 +temp17 (keep=a c g h f d )
310 +temp18 (keep=a c g h e d )
311 +temp19 (keep=a c g f e d )
312 +temp20 (keep=a c h f e d )
313 +temp21 (keep=a g h f e d )
314 +temp22 (keep=b g h f e d )
315 +temp23 (keep=b g h f e c )
316 +temp24 (keep=b g h f d c )
317 +temp25 (keep=b g h e d c )
318 +temp26 (keep=b g f e d c )
319 +temp27 (keep=b h f e d c )
320 +temp28 (keep=g h f e d c )
NOTE: %INCLUDE (level 1) ending.
321     ;
322     set a;
323  run;

NOTE: There were 3 observations read from the data set WORK.A.
NOTE: The data set WORK.TEMP1 has 3 observations and 6 variables.
NOTE: The data set WORK.TEMP2 has 3 observations and 6 variables.
NOTE: The data set WORK.TEMP3 has 3 observations and 6 variables.
NOTE: The data set WORK.TEMP4 has 3 observations and 6 variables.
NOTE: The data set WORK.TEMP5 has 3 observations and 6 variables.
NOTE: The data set WORK.TEMP6 has 3 observations and 6 variables.
NOTE: The data set WORK.TEMP7 has 3 observations and 6 variables.
NOTE: The data set WORK.TEMP8 has 3 observations and 6 variables.
NOTE: The data set WORK.TEMP9 has 3 observations and 6 variables.
NOTE: The data set WORK.TEMP10 has 3 observations and 6 variables.
NOTE: The data set WORK.TEMP11 has 3 observations and 6 variables.
NOTE: The data set WORK.TEMP12 has 3 observations and 6 variables.
NOTE: The data set WORK.TEMP13 has 3 observations and 6 variables.
NOTE: The data set WORK.TEMP14 has 3 observations and 6 variables.
NOTE: The data set WORK.TEMP15 has 3 observations and 6 variables.
NOTE: The data set WORK.TEMP16 has 3 observations and 6 variables.
NOTE: The data set WORK.TEMP17 has 3 observations and 6 variables.
NOTE: The data set WORK.TEMP18 has 3 observations and 6 variables.
NOTE: The data set WORK.TEMP19 has 3 observations and 6 variables.
NOTE: The data set WORK.TEMP20 has 3 observations and 6 variables.
NOTE: The data set WORK.TEMP21 has 3 observations and 6 variables.
NOTE: The data set WORK.TEMP22 has 3 observations and 6 variables.
NOTE: The data set WORK.TEMP23 has 3 observations and 6 variables.
NOTE: The data set WORK.TEMP24 has 3 observations and 6 variables.
NOTE: The data set WORK.TEMP25 has 3 observations and 6 variables.
NOTE: The data set WORK.TEMP26 has 3 observations and 6 variables.
NOTE: The data set WORK.TEMP27 has 3 observations and 6 variables.
NOTE: The data set WORK.TEMP28 has 3 observations and 6 variables.
NOTE: DATA statement used (Total process time):
      real time           0.50 seconds
      cpu time            0.21 seconds

已有 1 人评分论坛币 学术水平 热心指数 信用等级 收起 理由
akalius + 5 + 1 + 1 + 1 精彩帖子

总评分: 论坛币 + 5  学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

13
akalius 学生认证  发表于 2014-8-4 09:34:42
freerunning_sky 发表于 2014-8-4 08:40
到底要的是什么。。
不是说变量名要a1-a6么?怎么又要原来的,保持原来的变量名那就简单多了
恩,从8个变量中抽取任意的6个,并保持原来的变量名

14
akalius 学生认证  发表于 2014-8-4 09:44:42
bobguy 发表于 2014-8-4 08:49
Here i give a simpler solution which uses comb and allcomb routine to generate all data sets with ke ...
不好意思   ,  如果test是一下的样子  ,该怎样写
data test;
input a b c d e f g h;
cards;
1 2 3 5 9 7 4 2
5 8 6 9 7 2 0 3
run;

15
bobguy 发表于 2014-8-4 09:51:02
akalius 发表于 2014-8-4 09:44
不好意思   ,  如果test是一下的样子  ,该怎样写
data test;
input a b c d e f g h;
Here is the program.

Input data set a with variables a b c d e f g h.


data a;
        input a b c d e f g h;
        cards;
        10 20 30 40 50 60 70 80
        11 21 31 41 51 61 71 81
        12 22 32 42 52 62 72 82
        ;
run;

proc print;run;

data _null_;
  file 'c:\temp\8s6.sas';
  array x(8) $1 ('a' 'b' 'c' 'd' 'e' 'f' 'g' 'h');
   k=6;
   n=dim(x);
   nsets=comb(n, k);
   do j=1 to nsets;
      call allcomb(j, k, of x
  • );
          put 'temp'  j '(keep=' x1-x6 ')';
       end;
    run;

    options source2;

    data %include 'c:\temp\8s6.sas';
       ;
       set a;
    run;
  • 已有 1 人评分论坛币 学术水平 热心指数 信用等级 收起 理由
    akalius + 5 + 1 + 1 + 1 精彩帖子

    总评分: 论坛币 + 5  学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

    16
    akalius 学生认证  发表于 2014-8-4 11:10:58
    bobguy 发表于 2014-8-4 09:51
    Here is the program.

    Input data set a with variables a b c d e f g h.
    8s6.sas 是什么   我运行后出来这个界面

    QQ图片20140804110941.jpg (64.24 KB)

    QQ图片20140804110941.jpg

    17
    bobguy 发表于 2014-8-5 07:24:19
    akalius 发表于 2014-8-4 11:10
    8s6.sas 是什么   我运行后出来这个界面
    you need make a directory under c: namely c:\temp
    已有 1 人评分论坛币 学术水平 热心指数 信用等级 收起 理由
    akalius + 5 + 1 + 1 + 1 great thanks to you

    总评分: 论坛币 + 5  学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

    18
    akalius 学生认证  发表于 2014-8-8 15:35:43
    bobguy 发表于 2014-8-4 09:51
    Here is the program.

    Input data set a with variables a b c d e f g h.
    您好,不好意思,在这个问题的基础上再请教一下,如果此时增加了i j k,三个变量 ,在每个数据集中仍然只是选择a b c d e f g h 中的6个变量,并保持i j k 三个变量,请问程序该如何改写。非常感谢。数据变成这个样

    data a;
            input a b c d e f g h i j k;
            cards;
            10 20 30 40 50 60 70 80 1 2 3
            11 21 31 41 51 61 71 81 1 2 3
            12 22 32 42 52 62 72 82 3 4 6
            ;
    run;

    19
    bobguy 发表于 2014-8-11 10:54:13
    akalius 发表于 2014-8-8 15:35
    您好,不好意思,在这个问题的基础上再请教一下,如果此时增加了i j k,三个变量 ,在每个数据集中仍然只是 ...
    Here it is.  

      data _null_;
        file 'c:\temp\11s6.sas';
        array x(11) $1 ('a' 'b' 'c' 'd' 'e' 'f' 'g' 'h', 'i','j','k');
         k=6;
         n=dim(x);
         nsets=comb(n, k);
         do j=1 to nsets;
            call allcomb(j, k, of x(*));
            put 'temp'  j '(keep=' x1-x6 ')';
         end;
      run;

      data %include 'c:\temp\11s6.sas';
      ;
        set a;
      run;
    已有 1 人评分论坛币 收起 理由
    akalius + 5 精彩帖子

    总评分: 论坛币 + 5   查看全部评分

    20
    akalius 学生认证  发表于 2014-8-11 22:59:43
    bobguy 发表于 2014-8-11 10:54
    Here it is.  

      data _null_;
    不好意思,可能我没有说明白,我的意思是,每个数据集中都有i,j,k这三个变量,其他六个变量还是在a,b,c,d,e,f,g,h中选择,也就是说每个数据集有9个变量,但是i,j,k始终占有3个。最后的生成数据集个数还是与先前一样的,而不是像11选6那样几百个。
    谢谢

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

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