替换 SAS 宏变量中的引号

发布于 2024-10-01 17:56:09 字数 591 浏览 8 评论 0原文

考虑以下 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>&quot;term 1&quot; and &quot;term 2&quot;</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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爱情眠于流年 2024-10-08 17:56:09

将带有 'quot' 选项的 htmlencode 添加到数据步骤,并使用 %bquote 屏蔽引号,直到宏执行。

%macro temp(querystr=);

  filename request temp;

  data _null_;

    file request;

    string = cats('<string>',htmlencode("&querystr",' quot'),'</string>');

    put string;

  run;

%mend temp;

%temp(querystr=%bquote("term 1" and "term2"));

Add a htmlencode with 'quot' option to the datastep and use %bquote to mask the quotes until macro execution.

%macro temp(querystr=);

  filename request temp;

  data _null_;

    file request;

    string = cats('<string>',htmlencode("&querystr",' quot'),'</string>');

    put string;

  run;

%mend temp;

%temp(querystr=%bquote("term 1" and "term2"));
离线来电— 2024-10-08 17:56:09

只要双引号平衡,宏就可以正常调用。因此,您不需要强制宏用户宏引用参数。您的宏可以为他们执行此操作,如下所示。

   %macro temp(querystr=);
     filename request temp;
     data _null_;
       file request;
       s = catx(htmlencode("%superq(querystr)","quot"),"<string>","</string>");
       l = length(s);
       put s $varying. l;
     run;
   %mend temp;

   %temp(querystr="term 1" and "term2");

   /* check */
   data _null_;
     infile request;
     input;
     put _infile_;
   run;
   /* on log
   <string>"term 1" and "term2"</string>
   */ 

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.

   %macro temp(querystr=);
     filename request temp;
     data _null_;
       file request;
       s = catx(htmlencode("%superq(querystr)","quot"),"<string>","</string>");
       l = length(s);
       put s $varying. l;
     run;
   %mend temp;

   %temp(querystr="term 1" and "term2");

   /* check */
   data _null_;
     infile request;
     input;
     put _infile_;
   run;
   /* on log
   <string>"term 1" and "term2"</string>
   */ 
倾城花音 2024-10-08 17:56:09

请尝试以下操作:

options mcompilenote=all;
%macro temp(querystr=); 
  filename request temp; 

  data _null_; 
    file request; 
    querystr=symget('querystr');
    querystr=htmlencode(querystr,'quot');
    *putlog querystr $;
    string="<string>"!!trim(querystr)!!"</string>";
    put string $200.;
    *put "<string>&querystr</string>"; 
  run; 
%mend temp; 

%temp(querystr="term 1" and "term2"); 

Try the following:

options mcompilenote=all;
%macro temp(querystr=); 
  filename request temp; 

  data _null_; 
    file request; 
    querystr=symget('querystr');
    querystr=htmlencode(querystr,'quot');
    *putlog querystr $;
    string="<string>"!!trim(querystr)!!"</string>";
    put string $200.;
    *put "<string>&querystr</string>"; 
  run; 
%mend temp; 

%temp(querystr="term 1" and "term2"); 
任谁 2024-10-08 17:56:09

只需使用 put 'foo';反而:

%macro temp(querystr=);
  filename request temp;

  data _null_;
    file request;

    put '<string>&querystr</string>';
  run;
%mend temp;

%temp(querystr="term 1" and "term2");

Just use put 'foo'; instead:

%macro temp(querystr=);
  filename request temp;

  data _null_;
    file request;

    put '<string>&querystr</string>';
  run;
%mend temp;

%temp(querystr="term 1" and "term2");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文