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

I wrote the following hard coded CASE statment in in PROC SQL:

/*Notes is the memo field */

MAX(CASE WHEN FIND (Notes,  ' burn', "i")>0

/*exclusion for valid return */
AND FIND(Notes, 'burner', "i")=0
AND FIND(Notes, 'burn off', "i")=0
AND FIND(Notes, 'burnt', "i")=0
AND FIND(Notes, 'unit rely', "i")=0
AND FIND(Notes, 'burney', "i")=0
AND FIND(Notes, 'burnage', "i")=0
AND FIND(Notes, 'nnon bur', "i")=0
AND FIND(Notes, 'burnssd', "i")=0
AND FIND(Notes, 'wave', "i")=0
AND FIND(Notes, 'burns/', "i")=0
AND FIND(Notes, 'burn mark', "i")=0
AND Notes not like '%burn%cloth%'
THEN 'BURN' ELSE END)

I need to traslate the code to use terms stored in a table like the one below, I know it requires LOOPING of some sort, but not sure how to implement.  Thank you in advnaced

SAFETY TERMEXCLUSIONSSAFETY_BUCKET
BURNNBURNS
DAMAGNDAMAGE
BURNERYBURNS
BURNT OFFYBURNS
UNIT RELYYBURNS
NONN BURYBURNS
SHOULD LOOK LIKEYDAMAGE
1 ACCEPTED SOLUTION

Accepted Solutions
TomKari
Onyx | Level 15

Here's an option. Note that I had to modify your first "where" clause.

Tom

data _null_;
length SQLClause $2048;
retain SQLClause;
set terms end=LastRec;
if _n_ = 1
then SQLClause = "FIND(Notes,  ' burn', 'i')>0 /*exclusion for valid return */ AND FIND(Notes, '"||strip(term)||"', 'i')=0";
else SQLClause = catx(" ", SQLClause, "AND FIND(Notes, '"||strip(term)||"', 'i')=0");
if LastRec then
call symput("SQLClause", SQLClause);
run;

proc sql;
create table Want as
select * from text
where &SQLClause.;
quit;


View solution in original post

3 REPLIES 3
TomKari
Onyx | Level 15

Here's an option. Note that I had to modify your first "where" clause.

Tom

data _null_;
length SQLClause $2048;
retain SQLClause;
set terms end=LastRec;
if _n_ = 1
then SQLClause = "FIND(Notes,  ' burn', 'i')>0 /*exclusion for valid return */ AND FIND(Notes, '"||strip(term)||"', 'i')=0";
else SQLClause = catx(" ", SQLClause, "AND FIND(Notes, '"||strip(term)||"', 'i')=0");
if LastRec then
call symput("SQLClause", SQLClause);
run;

proc sql;
create table Want as
select * from text
where &SQLClause.;
quit;


ChrisHemedinger
Community Manager

I think Tom's approach helps to generalize your match/mapping game, and maybe that's enough for you.

Whenever I see the term "loop" with "SQL", I think "JOIN".  Because really, you want to iterate on your records that match a certain pattern and then output a new record (or set of records) that map into some new values.  If you can capture all variations of the pattern or express as a LIKE clause, then you can create a second table of those "starting values" and what they should map to, then design a JOIN to do the work.

Chris

It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.
air_dedman
Calcite | Level 5

Yes I do plan to write a join, I will try TOM's approach. Basically I want the search to include certain string ' burn', '  scortch', ' smell', which is a series of OR or IN statements, but also exclude strings which can only be done using AND statements in SQL. I'm using SQL mostly becuase the DBA's hear wanted the code to be portable, and they only know SQL. If this is not the best approach for a SAS app, I will just tell them I have to code it in SAS.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

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.

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