【问题标题】:SAS macro nested loopSAS 宏嵌套循环
【发布时间】:2013-10-02 22:37:15
【问题描述】:

我想构建一个包含两个预测变量的逻辑模型。一个来自集合 all_indeps1,一个来自 all_indeps2。我在宏下面运行,但是,它只运行带有来自 all_indeps1 的 first 变量和来自 all_indeps2 的所有变量的模型。我应该如何修复宏,以便我可以拥有两组中两个变量的所有可能组合?

另外,我只想从逻辑模型中输出每个预测变量的 p 值,有什么想法吗?

非常感谢!

%macro trivariate(all_indeps1, all_indeps2);
%let k = 1;
%let l = 1;
%let indep1 = %scan(&all_indeps1, &k);
%let indep2 = %scan(&all_indeps2, &l);

    %do %while("&indep1" NE "");
        %do %while ("&indep2" NE "");
    title "independent variable is &Indep1 and &Indep2";
    proc logistic data = A descending;
        model Y = &indep1 &indep2;
    run;
        %let l = %eval(&l + 1);
        %let indep2 = %scan(&all_indeps2, &l);
        %end;
    %let k = %eval(&k + 1);
    %let indep1 = %scan(&all_indeps1, &k );

    %end;

%修补;

【问题讨论】:

    标签: loops macros nested sas


    【解决方案1】:

    我根本不会将其编码为宏循环,而是将其设置为您的宏只是内部位,并调用宏 n1*n2 次。

    假设您有两个数据集,indep1indep2,每个数据集都包含一个列,每行一个变量名称。那么如果你有一个宏:

    %macro trivariate(indep1,indep2);
       title "independent variable is &Indep1 and &Indep2";
        proc logistic data = A descending;
            model Y = &indep1 &indep2;
        run;
    %mend trivariate;
    
    proc sql;
     select cats('%trivariate(',indep1.var,',',indep2.var,')') into :trivarlist
      separated by ' ' 
      from indep1, indep2;
    quit;
    
    &trivarlist.;
    

    除了非常简单的情况外,在宏语言之外控制重复几乎总是比在其内部更容易;而且它是一种更好的编程风格,因为它使代码更具可移植性和可重用性。

    【讨论】:

      【解决方案2】:

      这确实是 2 个问题。

      1.你的宏没有什么问题。试试options mprint mlogic; 了解更多幕后情况。

      我个人会将其编码为

      %macro trivariate(all_indeps1, all_indeps2);
      %let n1 = %sysfunc(countw(&all_indeps1));
      %let n2 = %sysfunc(countw(&all_indeps2));
      %do i=1 to &n1;
         %let indep1 = %scan(&all_indeps1,&i);
         %do j=1 %to &n2;
            %let indep2 = %scan(&all_indeps2,&i);
      
            STUFF
         %end
      %end;
      %mend;
      

      2.从 PROC 中仅选择 1 个输出。使用ods trace on; 您的过程,然后使用ods trace off; 这将打印放置到输出目标的表名。

      然后您可以使用ods select <list of table names>; 您的程序,然后使用ods select default; 这将告诉输出交付系统 (ODS) 仅打印您要求的表格,然后重置为默认输出。 (这个表在你的情况下可能是 ParameterEstimates)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-12-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-06
        • 2023-03-22
        相关资源
        最近更新 更多