In SAS can we use IF statement which validates condition from two datasets.
For eg. Dataset 'A' has Name, Age, Gender as variable and Dataset 'B' has LN variable.
I want to compare two datasets and delete all rows IF Names(from Dataset A) = LN (from dataset B)
I think it's easy to merge using the in= dataset option.
/* sample datasets */
data a;
set sashelp.class(keep=name age sex);
rename sex=gender;
run;
data b;
set sashelp.class(keep=name);
rename name=ln;
if _n_<10;
run;
/* merge with "in= dataset option" */
proc sort data=a;
by name;
run;
proc sort data=b out=b(rename=(ln=name));/* need to rename as merge key */
by ln;
run;
data c;
merge a(in=inA) b(in=inB);
if not(inA and inB);/* same as "if inA and inB then delete;" */
run;
Or maybe you can use proc sql.
Like this?
data WANT;
merge HAVE1 HAVE2;
by KEY;
if NAME=LN;
run;
Also, don't forget the account for when a variable is missing. For this use the IN= data set option.
Today, the proper tool for this is a hash object:
data want;
set a;
if _n_ = 1
then do;
declare hash b (dataset:"b (rename=(ln=name))");
b.definekey("name");
b.definedone();
end;
if b.check() ne 0;
run;
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.