将数据集变量值用作SAS中的IN子句值

将数据集变量值用作SAS中的IN子句值,sas,Sas,我有一个数据集,其中存储了要在其他地方的in子句中使用的所有值 DATA INVALUES; INPUT INVAL; DATALINES; 1 2 3 ; RUN; 我必须使用另一个数据集中的无效值,如下所示 DATA OUTPUT; SET INPUT; IF A IN ( --- INVAL values from dataset INVALUES ----); ; RUN; 这可以通过任何方式实现吗?您可以使用宏变量来实现 /* Put the inval values in a

我有一个数据集,其中存储了要在其他地方的in子句中使用的所有值

DATA INVALUES;
INPUT INVAL;
DATALINES;
1
2
3
;
RUN;
我必须使用另一个数据集中的无效值,如下所示

DATA OUTPUT;
SET INPUT;
IF A IN ( --- INVAL  values from dataset INVALUES ----);
;
RUN;

这可以通过任何方式实现吗?

您可以使用宏变量来实现

/* Put the inval values in a comma separated macro variable  */
proc sql;
select inval into:inval separated by ","
from invalues;

/* prints the macro variable in the log */
%put &inval; /* 1,2,3 */

/* use the macrovariable in the IF statement */
DATA OUTPUT;
SET INPUT;
IF A IN &inval;
RUN;

可以为此使用宏变量

/* Put the inval values in a comma separated macro variable  */
proc sql;
select inval into:inval separated by ","
from invalues;

/* prints the macro variable in the log */
%put &inval; /* 1,2,3 */

/* use the macrovariable in the IF statement */
DATA OUTPUT;
SET INPUT;
IF A IN &inval;
RUN;

更简洁的方法是在SQL中使用子查询,不需要担心数据类型/引用

proc sql ; create table output as select * from input where a in(select distinct inval from invalues) ; quit ;
更简洁的方法是在SQL中使用子查询,不需要担心数据类型/引用

proc sql ; create table output as select * from input where a in(select distinct inval from invalues) ; quit ;
您好,非常感谢您的回复。。。如果in子句的值是字符,如何将它们括在单引号“”中。请告知。没问题。请尝试在sql步骤中用“,”分隔,在if语句中用“&inval.”分隔。非常感谢您的回复。。。如果in子句的值是字符,如何将它们括在单引号“”中。请告知。没问题。尝试在sql步骤中用“,”分隔,在if语句中用“&inval.”分隔