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 TERM | EXCLUSIONS | SAFETY_BUCKET |
---|---|---|
BURN | N | BURNS |
DAMAG | N | DAMAGE |
BURNER | Y | BURNS |
BURNT OFF | Y | BURNS |
UNIT RELY | Y | BURNS |
NONN BUR | Y | BURNS |
SHOULD LOOK LIKE | Y | DAMAGE |
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;
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;
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
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 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.