BookmarkSubscribeRSS Feed
ammarhm
Lapis Lazuli | Level 10

This is a similar post to my previous one but with modification to use LIKE with a wild card instead (apology to moderators)

 

a quick question. i have a table (Table A) that contains two columns looking like this

Group ID
A 24
A 54
A 92
A 6
A 7
B 25
B 11
B 97
C 13
C 18
D 81
D 10

 

Then I have a very large table (table B) from which i want to extract data based on the group and ID in a proc sql.

For a number of reasons, I wont be able to use a left join or inner join statement, so I am trying to come up with a different approach that would look like this 

 

 

proc sql,
create table TABLEC as
select *
from tableB
where &selection

the macro variable &selection should be generated dynamically based on tableA and should at the end look like this

 

 

 

(group=A and (compress(ID,,’p’) like '%24%' or compress(ID,,’p’) like '%54%' or compress(ID,,’p’) like '%92%' or compress(ID,,’p’) like '%6%' or compress(ID,,’p’) like '%7%') ) or

(group=B and (compress(ID,,’p’) like '%25%' or compress(ID,,’p’) like '%11%' or compress(ID,,’p’) like '%97%' )) or

(group=C and (compress(ID,,’p’) like '%13%' or compress(ID,,’p’) like '%18%')) or

(group=D and (compress(ID,,’p’) like '%81%' or compress(ID,,’p’) like '%10%' ))

The number and name of groups is variable so the code should account for that too. The only thing that wont change is the name of columns (group, ID).

Has anyone done something similar before?

 

Yes, I know left join makes more sense but as I said, I need to this differently...

Thanks

Am

 

4 REPLIES 4
Astounding
PROC Star

Certainly this would be simplified if you were to concatenate the two fields.  Here is the idea:

 

proc sql;

create table TABLEC as

select * from TABLEB

where cats(group, id) in (select cats(group, id) from tableA);

quit;

 

I might have the syntax slightly wrong here, but the idea should work with a little tweaking.

ammarhm
Lapis Lazuli | Level 10

Thanks @Astounding

This approach will however not work if I am trying to use like %id%, 

Thanks anyhow for your time

RW9
Diamond | Level 26 RW9
Diamond | Level 26

You have a really bad setup there then, no getting away from that point.  Have to use sql, can't use joins, have to bang this nail into the wall can't use hammers - you see where I am going.

 

The simplest way to continue this mess is obviously to generate the code from the base table, though don't be suprised if the whole process falls over every run:

data _null_;
  set have end=last;
  if _n_=1 then call execute('proc sql; create table tablec as select * from tableb where ');
  call execute(cats('(group="',group,'" and index(compress(id,,"p")',id,'))'));
  if last then call execute(';quit;');
run;

This will create the proc sql with one group= for each line in the base dataset.  Note I use index() rather than like to avoid all the nasty % issues, the effect is the same, as is only doing one id per row rather than all in one row.

Again, I strongly advise to rethink your whole process including how you got to this mess in the first place.

ammarhm
Lapis Lazuli | Level 10

Thanks @RW9

I agree, it is not the best situation to be in, yet again, it also good to be in these situations as you always learn something new. I never thought of using index() so thanks. 

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 1500 views
  • 1 like
  • 3 in conversation