BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
NewUsrStat
Pyrite | Level 9

Hi guys, 

suppose to have the following dataset: 

 

data DB1;
input ID :$20. Admission :date09. Discharge :date09. Var1  Var2 Var3;
cards;
8   10NOV2014 15NOV2014   44022 25000 4019
8   18SEP2015 19SEP2015   4552
8   03APR2017 05APR2017   85201 5761 57450
170 27JAN2020 31JAN2020   0039  5849 42731
170 06DEC2016 14DEC2016   4280  5119 40211
170 14MAY2015 21MAY2015   4280  49120
166 19AUG2016 24AUG2016   035   4019
166 27APR2017 16MAY2017   81221 V1588
166 20JAN2018 28JAN2018   72665
;
run;

Then another dataset with a list: 

data DB2;
input MyVariable;
cards;
440
455
852
428
726
run;

I would like to subset from DB1 only rows where Var* match the first 3 integers of  MyVariable in DB2 to get the following: 

 

data DB3;
input ID :$20. Admission :date09. Discharge :date09. Var1  Var2 Var3;
cards;
8   10NOV2014 15NOV2014   44022 25000 4019
8   18SEP2015 19SEP2015   4552
8   03APR2017 05APR2017   85201 5761 57450
170 06DEC2016 14DEC2016   4280  5119 40211
170 14MAY2015 21MAY2015   4280  49120
166 20JAN2018 28JAN2018   72665
;
run;

Can anyone help me please? 

 

Thank you in advance

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data DB1;
infile cards truncover;
input ID :$20. Admission :date09. Discharge :date09. (Var1  Var2 Var3) ($);
format Admission Discharge date09.;
cards;
8   10NOV2014 15NOV2014   44022 25000 4019
8   18SEP2015 19SEP2015   4552
8   03APR2017 05APR2017   85201 5761 57450
170 27JAN2020 31JAN2020   0039  5849 42731
170 06DEC2016 14DEC2016   4280  5119 40211
170 14MAY2015 21MAY2015   4280  49120
166 19AUG2016 24AUG2016   035   4019
166 27APR2017 16MAY2017   81221 V1588
166 20JAN2018 28JAN2018   72665
;
run;


data DB2;
input MyVariable $;
cards;
440
455
852
428
726
;

proc sql noprint;
select quote(strip(MyVariable)) into :list separated by ' ' from DB2;
quit;
data want;
 set DB1;
 array v{*} var:;
 do i=1 to dim(v);
  if v{i} in: (&list.) then do;output;leave;end;
 end;
 drop i;
run;

View solution in original post

1 REPLY 1
Ksharp
Super User
data DB1;
infile cards truncover;
input ID :$20. Admission :date09. Discharge :date09. (Var1  Var2 Var3) ($);
format Admission Discharge date09.;
cards;
8   10NOV2014 15NOV2014   44022 25000 4019
8   18SEP2015 19SEP2015   4552
8   03APR2017 05APR2017   85201 5761 57450
170 27JAN2020 31JAN2020   0039  5849 42731
170 06DEC2016 14DEC2016   4280  5119 40211
170 14MAY2015 21MAY2015   4280  49120
166 19AUG2016 24AUG2016   035   4019
166 27APR2017 16MAY2017   81221 V1588
166 20JAN2018 28JAN2018   72665
;
run;


data DB2;
input MyVariable $;
cards;
440
455
852
428
726
;

proc sql noprint;
select quote(strip(MyVariable)) into :list separated by ' ' from DB2;
quit;
data want;
 set DB1;
 array v{*} var:;
 do i=1 to dim(v);
  if v{i} in: (&list.) then do;output;leave;end;
 end;
 drop i;
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 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 1 reply
  • 125 views
  • 1 like
  • 2 in conversation