SAS循环遍历宏变量列表 [英] SAS Loop through list of macro variable

查看:131
本文介绍了SAS循环遍历宏变量列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我是新手,如果这不是一个好问题,我深表歉意.我搜索但没有找到类似的东西.我不确定我的方法是否正确,因此我们将不胜感激.

First off, I'm new so I apologize if this is not a good question. I searched but did not find something similar. I'm not sure my approach is correct so any assistance would be appreciated.

我正在为具有学期的学校处理数据集,例如 2017SP 是 2017 年春季,2017SU 是 2017 年夏季等等.

I'm working on a data set for a school that has semesters for example 2017SP is Spring of 2017, 2017SU is Summer of 2017 and so on.

我有以下程序,我在顶部设置了一个宏变量,然后使用它从各种库和数据集中提取术语.请注意,我有几个数据步骤,只需要运行整个程序超过 5 次.

I have the following program where I set up a Macro Variable at the top and then use it to pull out the term from various libraries and data sets. Note that I have several data steps and just need to run the entire program over 5 times.

%let STC_TERM=2017SU;

数据步;集合(包含所有术语数据的库);如果 STC_TERM = "&STC_TERM";*我想做的其他事情*跑;我的程序中还有其他几个类似的数据步骤,它们最终给了我想要的输出数据.

Data Step; set (library that has data on all terms); if STC_TERM = "&STC_TERM"; *other things I want to do* run; I have several other similar data steps in my program that finally give me the output data I want.

现在我需要创建一个数据集,其中包含五个学期的数据,它们只是彼此附加.

Now I need to create a data set with five semesters worth of data just appended to each other.

而不是运行我的代码 5 次并只更改%let STC_TERM=2017SU;"到"%let STC_TERM=2016SU;"对于我想要的每一年,我希望有办法提供我的 5 个术语列表,并让 SAS 循环遍历 5 个术语中的每一个,并将结果附加在一起.

Instead of running my code 5 times and just changing "%let STC_TERM=2017SU;" to "%let STC_TERM=2016SU;" for each year I want I was hoping there was someway of providing my list of 5 terms and have SAS loop through each of the 5 terms and append the results together.

五个术语是(2017SU、2016SU、2015SU、2014SU、2013SU).

The five terms are (2017SU, 2016SU, 2015SU, 2014SU, 2013SU).

有没有办法将此列表提供给我的程序并让它首先在 2017SU 上执行所有数据步骤,然后在下一个学期等等,然后将生成的 5 个集合附加在一起?

Is there a way to give this list to my program and have it perform all the data steps first on 2017SU then on the next semester and so on and append the resulting 5 sets together?

我不能只在数据步骤中放入所有感兴趣的术语,因为某些术语之间存在重复数据,需要单独处理.以前尝试将所有术语都放在数据步骤中对我没有用,所以我想通过每个学期单独运行整个程序来将它们分开.

I can't just put in all the terms of interest in the data step because some of the terms have repeating data between them and need to be dealt with separately. Trying to put all the terms in the data step has not worked for me before so I want to keep them separate by running the entire program separately for each semester.

推荐答案

只需将现有代码包装在宏中,并让宏遍历值列表.有关另一个示例,请参见另一个问题:简单迭代在SAS中通过带有proc sql的数组

Just wrap your existing code in a macro and have the macro iterate over the list of values. See this other question for another example: Simple iteration through array with proc sql in SAS

如果要将结果累积到单个表中,请在代码末尾添加 PROC APPEND 步骤.

If you want to accumulate the results into a single table then add a PROC APPEND step to the end of your code.

%macro do_all(termlist);
  %local i stc_term ;
  %do i=1 %to %sysfunc(countw(&termlist,%str( )));
    %let STC_TERM=%scan(&termlist,&i,%str( ));

data step1;
  set have ;
  where STC_TERM = "&STC_TERM";
  * ... other things I want to do in this step ... ;
run;
...
data step_last;
   set stepX;
   ...
run;

proc append base=all data=step_last force ;
run;

  %end;
%mend do_all;

然后用值列表调用它.

%do_all(2017SU 2016SU 2015SU 2014SU 2013SU)

这篇关于SAS循环遍历宏变量列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆