If statement issue?

Reply
Super Contributor
Posts: 374

If statement issue?

Hello:

 

I have the codes list below.  I found the codes works fine when I only use either of &classkeeplist or &componentkeeplist.  However when I combine these two together and use them in "if statement",  it doesn't work.   I could see the proc sql generated the two sets of macro list in log.    But I don't understand why it didn't work when I use them in data step.  Please help, thanks.

 

proc sql;

select strip(Class) into : classkeeplist separated by '","'

from test

where Class in ('ANTI-INFECTIVES/MISC ANTIBACT',

'ANTIBACTERIALS/MISC B-LACTAM',);

 

 

select strip(Component) into : componentkeeplist separated by '","'

from test

where Component in ('NOS- INFLUENZA VACCINE UNKNOWN',

'NOS- NICOTINE PATCH UNKNOWN');

quit;

 

%put &classkeeplist;

%put &componentkeeplist;

 

data nosmisc;

set test (where=(max(

prxmatch('/NOS | NOS|-NOS|NOS-|MISC/i',Class),

prxmatch('/NOS | NOS|-NOS|NOS-|MISC/i',Component),

prxmatch('/NOS | NOS|-NOS|NOS-|MISC/i',Product) ) gt 0));

if Class not in ("&classkeeplist") or

Component not in ("&componentkeeplist") then output;

run;

 

Super User
Posts: 8,590

Re: If statement issue?

"Doesn't work" tells us exactly NOTHING. Please supply information like the log when an ERROR/WARNING happens, or where the resulting dataset differs from your expectations.

For testing, supply example data in a data step.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Super User
Posts: 8,634

Re: If statement issue?

Why do it this way in the first place?

data nomisc;
  set want (where=(class not in ('ANTI-INFECTIVES/MISC ANTIBACT','ANTIBACTERIALS/MISC B-LACTAM')
                    or component not in ('NOS-INFLUENZA...','NOS-...'));
  if max(prxmatch...,prxmatch...) gt 0;
run;

There isn't any need as far as I can tell to take all the sub components into a macro list and then use that, as the first sql will return a list of only 

'ANTI-INFECTIVES/MISC ANTIBACT',

'ANTIBACTERIALS/MISC B-LACTAM'

And the second the same on component.  Your not actually doing anything ther that isn't a simple where clause??

 

Super User
Posts: 5,987

Re: If statement issue?

Very likely:  the logic of the final IF statement is wrong.  It should use AND instead of OR.

PROC Star
Posts: 1,925

Re: If statement issue?

Posted in reply to Astounding

Very likely indeed. Combining negations and OR doesn't build immediately-obvious Boolean clauses.

 

if ^A | ^B 

 

is the same (but less legible imho) as 

 

if ^(A & B)

 

Is this really what you want?

 

Also, you may (or not) want to use the \b expression in your where clause to look for word boundaries.  

Ask a Question
Discussion stats
  • 4 replies
  • 194 views
  • 3 likes
  • 5 in conversation