Desktop productivity for business analysts and programmers

Looping AND/OR statements in PROC SQL

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

Looping AND/OR statements in PROC SQL

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

Accepted Solutions
Solution
‎10-07-2014 12:30 PM
Trusted Advisor
Posts: 1,051

Re: Looping AND/OR statements in PROC SQL

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


All Replies
Solution
‎10-07-2014 12:30 PM
Trusted Advisor
Posts: 1,051

Re: Looping AND/OR statements in PROC SQL

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;


Community Manager
Posts: 2,692

Re: Looping AND/OR statements in PROC SQL

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

Occasional Contributor
Posts: 7

Re: Looping AND/OR statements in PROC SQL

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.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 300 views
  • 3 likes
  • 3 in conversation