我在 MS SQL Server 中有一个表,如下所示:
ID, Code
01, A
02, A
03, B
04, C
...
并在 SAS 中定义为
LIBNAME MSSQLDB ODBC
CONNECTION=SHAREDREAD
COMPLETE='Description=OIPE DW (Dev);DRIVER=SQL Server Native Client 11.0;SERVER=Amazon;Trusted_Connection=Yes;DATABASE=OIPEDW_Dev;'
SCHEMA='dbo'
PRESERVE_TAB_NAMES=YES
PRESERVE_COL_NAMES=YES;
我有一个 SAS 数据集,它的记录格式与 MSSQLDB(ID 和代码变量)相同,但它只是整个数据库的一个子集。
我想做以下事情:
PROC SQL NOPRINT;
/* If SASDS contains just codes A and B, CodeVar=A B
SELECT DISCTINCT CODE INTO :CodeVar SEPARATED BY ' ' FROM SASDS;
QUIT;
/* seplist is a macro that wraps each code in a quote */
%LET CodeInVar=%seplist( &CodeVar, nest=%STR(") );
PROC SQL;
DELETE * FROM MSSQLDB WHERE CODE IN (&CodeInVar);
/* Should execute DELETE * FROM MSSQL WHERE CODE IN ('A','B');
QUIT;
问题是这会对 &CodeInVar 宏变量中的值产生语法错误。
知道如何在 IN 语句中将宏变量值传递给 SQL Server 吗?