Hello
In proc sql i want to use in operator.
What is the correct way?
If there are some items ,should I use comma between them or also a space can work?
I run this example and both method worked
Data tbl;
input ID branch;
cards;
1 100
2 100
3 200
4 300
5 400
6 500
7 500
8 500
9 500
10 500
;
run;
PROC SQL;
create table outcome as
select *
from tbl
where branch in (100, 200, 300, 400, 500)
;
QUIT;
PROC SQL;
create table outcome as
select *
from tbl
where branch in (100 200 300 400 500)
;
QUIT;
Both methods are equal. I personally prefer commas in "in"-lists because I rarely use SQL and commas are needed in the when() conditions of select() blocks in data steps, where the in-lists often end up.
Both methods are equal. I personally prefer commas in "in"-lists because I rarely use SQL and commas are needed in the when() conditions of select() blocks in data steps, where the in-lists often end up.
They generate identical results, but IMO it is bad programming practice to omit the commas.
That is one of the nicest features of SAS code, especially if you need to create macros that take in lists of values as parameters.
Commas are a pain to pass into macro calls because they are used to separate parameters.
%macro subset(in,out,var,list);
data &out;
set ∈
where &var in (&list);
run;
%mend subset;
proc sql noprint;
select quote(trim(name)) into :mylist separated by ' '
from sashelp.class
where sex='F'
;
quit;
%subset(in=sashelp.class,out=females,var=name,list=&mylist);
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.