连接引用的宏变量 [英] Concatenate quoted macro variables

查看:139
本文介绍了连接引用的宏变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是试图连接两个带引号的宏变量,但是似乎没有一种简单的方法。

I'm just trying to concatenate two quoted macro variables but there doesn't seem to be an easy way.

说我们有:

%LET VAR1="This is not the greatest song in the world";
%LET VAR2="this is just a tribute.";

%LET TRIBUTE=%SYSFUNC(CATX(%STR( ),&VAR1,&VAR2));
%PUT &TRIBUTE;

我实际上想要:

  "This is not the greatest song in the world this is just a tribute."

但是上面的代码实际上产生:

But the above code actually yields:

"This is not the greatest song in the world"  "this is just a tribute."

所以我尝试将%QUOTE()%BQUOTE 等。 & VAR1 %VAR2 左右,希望能掩盖引号,但我得到相同的结果。

So I try putting %QUOTE(),%BQUOTE,etc. around &VAR1 and %VAR2 in hopes of unmasking the quotes but I get the same result.

对我唯一有用的是:

 %LET TRIBUTE="%SUBSTR(&VAR1.,2,%LENGTH(&VAR1.)-2) %SUBSTR(&VAR2.,2,%LENGTH(&VAR2.)-2)"; 

但这很丑陋,而且很快就会变得冗长。
难道没有更好的方法吗?

But this is ugly and can get lengthy really fast. Is there not a better way to do this ?

推荐答案

您可以使用COMPRESS来做到这一点。

You can use COMPRESS to do this.

%LET VAR1="This is not the greatest song in the world";
%LET VAR2="this is just a tribute.";


%let VAR3=%sysfunc(compress(&VAR1,%str(%")));
%put &=var1 &=var3;

删除引号有点棘手,但可以。

It's a bit tricky to remove quotes, but it works.

您也可以在FCMP函数或函数样式宏中进行此操作;这是一个示例。

You could also do this in a FCMP function, or a function-style macro; here is one example.

%macro unquote_string(string=);
%sysfunc(compress(&string.,%str(%'%")))
%mend unquote_string;

%let VAR3="%unquote_string(string=&var1.) %unquote_string(string=&var2.)";
%put &=var3.;

请注意,您不应使用CAT函数来连接宏变量。它们只是文本,因此一个接一个地输入会自动将它们串联起来。

Note you shouldn't use CAT functions to concatenate macro variables. They're just text, so typing one after the other automatically concatenates them.

但是,是否有更好的方法这一问题的真正答案是:不要将引号存储在宏变量中。大多数时候,您应该存储不带引号的宏变量,并在需要时在引号内使用它。 SAS宏不将引号视为特殊字符-它们只是字符串中的一个字符-因此,它们没有用于处理引号的特殊工具。

However, the real answer to your question of 'Is there a better way', is to not store the quotes in the macro variable. Most of the time you should store the macro variable w/o quotes and use it within quotes when needed. SAS Macros do not respect quotes as anything special - they're just a character in a string - so they don't have a specific tool for dealing with this.

这篇关于连接引用的宏变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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