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

SAS For Dummies 3rd Edition! Check out the new edition, covering SAS 9.4, SAS Viya, and all of the modern ways to use SAS!
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.

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