所以这就是问题所在。
我有一个数据集,对于每条记录,根据条件,我想加载不同的哈希。我不知道我将在运行时加载的每个哈希的确切哈希结构。所以我希望能够definedata
有条件地执行该语句。但是由于不知道hash结构,所以想到了definedata
通过变量将参数传递给语句,但是还是不行。我怎样才能做到这一点?这是我到目前为止所拥有的:
/* Hashes have the same key field */
data hash1;
key = '1'; a = 10; b = 20; output;
key = '2'; a = 30; b = 40; output;
run;
/* Hash objects can have different data members and types */
data hash2;
key = '1'; x = 'AAA'; y = 'BBB'; output;
key = '2'; x = 'CCC'; y = 'DDD'; output;
run;
/* This the dataset I want to process */
/* hid specifies which hash I should lookup */
/* key contains the key value to use for the lookup */
/* def is the hash data definition piece of the hash.
In practice I will use another hash to retrieve this definition
But for simplicity we can assume that is part of the have dataset itself */
data have;
hid = '1'; key = '2'; def = "'a', 'b'"; output;
hid = '2'; key = '1'; def = "'x', 'y'"; output;
run;
/* This is what I want */
data want;
set have;
/* Though I don't know the structure of each hash, I can get a list of all hashes at the onset via some macro processing. So this statement is doable */
if _N_ = 0 then set hash1 hash2;
/* This part is OK. The hash declaration is able to accept a variable for the dataset name */
hashname = "hash" || hid;
declare hash hh(dataset: dsname);
hh.definekey('key');
/* The following line is the problematic piece */
hh.definedata(def);
hh.definedone();
rc = hh.find();
/* Do something with the values */
/* Finally delete the object so that it can be redefined again on the next record */
hh.delete();
run;
我得到的错误是:ERROR: Undeclared data symbol 'a', 'b' for hash object。我认为这里的问题是,defineddata 方法会一一解析变量,最终将整个字符串'a', 'b'
视为一个变量。
如果我将散列定义为所有可能变量的超集,那么当我加载包含这些变量子集的数据集时,它就会报错。此外,我不能将散列定义为包含所有变量的超集(即,我不能创建所有散列来包含 a、b、x 和 y 并遗漏无关元素)。
所以我的问题是我怎样才能完成我在这里尝试做的事情?是否可以仅使用 datastep 构造逐个提供每个变量来进行宏 %do 之类的迭代?还是有其他方法可以做到这一点?
约束
- 我不能依赖宏处理,因为我只知道在运行时要使用哪个哈希。
- 由于内存原因,我无法提前加载所有定义。
任何帮助将不胜感激。