如何在proc SQL中的from表中使用宏变量

时间:2012-10-09 05:02:21

标签: sql variables macros

我需要在SQL下面使用一个宏变量通过下面的代码但是继续出错并且我不知道如何修复它!

%let mon1 = 201209;
proc sql;
/*Connection String*/
connect to odbc as sqldata (noprompt="uid=dr;pwd=raven;dsn=FinanceDW;") ;
create table output1 as /*This will create a SAS data set*/
 select * /*this will select all from the command below and insert into the SAS         dataset*/
  from connection to sqldata
     ( /*Insert SQL CODE below - it can only use SQL any SAS code will cause it to fail*/
select top 10 *     
from "AUS_&mon1._FCM15.dbo.contract"

   );/*End SQL code*/

disconnect from sqldata;/*Close connection*/
quit;

然后我得到以下错误(从日志中提取):

23          select top 10 *     
24          from "AUS_&mon1._FCM15.dbo.contract"
25         
26                );
ERROR: CLI describe error: [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name 
       'AUS_201209_FCM15.dbo.contract'. : [Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) 
       could not be prepared.

我需要从中获取数据的实际表格称为AUS_201209_FCM15,所以我不知道问题是什么?

1 个答案:

答案 0 :(得分:0)

想出来。需要使用%bquote并删除""

%let mon1 = 201209; 
proc sql; 
connect to odbc as sqldata (noprompt="uid=dr;pwd=raven;dsn=FinanceDW;") ; 
create table output1 as 
select * 
from connection to sqldata     
select top 10 *      
from %bquote(AUS_&mon1._FCM15.dbo.contract)
);
disconnect from sqldata
quit;