在调用符号语句中使用动态宏变量 [英] Using a dynamic macro variable in a call symput statement

查看:37
本文介绍了在调用符号语句中使用动态宏变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不久前发布了一个关于修剪宏变量的问题,我用它来从 Yahoo Finance 下载 CSV,其中包含每次传递到网站的变量信息.建议我实现这一点的代码如下:

I posted a question a while back about trimming a macro variable down that I am using to download a CSV from Yahoo Finance that contains variable information on each pass to the site. The code that was suggested to me to achieve this was as follows:

data _null_;
a = "&testvar.";
call symputx('svar',trim(input(a,$8.)));
run;

效果很好,但是我此后需要重新设计代码,以便我可以声明多个宏变量并同时提交多个宏变量.

That worked great, however I have since needed to redesign the code so that I am declaring multiple macro variables and submitting multiple ones at the same time.

为了同时声明多个宏,我使用了以下代码行:

To declare multiple macros at the same time I have used the following lines of code:

%let svar&e. = &svar.;
%put stock_ticker = &&svar&e.;

变量 &e.是一个每次增加一个的迭代变量.这声明了一个看起来与名为 &svar 的宏相同的宏.每次将它们放入日志时,但是新的动态宏现在会抛出原始警告消息:

The varible &e. is an iterative variable that goes up by one everytime. This declares what looks to be an identical macro to the one called &svar. everytime they are put into the log, however the new dynamic macro is now throwing up the original warning message of:

WARNING: The quoted string currently being processed has become more than 262 characters long.  You
         may have unbalanced quotation marks.

这是我在开始使用原始问题中建议的 symputx 选项之前得到的.

That i was getting before i started using the symputx option suggested in my original problem.

下面列出了这个特定嵌套宏的完整代码:

The full code for this particular nested macro is listed below:

%macro symbol_var;

/*here the start row and end row created in the macro above are passed to this nested macro and then passed through the*/
/*source dataset. at the end of the loop each ticker macro variable is defined in turn for use in the following nested*/
/*macro, symbol by metric.*/

%do e = &beg_point. %to &end_point. %by 1;

%put stock row in dataset nasdaq ticker = &e.;

%global svar&e;

proc sql noprint;
select symbol
into :testvar
from nasdaq_ticker
where monotonic() = &e.;
quit;

/*convert value to string here*/

data _null_;
a = "&testvar.";
call symputx('svar',trim(input(a,$8.)));
run;

%let svar&e. = &svar.;
%put stock_ticker = &&svar&e.;


%end;
%mend;

%symbol_var;

任何人对我如何声明宏 &&svar&e 有任何建议.直接进入调用synputx步骤?它目前抛出一个错误,指出正在创建的宏变量不能包含任何特殊字符.我曾尝试使用 &QUOTE、%NRQUOTE 和 %NRBQUOTE,但要么我在无效上下文中使用了该函数,要么语法不完全正确.

Anyone have any suggestions how I could declare the macro &&svar&e. directly into the call synputx step? It currently throws up an error saying that the macro variable being created cannot contain any special characters. Ive tried using &QUOTE, %NRQUOTE and %NRBQUOTE but either I have used the function in an invalid context or I haven't got the syntax exactly right.

谢谢

推荐答案

这不就是下面两行数据步骤那么简单吗?

Isn't this as simple as the following two line data step?

%macro symbol_var;

/*here the start row and end row created in the macro above are passed to this nested macro and then passed through the*/
/*source dataset. at the end of the loop each ticker macro variable is defined in turn for use in the following nested*/
/*macro, symbol by metric.*/

data _null_;
   set nasdaq_ticker(firstobs=&beg_point. obs=&end_point.);
   call symputx('svar' || strip(_n_), symbol);
run;

%mend;

%symbol_var;

或以下(包括调试输出)

Or the following (which includes debugging output)

%macro symbol_var;

/*here the start row and end row created in the macro above are passed to this nested macro and then passed through the*/
/*source dataset. at the end of the loop each ticker macro variable is defined in turn for use in the following nested*/
/*macro, symbol by metric.*/

data _null_;
   set nasdaq_ticker(firstobs=&beg_point. obs=&end_point.);
   length varname $ 32;
   varname = 'svar' || strip(_n_);
   call symputx(varname, symbol);

   put varname '= ' symbol;
run;

%mend;

%symbol_var;

在操作宏变量和需要防弹代码时,我经常发现自己恢复使用数据 null 步骤.原始帖子包含有关带引号的字符串警告的问题.发生这种情况是因为 SAS 宏解析器不会对语法扫描器隐藏宏变量的值.这意味着您的数据(存储在宏变量中)可能会在您的程序中产生语法错误,因为 SAS 试图将其解释为代码(不寒而栗!).它真的让我脖子后面的头发竖起来,冒着我的程序受到数据中可能存在的风险.使用数据步骤和函数可以完全保护您免受此影响.您会注意到,除了观察窗口点之外,我的代码从不使用与号字符.这使我的代码能够证明 nasdaq_ticker 数据集中可能存在哪些脏数据.

When manipulating macro variables and desiring bullet-proof code I often find myself reverting to using a data null step. The original post included the problem about a quoted string warning. This happens because the SAS macro parser does not hide the value of your macro variables from the syntax scanner. This means that your data (stored in macro vars) can create syntax errors in your program because SAS attempts to interpret it as code (shudder!). It really makes the hair on the back of my neck stand up to risk my program at the hands of what might be in the data. Using the data step and functions protects you from this completely. You will note that my code never uses an ampersand character other than the observation window points. This makes my code bullet proof regarding what dirty data there may be in the nasdaq_ticker data set.

另外,重要的是要指出 Dom 和我都编写了代码,可以通过 nasdaq_ticker 数据集.不是要破坏原始发布的代码,而是以这种方式循环会导致对结果集中的每个观察进行 proc sql 调用.这将为大型结果集带来非常差的性能.我建议您了解宏循环将导致您读取数据集的次数.我在自己的代码中被这个咬过很多次了.

Also, it is important to point out that both Dom and I wrote code that makes one pass over the nasdaq_ticker data set. Not to bash the original posted code, but looping in that way causes a proc sql invocation for every observation in the result set. This will create very poor performance for large result sets. I recommend developing an awareness of how many times a macro loop is going to cause you to read a data set. I have been bitten by this many times in my own code.

这篇关于在调用符号语句中使用动态宏变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆