创建一系列以升序数值命名的全局宏变量

时间:2013-12-05 12:35:22

标签: sas sas-macro

我构建了以下宏,其中代码遍历数字1到82,并在创建该宏变量时分配在proc format语句中定义的格式:

%macro metric_var;

/*macro to define 86 metric variables using proc format statement. this only needs doing once. to save processing time*/
/*this macro is outside of the main macro loop and the proc format statement is outside the do loop within the macro*/
/*as again it only needs defining once.*/

proc format;
value metricvar 

1 = a 2 = y 3 = b 4 = d 5 = b2
6 = r1 7 = b3 8 = q 9 = p 10 = o 

11 = c1 12 = d1 13 = c 14 = d2
15 = c6  16 = t1 17 = k2 18 = p2

19 = c8 20 = m5 21 = c3 22 = m6
23 = g 24 = m7 25 = h 26 = m8
27 = k1  28 = m3 29 = l 30 = m4
31 = l1  32 = t8

33 = w1 34 = g1  35 = w4 36 = g3
37 = p1 38 = g4 39 = m 40 = g5
41 = m2 42 = g6 

43 = k 44 = v 45 = j 46 = j1
47 = j5 48 = j3 49 = k4 50 = f6
51 = j6 52 = n 53 = k5 54 = n4 
55 = w 56 = s 57 = s1 58 = x
59 = j2

60 = v 61 = a5 62 = b6 63 = k3
64 = t7 65 = a2 66 = t6 67 = i5 

68 = l2 69 = e 70 = e7  71 = e8 
72 = e9 73 = b4 74 = j4 75 = p5  
76 = p6 77 = r 78 = r2 79 = r5   
80 = r6  81 = r7 82 = s7

/*don't forget now runs to 86 metrics*/
;
run;

%let metvar1 = 1;
%let metvar2 = 82;

%do U = &metvar1. %to &metvar2. %by 1;


data _null_;
a = &u.;
call symput('myvar',(put(a,metricvar.)));
run;

%let myvar_&u. = &myvar.;
%put &myvar.;
%put &myvar_&u.;


%end;
%mend;

%metric_var;

在此,宏变量&myvar.正常工作,并按照我的意愿按升序分配值。

但是,这个变量被分配了82次新值,并且在过程结束时它具有最后一次迭代的值。

我尝试使用行%let myvar_&u. = &myvar.;创建了82个单独的全局变量,这些变量将被称为&myvar_1.&myvar_82.,每个变量都有不同的值。< / p>

当日志运行时,会出现以下警告

WARNING: Apparent symbolic reference MYVAR_ not resolved.
&myvar_79

似乎最终变量,我想要的那个没有被正确分配,我不知道为什么。

有没有人对问题是什么有任何建议?

由于

2 个答案:

答案 0 :(得分:2)

您不需要宏来执行此操作。老实说,你可能根本不需要这样做 - 很可能你正在使用它的东西在数据步骤中完全做得更好 - 但即使你已经说明了你的愿望,这也是在数据步骤中完成的。

data _null_;
do iter = 1 to 82;
call symputx(cats('myvar_',iter),put(iter,metricvar.));
end;
run;

毕竟,CALL SYMPUT(x)接受一个字符参数,而不是常量 - 所以你可以修改82次。

答案 1 :(得分:1)

%macro metric_var;
%let metvar1 = 1;
%let metvar2 = 82;

%do U = &metvar1. %to &metvar2. %by 1;
    %let myvar_&u. = &u;
    %put myvar_&u is: &&myvar_&u;
%end;
%mend;

%metric_var;

魔法在&&myvar_&u - 双放大器得到的解析为&amp; myvar_1等宏变量的值。 如果myvar也是宏变量,那么你需要tripple ampresand &&&myvar_&u

我也跳过了数据步骤,不需要它。