SAS Do 循环:在循环内使用循环变量来创建滞后变量 [英] SAS Do loops: use loop variable inside the loop to create lagged variables
问题描述
我想创建包含大量滞后的给定变量的滞后值的变量.我怎么能这样做?我尝试以下操作:
I would like to create variables containing lagged values of a given variable for a large number of lags. How could I do this? I try the following:
data out;
set in;
do i = 1 to 50;
%let j = i;
lag_&j = Lag&j.(x);
end;
run;
如何将循环变量 i
放入宏变量 j
或如何直接使用它来创建适当命名的变量和 Lag 函数?>
How can I get the loop variable i
into the macro variable j
or how to use it directly to create the appropriately named variable and for the Lag function?
推荐答案
Chris J 回答了这个问题,但在这里我将提供我首选的方法.
Chris J answers the question, but here i'll provide my preferred way of doing this.
%macro lagvar(var=,num=);
%do _iter = 1 %to &num.;
lag_&_iter. = lag&_iter.(&var.);
%end;
%mend lagvar;
data out;
set in;
%lagvar(var=x,num=50); *semicolon optional here;
run;
这是宏循环的更模块化用法(并且更具可读性,假设您使用智能名称 - 上面没问题,如果您想非常清楚,您可以对名称进行更多操作,当然还可以添加注释).
This is a more modular usage of the macro loop (and more readable, assuming you use intelligent names - the above is okay, you could do even more with the name if you wanted to be very clear, and of course add comments).
这篇关于SAS Do 循环:在循环内使用循环变量来创建滞后变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!