我正在寻找一种在数据步骤中使用普通变量值作为宏变量的方法。
例如,我有宏变量 &statesList_Syphilis = AAA
和另一个宏变量 &statesList_Giardia = BBB
在数据步骤中,我有一个变量Germ ,其中包含 2 行:“Syphilis”和“Giardia”。
在我的数据步骤中,当 Germ="Syphilis" 迭代第一行时,我需要找到AAA
,
而当 Germ="Giardia" 迭代第二行时,我需要找到BBB
尝试看起来像这样
%let statesList_Syphilis = AAA;
%let statesList_Giardia = BBB;
data test;
set mytablewithgerms; * contains variable Germ ;
* use germ and store it in &germ macro variable ;
* something like %let germ = germ; or call symput ('germ',germ);
* I want to be able to do this;
xxx = "&&statesList_&germ"; * would give xxx = "AAA" or xxx = "BBB";
* or this;
&&statesList_&germ = "test"; * would give AAA = "test" or BBB = "test";
run;
我不认为这是可能的,但我想我会问只是为了确定。
谢谢!
编辑(根据评论中的问题,我正在为我的具体问题添加上下文,但我觉得这让事情变得更加复杂):
这是一种简化问题的尝试。
实际上AAA和BBB是一长串单词,例如
"asymptomatic_1 fulminant_1 chronic_1 chronic_1 fatalFulminant_1 hepatocellular_1 compensated_1 hepatocellular_2 decompensated_1 fatalHepatocellular_1 fatalHepatocellular_2 fatalDecompensated_1"
而且我不想将这个长字符串存储在变量中,我想在 do 循环中迭代这个字符串的每个单词,例如:
%do k=1 %to %sysfunc(countw(&&statesList_&germ));
%let state = %scan(&&statesList_&germ, &k);
* some other code here ;
%end;
EDIT2:
这是我的问题的更完整视图:
%macro dummy();
data DALY1;
* set lengths ;
length Germ $10 Category1 $50 Category2 $50 AgeGroupDALY $10 Gender $2 value 8 stateList$999;
* make link to hash table ;
if _n_=1 then do;
*modelvalues ----------------;
declare hash h1(dataset:'modelData');
h1.definekey ('Germ', 'Category1', 'Category2', 'AgeGroupDALY', 'Gender') ;
h1.definedata('Value');
h1.definedone();
call missing(Germ, Value, Category1, Category2);
* e.g.
rc=h1.find(KEY:Germ, KEY:"ssssssssss", KEY:"ppppppppppp", KEY:AgeGroupDALY, KEY:Gender);
*states ---------------------;
declare hash h2(dataset:'states');
h2.definekey ('Germ') ;
h2.definedata('stateList');
h2.definedone();
end;
set DALY_agregate;
put "°°°°° _n_=" _n_;
DALY=0; * addition of terms ;
rc2=h2.find(KEY:Germ); * this creates the variable statesList;
put "statesList =" statesList;
* here i need statesList as a macro variable,;
%do k=1 %to %sysfunc(countw(&statesList)); *e.g. acute_1 asymptomatic_1 ...;
%let state = %scan(&statesList, &k);
put "=== &k &state";
&state = 1; * multiplication of terms ;
* more code here;
%end;
run;
%mend dummy;
%dummy;
EDIT3:
输入数据集如下所示
Germ AgeGroup1 AgeGroup2 Gender Cases Year
V_HBV 15-19 15-19 M 12 2015
V_HBV 15-19 15-19 M 8 2016
V_HBV 20-24 20-24 F 37 2011
V_HBV 20-24 20-24 F 46 2012
V_HBV 20-24 20-24 F 66 2013
输出数据集将添加包含在由依赖于 Germ 的宏变量定义的字符串中的变量。
例如对于 V_HBV,它将创建以下变量:无症状_1 暴发性_1 慢性_1 慢性_1 致命暴发_1 肝细胞_1 补偿_1 肝细胞_2 失代偿_1 致命肝细胞_1 致命肝细胞_2 致命去补偿_1