I'm having troubles pulling rows from a dataset. They need to match a row in another dataset. Here's an example
DATA1
TYPE1 | TYPE2 | VALUE1 | VALUE2 |
---|---|---|---|
HAIR | EYES | BROWN | BLUE |
SEX | EYES | MALE | BROWN |
HAIR | SEX | BLONDE | FEMALE |
SEX | EYES | MALE | BLUE |
DATA2
NAME | HAIR | SEX | EYES |
---|---|---|---|
ROGER | BROWN | MALE | BLUE |
GLEN | BLONDE | MALE | HAZEL |
JULIE | BLONDE | FEMALE | BROWN |
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!
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;
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
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;
Perfect. Scalable for additional data and variables. Exactly what I was looking for. Thanks Tom.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.