BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
pk10
Calcite | Level 5

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)

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
CODE NOT TESTED.

proc sql;
create table want as
select * from A
where name not in
( select LN from B );
quit;

View solution in original post

4 REPLIES 4
japelin
Rhodochrosite | Level 12

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.

 

ChrisNZ
Tourmaline | Level 20

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.

 

Ksharp
Super User
CODE NOT TESTED.

proc sql;
create table want as
select * from A
where name not in
( select LN from B );
quit;
Kurt_Bremser
Super User

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 578 views
  • 0 likes
  • 5 in conversation