动态宏变量访问SAS [英] Dynamic macro variable access SAS
问题描述
我已经使用调用symputx创建了一个宏变量Item 1到Item N的列表,现在我想将它们传输到另一个数据步骤中的数组,以便数组中的点1获得Item1,点2获得Item2,依此类推.
I've used call symputx to create a list of macro variables Item 1 to Item N and now I want to transfer them to an array in another datastep so that spot 1 in the array gets Item1, spot 2 gets Item2, etc.
do j=1 to &num_OR;
rulesUsed{j}=&&Item&j;
end;
我读到,双与号语法是引用此类宏变量的方式,但我不断遇到各种错误.我敢肯定有一个简单的方法可以解决这个问题,但是我是SAS的新手,在搜索中没有我读过的文档中提到这种确切类型的问题.
I read that the double ampersand syntax is the way to reference macro variables like this but I keep getting all sorts of errors. I'm sure there's a simple way around this but I'm new to SAS and no document I've read through that's come up in searches mentions this exact type of problem.
推荐答案
简短的答案是:通常不要这样做.宏变量不是存储数据的好方法,而且几乎总是有更好的方法.
The short answer is: don't do this, in general. Macro variables aren't a great way to store data, and there's nearly always a better way.
但是,如果需要,这里的问题是宏变量不能使用数据步长变量.
But if you need to, your issue here is that the macro variable can't use the data step variable.
do j=1 to &num_OR;
rulesUsed{j}=&&Item&j;
end;
j
是数据步变量,而不是宏变量,因此不是&j
.您需要:
j
is a data step variable, not a macro variable, and so it's not &j
. You need to either:
1-使用symget
检索宏变量.这是一个数据步骤函数,它使用常规的数据步骤字符参数(因此是变量,"字符串等),并返回具有该名称的宏变量.所以
1 - Use symget
to retrieve the macro variable. That's a data step function that takes a normal data step character argument (so a variable, a " " string, etc.) and returns the macro variable with that name. So
rulesUsed[j] = symget(cats("item",j));
2-使用宏循环检索宏变量.
2 - Use a macro loop to retrieve the macro variable.
%do j = 1 %to &num_or;
rulesUsed[&j.] = &&item&j;
%end;
这两种方法都可以正常工作.
Either of these methods work fine.
这篇关于动态宏变量访问SAS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!