替换 SAS 宏变量中的引号
考虑以下 sas 代码片段:
%macro temp(querystr=);
filename request temp;
data _null_;
file request;
put "<string>&querystr</string>";
run;
%mend temp;
%temp(querystr="term 1" and "term2");
请注意,这段代码不会编译,因为当编译器在数据步骤中替换 querystr 时,querystr 中的第一个引号将关闭 put 语句的起始引号。
我想屏蔽查询字符串中的引号,将其转换为有效的 xml 片段,例如:
<string>"term 1" and "term 2"</string>
有没有办法通过正确屏蔽引号将上面的行输出到文件?我尝试将 %sysfunc(TranWrd()) 函数与 %nrbquote() 等屏蔽函数结合使用,但到目前为止我还没有找到可行的解决方案。任何帮助表示赞赏!
Consider the following sas code fragment:
%macro temp(querystr=);
filename request temp;
data _null_;
file request;
put "<string>&querystr</string>";
run;
%mend temp;
%temp(querystr="term 1" and "term2");
Note that this piece of code will not compile because the first quote in querystr will close the starting quote of the put statement when the compiler replaces querystr in the data step.
I would like to mask the quotes in the query string to transform it to a valid xml fragment, like:
<string>"term 1" and "term 2"</string>
Is there a way to output the above line to the file with proper masking of the quotation marks? I tried the %sysfunc(TranWrd()) function in combination with masking functions like %nrbquote() etc but so far I haven't found a working solution. Any help is appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将带有 'quot' 选项的 htmlencode 添加到数据步骤,并使用 %bquote 屏蔽引号,直到宏执行。
Add a htmlencode with 'quot' option to the datastep and use %bquote to mask the quotes until macro execution.
只要双引号平衡,宏就可以正常调用。因此,您不需要强制宏用户宏引用参数。您的宏可以为他们执行此操作,如下所示。
As long as the double quotes are balanced, macro is invoked fine. So, you don't need to impose on macro users to macro quote the parameter. Your macro can do this for them as below.
请尝试以下操作:
Try the following:
只需使用 put 'foo';反而:
Just use put 'foo'; instead: