SAS数据中存在“&”符号时出现明显的符号引用。

huangapple go评论63阅读模式
英文:

SAS Apparent symbolic reference when there is a n ampersand in data

问题

这似乎是一个旧问题,但我还没有看到完全解决它的答案。完全有可能是我漏看了。

我正在处理一个包含名为fullDescription的文本字段的数据,其中包含一个类似以下模式的字符串:"00001234456 Wells Fargo DR FM AT&T PYMT 00987600"。

我试图解析数据并提取诸如"Wells Fargo"和"AT&T"之类的片段。然而,当我操作"AT&T"时,SAS试图将其读取为"AT",然后是变量值T。当我加入这行代码时,错误虽然停止了(但仍有警告):

%LET description = %SYSFUNC(COMPRESS(%BQUOTE(&&fullDescription&row), '', P));

这样至少返回"00001234456 Wells Fargo DR FM ATT PYMT 00987600"(缺少和号),但仍然抛出警告:

WARNING: Apparent symbolic reference T not resolved

我还没有找到防止警告的方法。有没有办法保留和号但不将其视为变量?如果这不可能,我能否清理它一次而不会出现错误?

英文:

This appears to be an old problem but I haven't seen an answer that fully addresses it. Totally possible I just missed it.

I'm consuming data that has a text field called fullDescription that contains a string like (made up but fits the pattern):
"00001234456 Wells Fargo DR FM AT&T PYMT 00987600"

I'm attempting to parse the data and dig out tidbits like "Wells Fargo" and "AT&T". However, when I manipulate "AT&T" SAS tries to read it as "AT" then the variable value for T. It stopped erroring (but still warns) when I instituted this line:

%LET description = %SYSFUNC(COMPRESS(%BQUOTE(&&fullDescription&row),'',P));

This, at least, returns "00001234456 Wells Fargo DR FM ATT PYMT 00987600" (missing ampersand) but still throws:

WARNING: Apparent symbolic reference T not resolved

I haven't figured out a way to prevent the warning. Is there a way to leave the ampersand in but not treat it as a variable? If that's not possible, can I cleanse it once and not get the error?

答案1

得分: 1

你已经快完成了,只需在定义宏变量 fullDescription 时添加 %nrstr() 函数。

data _null_;
  call symputx('fullDescription','%nrstr(00001234456 Wells Fargo DR FM AT&T PYMT 00987600)');
run;

%LET description = %SYSFUNC(COMPRESS(%bquote(&fullDescription),' ',P));
%put &description;

这样就不再产生警告。

英文:

You are almost there, just add %nrstr() function when define the macro variable fullDescription.

data _null_;
  call symputx('fullDescription','%nrstr(00001234456 Wells Fargo DR FM AT&T PYMT 00987600)');
run;

%LET description = %SYSFUNC(COMPRESS(%bquote(&fullDescription),'',P));
%put &=description;

This makes no warning anymore.

答案2

得分: 1

如果您已经创建了一个包含宏触发器(如 & 或 %)且您不希望解析的宏变量,请使用宏引用。

%SUPERQ() 用于检索宏变量的值并对其应用宏引用。请使用 %QSYSFUNC() 而不是 %SYSFUNC(),以使 SAS 函数返回的结果带引号。

让我们创建一些宏变量,就像在您的问题中一样。

    data _null_;
      call symputx('fulldescription1','AT&T');
    run;
    %let row=1;

现在让我们尝试调用 COMPRESS() 函数。

    %LET description=%QSYSFUNC(COMPRESS(%SUPERQ(fullDescription&row),'',P));

结果:

    274  data _null_;
    275    call symputx('fulldescription1','AT&T');
    276  run;
    
    NOTE: DATA statement used (Total process time):
          real time           0.00 seconds
          cpu time            0.00 seconds
    
    277  %let row=1;
    278
    279  %LET description=%QSYSFUNC(COMPRESS(%SUPERQ(fullDescription&row),'',P));
    280
    281  %put ROW=%superq(row);
    ROW=1
    282  %put FULLDESCRIPTION&row=%superq(fulldescription&row);
    FULLDESCRIPTION1=AT&T
    283  %Put DESCRIPTION=&description;
    DESCRIPTION=ATT
英文:

If you have created a macro variable that contains macro triggers like & or % that you do not want resolved use macro quoting.

%SUPERQ() is useful to retrieving the value of a macro variable and applying macro quoting to it. Use %QSYSFUNC() instead of %SYSFUNC() to have the results return by the SAS function quoted.

Let's create some macro variables like in your problem.

data _null_;
  call symputx('fulldescription1','AT&T');
run;
%let row=1;

Now let's try your call to the COMPRESS() function.

%LET description=%QSYSFUNC(COMPRESS(%SUPERQ(fullDescription&row),'',P));

Results:

274  data _null_;
275    call symputx('fulldescription1','AT&T');
276  run;

NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


277  %let row=1;
278
279  %LET description=%QSYSFUNC(COMPRESS(%SUPERQ(fullDescription&row),'',P));
280
281  %put ROW=%superq(row);
ROW=1
282  %put FULLDESCRIPTION&row=%superq(fulldescription&row);
FULLDESCRIPTION1=AT&T
283  %Put DESCRIPTION=&description;
DESCRIPTION=ATT

huangapple
  • 本文由 发表于 2023年2月18日 07:27:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75490105.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定