3

Executing locally in a clean session:

%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?

jl6
  • 6,110
  • 7
  • 35
  • 65

5 Answers5

3

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.

Laurent de Walick
  • 2,154
  • 14
  • 11
  • But why would masking a character string during macro compilation cause this error? I would expect all macro quoting to be resolved by the time the code is executed. – jl6 Jun 21 '11 at 13:19
  • 1
    The %str masks strings during compilation while its sibling %quote masks strings during execution. It's something in the combination of proc sql, macro compilation and execution. This paper explains a lot about macro quoting: http://www2.sas.com/proceedings/sugi28/011-28.pdf – Laurent de Walick Jun 22 '11 at 05:43
2

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!

jl6
  • 6,110
  • 7
  • 35
  • 65
1

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;
cmjohns
  • 4,465
  • 17
  • 21
  • The example is somewhat artificial; I'm aware of any number of workarounds, so what I'm really asking is WHY this error occurs. – jl6 Jun 21 '11 at 13:17
1

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.));

????

richie
  • 106
  • 2
0

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

Jay Corbett
  • 28,091
  • 21
  • 57
  • 74