开发者

How does SAS macro quoting interact with format literals?

Executing locally in a clean sess开发者_如何学Goion:

%let x = %str(put(age, best.));

proc sql;
    select &x from sashelp.class;
quit;

This generates the following error:

1     put(age, best.)
               ----
               22
                ----
                76
ERROR 22-322: Syntax error, expecting one of the following: a format name, ?.

ERROR 76-322: Syntax error, statement will be ignored.

But this "manually-resolved" version runs without notes, warnings or errors:

proc sql;
    select put(age, best.) from sashelp.class;
quit;

Can somebody explain exactly what %str() is doing in this program that causes an issue at execution time? Apologies for the vague question, but I am unsure what the relevant interactions are; I cannot replicate using equivalent data-step syntax so perhaps proc SQL peculiarities are involved?


The %str() function masks a character string during macro compilation. Remove the %str() function in the let statement or add an %unquote() function in the sql select to have if resolve correctly.


Answered at this question on runsubmit.com:

I'm going to mark this answer as correct because it led me to this page of documentation: http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#tw3514-unquote.htm - "In rare cases, masking text with a macro quoting function changes the way the word scanner tokenizes the text ... The word scanner does not use it as the boundary of a literal token in the input stack". Sounds like a bug, frankly, but if the tokenizer algorithm is as ancient and hairy as I imagine, I'd spin it as a quirk too!


Can you use a format statement instead? For example, this works just fine.

%let x = %str( age format=best.);

proc sql;
    select &x. from sashelp.class;
quit;


For some reason SAS doesn't like the "best." format.

i.e. when I try this, your code works

 %let x = %str(put(age, 8.));

????


If you add this to your code

%put _user_ ;

you will see how &x is quoted by %str, in the log. That is why the proc sql code doesn't work. Using %Unquote in the select portion of the proc sql statement will allow the code to run.

http://www2.sas.com/proceedings/forum2007/152-2007.pdf

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜