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

I'm having troubles pulling rows from a dataset. They need to match a row in another dataset. Here's an example

DATA1

TYPE1TYPE2VALUE1VALUE2
HAIREYESBROWNBLUE
SEXEYESMALEBROWN
HAIRSEXBLONDEFEMALE
SEXEYESMALEBLUE

DATA2

NAMEHAIRSEXEYES
ROGERBROWNMALEBLUE
GLENBLONDEMALEHAZEL
JULIEBLONDEFEMALEBROWN

I want it to pull records in data2 based on matching the criteria in data1. The value in data1 will exactly match the column name in data2.

So the result would be:

ROGER

JULIE

Because their attributes match those laid out in data1.

I've spent a lot of time trying different approaches. Hopefully you guys can help.

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Use the first dataset to generate a WHERE statement.  I find it is easiest to do this with a datat step and the PUT statement.

data criteria ;

  length type1 $32 value1 $20 type2 $32 value2 $20 ;

  input TYPE1 TYPE2 VALUE1 VALUE2 ;

cards;

HAIR EYES BROWN BLUE

SEX EYES MALE BROWN

HAIR SEX BLONDE FEMALE

SEX EYES MALE BLUE

run;

data records ;

  length NAME HAIR SEX  EYES $20 ;

  input name hair sex eyes ;

cards;

ROGER BROWN MALE BLUE

GLEN BLONDE MALE HAZEL

JULIE BLONDE FEMALE BROWN

run;

filename code temp;

data _null_;

  file code;

  if eof then put ';' ;

  set criteria end=eof;

  if _n_ = 1 then put 'WHERE';

  else put ' OR' ;

  put '(' type1 '= ' value1 :$quote. 'and ' type2 '= ' value2 :$quote. ')';

run;

data want ;

  set records ;

%inc code / source2 ;

run;

View solution in original post

3 REPLIES 3
PGStats
Opal | Level 21

A bit tricky indeed...


data types;
array t{3} $ HAIR SEX EYES;
set data1;
t(findw("HAIR SEX EYES", trim(TYPE1), " ", "E")) = VALUE1;
t(findw("HAIR SEX EYES", trim(TYPE2) ," ", "E")) = VALUE2;
run;

proc sql;
select distinct d.name
from data2 as d, types as t
where sum(d.HAIR=t.HAIR, d.SEX=t.SEX, d.EYES=t.EYES) = 2;
quit;

PG

PG
Tom
Super User Tom
Super User

Use the first dataset to generate a WHERE statement.  I find it is easiest to do this with a datat step and the PUT statement.

data criteria ;

  length type1 $32 value1 $20 type2 $32 value2 $20 ;

  input TYPE1 TYPE2 VALUE1 VALUE2 ;

cards;

HAIR EYES BROWN BLUE

SEX EYES MALE BROWN

HAIR SEX BLONDE FEMALE

SEX EYES MALE BLUE

run;

data records ;

  length NAME HAIR SEX  EYES $20 ;

  input name hair sex eyes ;

cards;

ROGER BROWN MALE BLUE

GLEN BLONDE MALE HAZEL

JULIE BLONDE FEMALE BROWN

run;

filename code temp;

data _null_;

  file code;

  if eof then put ';' ;

  set criteria end=eof;

  if _n_ = 1 then put 'WHERE';

  else put ' OR' ;

  put '(' type1 '= ' value1 :$quote. 'and ' type2 '= ' value2 :$quote. ')';

run;

data want ;

  set records ;

%inc code / source2 ;

run;

atang
Calcite | Level 5

Perfect. Scalable for additional data and variables. Exactly what I was looking for. Thanks Tom.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 3 replies
  • 1335 views
  • 3 likes
  • 3 in conversation