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

查看:53
本文介绍了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;

然后使用值列表进行调用.

Then call it with the list of values.

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

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

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