按照某个变量的值拆分数据集(为循环和批量分析服务)



  • /*按照变量date的值来拆分数据集
    如date=19960131生成一个数据集,date=19960228生成一个
    现在有很多个,到20080731了
    */
    /*方法一
    思路是先把distinct date的值变为宏变量,然后用data步输出。
    反复读数据,会很慢
    */
    data a;
    do date=1991 to 2011;
    output;
    end;
    run;
    data null;
    set a end=last;
    call symputx('date'||left(n),date);
    if last then call symput ('n',n);
    run;
    %macro split;
    %do i=1 %to &n;
    data b&i;
    set a;
    where date=&&date&i;
    run;
    %end;
    %mend;
    %split;

    /方法二/
    data date;
    date=input("19960131",yymmdd8.);
    output;
    date=input("19960228",yymmdd8.);
    output;
    date=input("19960309",yymmdd8.);
    output;
    run;

    data null;
    set date;
    call execute ("data data_"||strip(put(date,yymmddn8.))||";set a;if date="||date||";run;");
    run;

    /方法三/

    data a;
    format target_day $8.;
    input target_day $;
    cards;
    20120730
    20120731
    20120810
    20120817
    ;run;

    %macro segmentation();
    proc sort data = a out = work.t1(keep = target_day) nodupkey;
    by target_day;
    run;
    data null;
    set work.t1 nobs=n;
    call symputx(compress("set_"||n), put(input(target_day, yymmdd8.), yymmddn8.));
    if n = 1 then call symputx('n',n);
    run;
    data %do ii = 1 %to &n.;
    data_&&set_&ii..
    %end;;
    set a;
    select (target_day);
    %do ii = 1 %to &n.;
    when ("&&set_&ii..") output data_&&set_&ii..;
    %end;
    otherwise;
    end;
    run;
    proc sql;
    drop table work.t1;
    quit;
    %mend;
    %segmentation();


登录后回复