楼主: ertyuj
1684 3

求助:需要一个宏来自动生成所有变量x组合,之后执行相应回归程序 [推广有奖]

  • 0关注
  • 1粉丝

硕士生

40%

还不是VIP/贵宾

-

威望
0
论坛币
254 个
通用积分
0.7615
学术水平
0 点
热心指数
1 点
信用等级
0 点
经验
-351 点
帖子
136
精华
0
在线时间
164 小时
注册时间
2007-2-7
最后登录
2022-3-17

20论坛币
我有一个数据文件,包括一个y和若干个x,比如下面的例子只有4个x(实际问题包含10个变量).

data test;
input y income age hight;
datalines;
1 2 3 4
2 3 5 2
3 1 8 4
4 7 1 5
8 6 2 1
9 4 0 9
;
run;

我想将y回归这些x变量,并且运行所有组合,比如:

y = income ;
y = age;
y = hight;
y = income age;
y = income hight;
y = age hight;
y = income age hight;

如果检验y = income age hight,使用PROC REG(实际问题使用的并非PROC REG),可以运行如下程序:

proc reg data=test;
model y = income age hight;
run;quit;

如果检验其他某种组合,使用如上回归程序,仅需改变相应的行即可。如果变量x有多少种组合,便需运行如上程序相应次数。

现在,我需要一个宏来自动生成所有变量x组合,之后执行相应回归程序。请各位大侠在我的这个程序的基础之上帮忙编写这个宏。非常感谢。

如果我的问题没有完全描述清楚,那么尽情按照您的理解提供相应答案。只要提供的答案与我的问题相关,鄙人皆将感激不尽。


最佳答案

mymine 查看完整内容

编了很长一段程序完成了任意多个变量进行全部组合 然后用宏调用这些不同的组合启动回归 具体见附件
关键词:自动生成 Income Input model Lines 模型 如何 income 程序
数据科学和机器学习博客:https://shorturl.at/jtHTW
沙发
mymine 发表于 2012-6-27 04:14:12 |只看作者 |坛友微信交流群
编了很长一段程序完成了任意多个变量进行全部组合
然后用宏调用这些不同的组合启动回归

具体见附件

实例.rar

826 Bytes

需要: 1 个论坛币  [购买]

本附件包括:

  • 实例.sas

使用道具

藤椅
mymine 发表于 2012-6-27 15:45:53 |只看作者 |坛友微信交流群
用了一个很麻烦的方法搞定了

使用道具

板凳
bobguy 发表于 2012-6-28 07:14:17 |只看作者 |坛友微信交流群
Here are two macros supplied by SAS institute. One does combination and one does permutation. You may modify a little bit to achieve your needs. Left it as your homework.  

%macro permute(r) / parmbuff;      /* the parmbuff option assigns */
      %let i=2;               /* the invocation parameter list to the */
      %let things=;                       /* macro variable &syspbuff */
      %do %while (%Qscan(&syspbuff,&i,%STR(,%))) ne );  /* scan the syspbuff */
         %let p&i="%Qscan(&syspbuff,&i,%STR(,%)))";  /* to determine r */
        %if &i=2 %then %let things=&&p&i;     /* and count the number */
        %else %let things=&things,&&p&i;            /* of elements, n */
        %let i=%eval(&i+1);
      %end;
      %let n=%eval(&i-2);
      data permute;
         drop i j copy;
         array check (*) $ 10 r1-r&r;          /* create a total of r */
          %do m=1 %to &r;                   /* variables  for looping */
            do r&m = &things;
          %end;
          copy=0;
            do i=2 to &r;                 /* look for duplicate items */
              do j=1 to i-1;              /* and keep the unique ones */
                if check(j)=check(i) then copy+1;
              end;
            end;
          if copy = 0 then output;        /* writes to a SAS data set */
          if copy = 0 then put r1-r&r;        /* writes to the log    */
          %do m=1 %to &r;
            end;                               /* end the r DO LOOPS */
          %end;
        run;
       proc print uniform data=permute;
            title "permutations of &n items taken &r at a time ";
            run;
     %mend permute;

%permute(1,Chip,Pete,Mike,William)
%permute(2,Chip,Pete,Mike,William)
%permute(3,Chip,Pete,Mike,William)
%permute(4,Chip,Pete,Mike,William)

%macro combo(r)/parmbuff;

      %let i=2;
      %let things=;
      %do %while (%Qscan(&syspbuff,&i,%STR(,%))) ne );
        %let p&i="%Qscan(&syspbuff,&i,%STR(,%)))";
        %if &i=2 %then %let things=&&p&i;
        %else %let things=&things,&&p&i;
        %let i=%eval(&i+1);
      %end;
      %let n=%eval(&i-2);
       data combo;
            keep v1-v&r;
            array word $8  w1-w&n (&things);
            array rr (*) r1-r&r;
            array v $8  v1-v&r;
           %do i=1 %to &r;                    /* create the DO LOOPs */
             %if &i=1 %then %do;
               do r&i=1 to &n-(&r-&i);
               %end;
             %else %do;
               do r&i=r%eval(&i-1)+1 to &n-(&r-&i);
               %end;
             %end;
               do k=1 to &r;              /* select subscripted items */
               v(k)=word (rr(k));               /* for a SAS data set */
               put v(k)      '  ' @;                       /* for log */
               end;
               put;                                  /* writes to log */
               output;                    /* writes to a SAS data set */
           %do i=1 %to &r;
             end;                     /* create ENDs for the DO LOOPs */
             %end;
            put;
            run;
         proc print uniform data=combo;
            title "combinations of &n items taken &r at a time ";
            run;
       %mend combo;

%combo(1,Chip,Pete,Mike,William)
%combo(2,Chip,Pete,Mike,William)
%combo(3,Chip,Pete,Mike,William)
%combo(4,Chip,Pete,Mike,William)

使用道具

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

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

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

GMT+8, 2024-5-14 11:30