SAS Macro Do LOOP

家住魔仙堡 提交于 2021-02-10 15:19:46

问题


I want to have a sas macro loop by even years, so the do loop would jump from 2006 to 2008 to 2010...all the way to 2018 and not from 2006 to 2007.

When I do a %by = 2, SAS doesn't work and gives me an error message. What is the best solution?

I have the following Code:

%macro import;
    %do I = 2006 %to 2018;
        data BTI&I;
            set edited.Bti_&I;
            year=&I;
        run; 
    %end;
%mend import;
%import;

回答1:


Add the %by 2 keyword to increment intervals of 2. I would also recommend passing the start and end years as parameters to your function and give defaults values of 2006 and 2018.

%macro import(start=2006, end=2018);
    %do I = &start. %to &end. %by 2;
        data BTI&I;
            set edited.Bti_&I;
            year=&I;
        run; 
    %end;
%mend import;
%import;

Usage:

  • %import(); which will use the default values 2006 & 2018
  • %import(start=2009, end=2018); specify the date range you want to use


来源:https://stackoverflow.com/questions/49660035/sas-macro-do-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!