我通常首先编写一个proc SQl而没有任何宏变量并且它可以工作,现在我想将它转换为宏并且它不起作用,你能看到问题所在吗?
%macro macrova(LIB=, DATA=);
proc sql noprint;
/*creating an in memory variable with all char variables from the dataset*/
select name into :names separated by ' '
from dictionary.columns where libname="&lib" and memname="&data" and type='char';
/*to make sure we have the same order of the variables an in-memory variable ORDER is created*/
SELECT NAME INTO: ORDER SEPARATED BY ' '
from sashelp.vcolumn where LIBNAME= "&lib" and memname="&datA" ;
quit;
%MEND;
OPTIONS MLOGIC SYMBOLGEN;
%macrova(LIB=SASHELP,DATA=CLASS)
%PUT &NAMES;
%PUT ℴ
日志:
55 %PUT &NAMES;
WARNING: Apparent symbolic reference NAMES not resolved.
&NAMES
56 %PUT ℴ
WARNING: Apparent symbolic reference ORDER not resolved.
&ORDER
答案 0 :(得分:4)
您需要在调用宏之前定义宏变量,或者将%GLOBAL语句添加到宏。否则,宏变量将被创建为本地,并在宏退出时消失。
请注意,dictionary.columns中没有名为ORDER
的变量,我假设您打算使用VARNUM
变量。
此外,无需运行两个查询来生成两个宏变量。
%macro macrova(LIB=, DATA=);
%global names order ;
proc sql noprint;
select name
, varnum
into :names separated by ' '
, :order separated by ' '
from dictionary.columns
where libname=%upcase("&lib")
and memname=%upcase("&data")
and type='char'
order by 2
;
quit;
%mend macrova;
%macrova(LIB=SASHELP,DATA=CLASS)
%put &NAMES;
%put ℴ
答案 1 :(得分:0)
名称在宏内创建为LOCAL值,并且在其外部不可用。尝试添加
%global NAMES ORDER;
到SELECT
语句之前的宏。