如何在 SAS 中处理 href 中的撇号

  • 本文关键字:href 处理 SAS html sas
  • 更新时间 :
  • 英文 :


我有一个使用 HTML 发送电子邮件的 SAS 程序,但我尝试引用为链接的文件夹之一包含一个撇号:

%let body1 = %nrquote(
<ul>
<li><a href='\serverStudiesAlzheimer'sDocuments'>Alzheimer's Documents</a>
</ul>
) ;

此宏变量将在以下宏中使用:

%macro sas_email(to=, subject=, body1=, body2=, body3=) ;
options 
  emailsys=smtp 
  emailhost=("smtp.gmail.com" port=465) ;
filename alert email to=(&to.) 
                     subject="&subject." 
                     content_type="text/html" 
                     debug ;
data _null_ ;
  file alert ;
  put "&body1." ;
  %if %length(&body2.) > 0 %then %do ;
  put "&body2." ;
  %end ;
  %if %length(&body3.) > 0 %then %do ;
  put "&body3." ;
  %end ;
run ;
%mend sas_email ;

可以想象,Alzheimer's中的撇号会导致问题。 使用双引号而不是单引号给我错误:

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric 
operand is required. The condition was: %length(&body1.) > 0

HTML不在乎你使用双引号还是单引号。 因此,您生成的 HTML 标记可能如下所示:

<a href="\serverStudiesAlzheimer'sDocuments">
如果要将单引号

添加到括在单引号中的字符串中,请对其进行编码。

<a href='\serverStudiesAlzheimer%27sDocuments'>

由于您将在数据步骤中使用宏变量,因此在创建值时,请尝试仅使用 %BQUOTE() 添加宏引用。这应该允许您创建一个字符串,该字符串在 SAS 中看起来像不平衡的引号。

%let body1 = %bquote(
<ul>
<li><a href="\serverStudiesAlzheimer'sDocuments">Alzheimer's Documents</a>
</ul>
) ;
%let body2=;
%let body3=;

然后,使用它时,避免尝试扩展宏变量,方法是使用symget()函数将宏变量的值拉取为实际变量,然后可以使用 PUT 语句编写该变量。

data _null_;
  file alert ;
  length str $32767;
  do i=1 to 3;
    str=symget(cats('body',i));
    put str ;
  end;
run;

最新更新