【问题标题】:How to make multiple data sets using do loops in SAS/IML?如何使用 SAS/IML 中的 do 循环制作多个数据集?
【发布时间】:2015-04-01 01:42:06
【问题描述】:

我正在尝试以下代码:

proc IML;
do i=1 to 20;  
[some codes to execute]  
data[i];  
end;  
QUIT;

所以我希望在完成 do 循环后获得 20 个数据集。 SAS 有可能吗?我可以使用 macro,但我不喜欢在 PROC IML 中使用宏!

提前致谢。

【问题讨论】:

    标签: sas sas-iml


    【解决方案1】:

    如果您拥有 SAS/IML 12.1,它于 2012 年 8 月作为 SAS 9.3m2 的一部分发布,那么您只需将每个数据集的名称括在括号中,如下所示

    proc iml;
    names = "Data1":"Data20";
    do i = 1 to ncol(names);
       x = i;
       dsname = names[i];   /* construct each name */
       create (dsname) from x;
       append from x;
       close (dsname);
    end;
    

    完整的程序和解释见文章"Read data sets that are specified by an array of names."最后一个例子

    【讨论】:

    • 非常感谢@Rick。我使用了dsname ={},然后我得到了 20 个名为 data1、data2 的数据集,依此类推。当我重新运行代码时,它再次生成从 21 开始的 20 个数据集,即 data21, data22 等等。有什么办法可以设置我的首选名称?而且,在重新运行代码的情况下,有什么方法可以覆盖数据集?
    • DSName 需要是一个标量。我已经编辑了程序以使其更清晰。
    • 真棒@Rick。我很感激。
    【解决方案2】:

    是的,在模块内使用CALL EXECUTE 子例程。

    proc iml;
    file LOG;
    
    output = (1:10)`;
    
    /*This is how you create a data set from a matrix*/
    create outdata from output;
    append from output;
    close outdata;
    
    /*This module will create 1 data set for each variable in OUTPUT*/
    start loopit;
    do i=1 to 10;
        x = output[i];
        /*build the string you want to execute*/
        outStr = 'create outdata' + catt(i) + " from x; append from x; close outdata" + catt(i) + ";";
        put outStr; /*Print the string to the log*/
    
        /*Execute the string*/
        call execute(outStr);
    end;
    finish loopit;
    
    /*Call the module*/
    call loopit;
    
    quit;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-09
      • 1970-01-01
      • 1970-01-01
      • 2019-01-15
      • 2015-03-06
      • 1970-01-01
      相关资源
      最近更新 更多