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: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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