【问题标题】:SAS All Combinations with macro variableSAS 与宏变量的所有组合
【发布时间】:2016-10-26 11:22:21
【问题描述】:

我从其他地方窃取了一些代码来创建所有变量组合。我需要这个来创建多个回归,然后确定最好的。我喜欢输出,因为我可以使用一行并将所有变量的名称放在一个位置。

当我手动输入数据时,该数组有效,但这需要跨不同的数据工作并自行选择变量,因此我需要使用宏变量来输入数据。这应该不是问题,这适用于其他数据步骤。有人可以告诉我哪里出错了。

data test(keep=my_string);
length my_string $200.;
  array a[4] $ ('new1' 'new2' 'new3' 'new4');

  n = dim(a);

   do k=1 to n;
         do j=1 to comb(n,k);
             call allcomb(j,k,of a[*]);
                 do i = 1 to k;
                    if i=1 then do; my_string="";counter=0;end;
                    counter=counter+1;
                    my_string=catx(" ",my_string, a[i]);
                 if counter=k then output;

                 end;
           end;
    end;
run;

下一个元素不起作用。只是给了我缺失的值 - 但它知道它需要 127 ...... subs 只是一个包含 new1-new7 的宏变量。

rsubmit;
data xx(keep=my_string);
length my_string $200.;
  array a &subs;

  n = dim(a);

   do k=1 to n;
         do j=1 to comb(n,k);
             call allcomb(j,k,of a[*]);
                 do i = 1 to k;
                    if i=1 then do; my_string="";counter=0;end;
                    counter=counter+1;
                    my_string=catx(" ",my_string, a[i]);
                 if counter=k then output;

                 end;
           end;
    end;
run;
endrsubmit;

非常感谢您的帮助。
J

【问题讨论】:

    标签: arrays macros sas


    【解决方案1】:

    如果您已将 subs 定义为

    %let subs=new1-new7;
    

    那么 SAS 认为这些是变量,而不是字符串值。如果删除 keep= 语句,您将看到 SAS 创建了变量 new1-new7

    您需要保持第一个示例中的格式。试试这个:

    %let subs='new1' 'new2' 'new3' 'new4' 'new5' 'new6' 'new7';
    %let n=7;
    data xx(keep=my_string);
    length my_string $200.;
      array a[&n] $ (&subs);
    
      n = dim(a);
    
       do k=1 to n;
             do j=1 to comb(n,k);
                 call allcomb(j,k,of a[*]);
                     do i = 1 to k;
                        if i=1 then do; my_string="";counter=0;end;
                        counter=counter+1;
                        my_string=catx(" ",my_string, a[i]);
                     if counter=k then output;
    
                     end;
               end;
        end;
    run;
    

    如果你想使用你拥有的表格,那么你必须读取数组中变量的名称并使用它。在这里,我正在创建一个新的字符串数组来保存名称。您可以更改n 的值,然后查看这将适用于所有值(直到您在my_string 中用完我增大了大小的空间):

    %let n=7;
    %let subs=new1-new&n;
    
    data xx(keep=my_string);
    length my_string $1000.;
      array v &subs;
      array a[&n] $32. _temporary_;
    
      n = dim(v);
    
      do i=1 to n;
         a[i] = vname(v[i]); 
      end;
    
       do k=1 to n;
             do j=1 to comb(n,k);
                 call allcomb(j,k,of a[*]);
                     do i = 1 to k;
                        if i=1 then do; my_string="";counter=0;end;
                        counter=counter+1;
                        my_string=catx(" ",my_string, a[i]);
                     if counter=k then output;
    
                     end;
               end;
        end;
    run;
    

    【讨论】:

      【解决方案2】:

      很抱歉浪费了您的时间。答案是我的数组没有语音标记,所以我必须用数组中的语音标记重新创建它。然后它就像一个魅力。

      其中 anz 是宏中的数字。

      rsubmit;
      data hasitreallyworked;
      length my_string $200.;
        array a[&anz] $ (&subs2); 
      
        n = dim(a);
      
         do k=1 to n;
               do j=1 to comb(n,k);
                   call allcomb(j,k,of a[*]);
                       do i = 1 to k;
                          if i=1 then do; my_string="";counter=0;end;
                          counter=counter+1;
                          my_string=catx(" ",my_string, a[i]);
                       if counter=k then output;
      
                       end;
                 end;
          end;
      run;
      endrsubmit;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-16
        • 1970-01-01
        • 2014-12-02
        • 2021-12-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多