从数据变量生成宏变量的宏函数

从数据变量生成宏变量的宏函数

本文介绍了从数据变量生成宏变量的宏函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

data sample;
    input x $;
    datalines;
one
two
three
;

%macro variable_to_macvar(variable=, dataset=);
    proc sql noprint;
        select &variable into : outlist separated by ' '
        from &dataset;
    quit;
&outlist
%mend variable_to_macvar;

%put %variable_to_macvar(variable=x, dataset=sample);

预期输出:一二三.相反,我收到了一个错误.为什么?这可以解决吗?

我已经成功地创建了其他非常相似形式的宏,其中函数使用宏末尾的 & 宏变量返回"一个值,没有分号.例如,这里有一个类似的函数类型:

I've successfully created other macros of a very similar form, where the function "returns" a value using the &macrovariable at the end of the macro without a semicolon. For example, here is a similar type of function that works:

%macro zcat(first=5, last=15, prefix=freq);
    %let x=;
    %do i = &first %to &last;
        %let x=&x &prefix.&i;
    %end;
    &x
%mend zcat;
%put %zcat();

推荐答案

DOSUBL 在 9.3(至少是 9.3TS1M2,我有)中可用(但实验性).这就是你要做的.

DOSUBL is available (but experimental) in 9.3 (at least, 9.3TS1M2, which I have). This is how you'd do it.

data sample;
    input x $;
    datalines;
one
two
three
;

%macro variable_to_macvar(variable=, dataset=);
   %let rc=%sysfunc(dosubl(%str(
    proc sql noprint;
        select &variable into : outlist separated by ' '
        from &dataset;
    quit;
    )));
&outlist
%mend variable_to_macvar;

%put %variable_to_macvar(variable=x, dataset=sample);;

如果你不能使用 DOSUBL,或者想避免实验性的事情,你可以用 PROC FCMP 而不是宏来做到这一点.如果您喜欢编写函数,PROC FCMP 可能适合您:实际上可以编写函数,而不必处理宏语言的烦恼.

If you can't use DOSUBL, or want to avoid experimental things, you can do this with PROC FCMP rather than a macro. If you like to write functions, PROC FCMP is probably for you: actually being able to write functions, rather than having to deal with the annoyances of the macro language.

这篇关于从数据变量生成宏变量的宏函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 08:15