【问题标题】:doing a do loop within macro sas在宏 sas 中做一个循环
【发布时间】:2015-10-08 05:01:35
【问题描述】:

我有以下代码:

%macro initial (first=, second=, third=, fourth=, final=); 
   data &first;
     set wtnodup.&first;
         DATE1 = INPUT(PUT(Date,8.),YYMMDD8.);
         format DATE1 monyy7.;
 RUN;

proc freq data=&first order= freq;                                                                                                                              
   tables date1*jobboardid / list out=&second (drop = percent rename=   
                                              (Count=CountNew));
 run;

 data &third;
  set &second (firstobs=2);
    if countnew le 49 then delete; 
 run;
proc sort data = &third;
   by jobboardid Date1;    
run;
data &fourth (keep = countnew oldcountnew Date1 rate from till jobboardid 
                                                                    rate);
    set &third;
        by jobboardid Date1;
        format From Till monyy7.;
        from = lag12(Date1);
        oldcountnew = lag12(countnew);

        if lag12(jobboardid) EQ jobboardid and
        INTCK('month', from, Date1) EQ 12 then do;
        till = Date1;
        rate =  ((countnew/oldcountnew)-1)*100;
       output;
    end;
run;
proc sort data = &fourth;
   by Date1 rate;
proc means data=&fourth noprint;
   by Date1;
output out=Result.&final median(rate)=medianRate;
run; 

%mend initial;
%initial (first = Alabama, second = AlabamaOne, third =AlabamaTwo, 
          fourth = AlabamaThree, final=AL_10);
%initial (first = Alaska, second = AlaskaOne, third =AlaskaTwo, 
          fourth = AlaskaThree, final=AK_10);
%initial (first = Arizona, second = ArizonaOne, third =ArizonaTwo, 
          fourth = ArizonaThree, final=AZ);
%initial (first = Arkansas, second = ArkansasOne, third =ArkansasTwo, 
          fourth= ArkansasThree, final=AR_10);

我想做的是在提出条件的部分:

if countnew < 10 then delete; 

我想创建一种 do-loop,当 countnew

所以我会有一个最终数据集,用于 when countnew 的不同迭代

这样做的最佳方法是什么?

【问题讨论】:

    标签: macros sas


    【解决方案1】:

    为什么不做循环,十乘十,并像这样在数据集名称中添加迭代扩展?

    ** Sample dataset;
    data try;
    do i=1 to 1000;
    value=1+ranuni(12345)*100;
    output;
    end;
    drop i;
    run;
    
    
    ** Macro iterator:
    
    %macro iter(ds=);
    
    %do i=10 %to 70 %by 10;
    
        data &ds._&i;
        set &ds;
        if value le &i then delete;
        run;
    
    %end;
    
    %mend;
    
    %iter (ds=try)
    

    您将拥有 7 个名为 try_10--try_70 的数据集,其中 try 将替换为数据集名称。

    【讨论】:

    • 所以我会在我更大的循环中添加这个循环?我有点困惑。
    • 您是否理解了该宏循环的含义,其中数据步根据迭代器 i 的值命名和过滤?如果是这样,只需在您想要执行此操作的步骤中的循环(从 %do 到 %end)中添加该循环(没有宏规范和宏调用)。我认为 &ds 对我来说是 &second 对你来说。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多