宏 Do 循环在 RSUBMIT 中无法正常工作 [英] Macro Do loop not working properly in RSUBMIT

查看:31
本文介绍了宏 Do 循环在 RSUBMIT 中无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的这段代码可以正常运行一年,但我需要将其转换为循环,以便它可以在 1970 年到 2015 年间运行多年.

So I have this code that works well for one year, but I need to convert it as a loop so it works for years from 1970 to 2015.

这是我在 %let 语句中指定的 1 年的代码.

Here is the code for 1 year that I specify in a %let statement.

%let year=1970
rsubmit; 
data home.historical_returns_&year;
set home.crspdata;
where (year <= &year - 1) and (year >= &year - 5);
returns_count + 1;
by id year;
if first.id or missing(tot_ret) then returns_count = 1;
run;
endrsubmit;

到目前为止,该代码对我来说效果很好.现在,我正在尝试使用循环,所以我在 1970 年到 2015 年使用它.

So far, that code works great for me. Now, I am trying to use a loop so I do it for year 1970 to 2015.

我想出了这个.看起来效果很好,但年份停留在 1970 年.

I have came up with this. Which looks like it works great, but the year stays at 1970.

%macro GMV;
rsubmit;
%do year=1970 %to 2015;
data home.historical_returns_&year;
set home.crspdata;
where (year <= &year - 1) and (year >= &year - 5);
returns_count + 1;
by id year;
if first.id or missing(tot_ret) then returns_count = 1;
run;
%end;
endrsubmit;

%mend GMV;

%GMV

在日志中,我看到名称中的 &year 从 1970 年到 1971 年到 1972 年实际上从未发生过变化,依此类推.所以我最终没有得到我需要的 45 个不同的数据集.

In the log, I see that the &year in the name never actually changes from 1970 to 1971 to 1972 and so on. So I do not end up with the 45 different datasets that I need.

有人遇到过这个问题吗?

Anybody ever had this problem?

谢谢!

推荐答案

您将远程处理与本地处理混合在一起,这会导致此类问题.你的宏变量不会更新(我有点惊讶它没有抛出关于 %do 循环的错误,个人).

You're mixing up remote processing with local processing in a way that's going to cause problems like this. Your macro variable won't be updated (and I'm a bit surprised it's not throwing an error about the %do loop, personally).

rsubmit;

%macro GMV;
%do year=1970 %to 2015;
data home.historical_returns_&year;
set home.crspdata;
where (year <= &year - 1) and (year >= &year - 5);
returns_count + 1;
by id year;
if first.id or missing(tot_ret) then returns_count = 1;
run;
%end;

%mend GMV;

%GMV
endrsubmit;

将整个宏放在 rsubmit 中以获得您正在寻找的结果 - 或者将整个 rsubmit 放在宏中(在我看来这不是一个好主意,尽管 Tom 在评论中指出这可能更安全某些情况下的选项).

Put the whole macro in the rsubmit to get the result you're looking for - or put the whole rsubmit in the macro (not as good of an idea in my opinion, though Tom in comments notes that it might be the safer option in some cases).

这篇关于宏 Do 循环在 RSUBMIT 中无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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