1

I am trying to insert a row into a SAS data set using PROC SQL and the values of existing macro variables but I'm getting the standard syntax error message. Here is an example of the code that fails:

%let viewname=MKTVIEWS.imei_ref;
%let xrc=0;
%let xmsg= ;

proc sql;
   create table results (viewname char(40), xrc numeric, xmsg char(200));
   insert into results (viewname, xrc, xmsg)
   values ( %str(%')&viewname%str(%') 
          , &xrc 
          , %str(%')%superq(xmsg)%str(%') );
quit;

Here is the error message:

ERROR 22-322: Syntax error, expecting one of the following: a quoted string, 
a numeric constant, a datetime constant, a missing value, +, -, MISSING, NULL,
USER.

Running the program without the two character macro variables works fine:

proc sql;
   create table results (viewname char(40), xrc numeric, xmsg char(200));
   insert into results (viewname, xrc, xmsg)
   values ( 'MKTVIEWS.imei_ref'
          , &xrc 
          , '' );
quit;

Clearly I'm missing something about macro quoting or something similar. I even tried using temporary macro variables rather than embedding those %STR calls to create a quoted string, but that didn't work either.

4

3 回答 3

3

Maybe I'm missing something, but wouldn't "&viewname" do the job?

于 2013-06-26T19:42:44.563 回答
1

我发现使用 datastep 引用函数最容易,主要是因为我不擅长真正的宏引用。

%let viewname=MKTVIEWS.imei_ref;
%let xrc=0;
%let xmsg= ;


options symbolgen mprint;
proc sql;
   create table results (viewname char(40), xrc numeric, xmsg char(200));
   insert into results (viewname, xrc, xmsg)
   values ( %sysfunc(quote(&viewname))
          , &xrc 
          , %sysfunc(quote(%superq(xmsg))) );
quit;

这是否实现了您的期望?

于 2013-06-26T19:34:24.273 回答
1

鲍勃,它不起作用,因为它起作用了。%STR 蒙面引号不仅来自宏编译器(如您所料),而且通常来自程序编译器。因此,在您的 SQL 中,您为没有引号的字符变量提供了值。

看到这里的区别,没有宏的东西,除了%str

data text;
length text $50;
text=%str(%')bla bla text%str(%');
run;

data text;
length text $50;
text="%str(%')bla bla text%str(%')";
run;

如果您需要在表中引用字符串:

proc sql;
   create table results (viewname char(40), xrc numeric, xmsg char(200));
   insert into results (viewname, xrc, xmsg)
   values ( "%str(%')&viewname%str(%')"
          , &xrc 
          , "%str(%')%superq(xmsg)%str(%')" );
quit;

当然,正如已经说过的,没有人真正擅长宏引用:-)

于 2013-06-27T21:25:51.590 回答