在SaS中重复执行宏

问题描述 投票:0回答:3

我正在使用一个实际上将有数千个细分的数据集。为了说明,我附上8。

我想要做的是为每个段创建一个数据集,以便我可以计算每个数据集的贡献。我已经完成了大部分工作,但我要问的是获取此输出的每一行并将其转换为自己的数据集。

数据集看起来像这样

ID    Segment    Asset    Mapping    Performing    Fixed 

1     Loan       Asset    Loan1      Performing    Fixed
2     Loan       Asset    Loan1      No            Fixed
3     Loan       Asset    Loan1      P             Floating
4     Loan       Asset    Loan1      N             floating
5     Loan       Asset    Loan2      P             Fixed
...
8     Loan       Asset    Loan2      N             Floating

如上所述,数据已经很好地排序。

我写的宏如下:

%macro BinData(i);

Data Bin&i;
set Import;
If _N_ = &i ;
run;

%mend;

有没有这样的方法重复循环这个宏(在较大的图片中)所有8(1000)段,如:

proc
do x=1 to 8;
%bindata(x);
run;

重点是为每一行创建一个新的细分市场。 TIA。

loops macros sas proc-sql do-loops
3个回答
0
投票

试试这个:

/* Your import data set */
data import;
    id=1;
    segment=1;
    output;
    id=2;
    segment=1;
    output;
    id=3;
    segment=1;
    output;
run;

/* Your macro to separate the rows into different data sets */
%macro temp;
    /* Create a list with all possible id's */
    proc sql noprint;
        SELECT DISTINCT id
        INTO :list separated by '#'
        FROM import
        ;
    quit;
    /* Create for each id a separate data set and output the rows into it */
    data
        %do i=1 %to %sysfunc(countw(&list.,#));
            bin%scan(&list.,&i.,#) 
        %end;
        ;
        set import;
        %do i=1 %to %sysfunc(countw(&list.,#));
            if _n_=&i. then do;
                output bin%scan(&list.,_n_,#);
            end;
        %end;
    run;
%mend;
%temp;

0
投票

这是一个根据分组变量分割的非宏方法,在本例中,它是按年龄划分的,但您可以将其更改为ID。没有循环,一般来说,WHERE比IF快。

proc sort data=sashelp.class out=class;
by age;
run;

data _null_;
set class;
by age;
retain count;

if first.age then do;
    count+1;

    *create string;
    str1='data sub'||put(count,z3.)||"; set sashelp.class; where age="||put(age, 2.)||";run;";
    call execute(str1);

end;
run; 

0
投票

这是一些代码:

%macro BinData;

%do i = 1 %to 8;

Data Bin&i;
set Import;
If _N_ = &i;
run;

%end;

%mend;

%bindata();
© www.soinside.com 2019 - 2024. All rights reserved.