How do I exclude both EM codes and OP procedures from my data? Are the operators correct?
data HospOPDept;
set OUTPAT;
real_date=input(eventdate,YYMMDD10.);
/*format real_date date9.;*/
format real_date yyq6.; /*year-qtr*/
where rptGrouper not in ("OP Procedures")
or (cpt not between '99201' and '99215')
or (cpt not between '99241' and '99245')
or (cpt not between '99381' and '99397')
or (cpt not between '99354' and '99355')
or (cpt not between '99401' and '99412')
;
run;
Here's what I did and it worked (I created a macro for the codes instead of listing them out and did a "where also" statement to exclude those codes in addition to excluding OP Procedures):
data HospOPDept;
set OUTPAT;
real_date=input(eventdate,YYMMDD10.);
/*format real_date date9.;*/
format real_date yyq6.; /*year-qtr*/
where rptGrouper not in ("OP Procedures");
where also cpt not in &EMCODES;
run;
Guessing:
where rptGrouper not in ("OP Procedures")
AND ((cpt not between '99201' and '99215')
or (cpt not between '99241' and '99245')
or (cpt not between '99381' and '99397')
or (cpt not between '99354' and '99355')
or (cpt not between '99401' and '99412'))
;
I used an "OR" operator but it's not excluding those codes or OP procedures...
Try ANDs:
where rptGrouper not in ("OP Procedures")
and (cpt not between '99201' and '99215')
and (cpt not between '99241' and '99245')
and (cpt not between '99381' and '99397')
and (cpt not between '99354' and '99355')
and (cpt not between '99401' and '99412')
;
It might be a good idea to provide some example data, the basic rules (not code when you don't have code that works) and an example of the output for the example data.
It may be that you need more parentheses for grouping related concepts.
https://brilliant.org/wiki/de-morgans-laws/ may help a bit to work out some details of OR , AND combinations of logic.
This "trick" of converting your values to numeric, assuming all of the values you need to test follow the 5 digit examples, may help simplify the code:
data have; input x $; datalines; 99123 99124 99125 99127 99234 99235 99236 99237 ; data example; set have; where input(x,f5.) not in (99123:99125 99234:99236); run;
The IN operator will accept numeric ranges, indicated by the : between two integers to select integer values and might considerably reduce the amount of code needed. The above code excludes values and is what I think could replace
(cpt not between '99123' and '99125') and (cpt not between '99234' and '99236')
Personally I will go out my way to do any comparisons with text values and ranges as they so seldom work nicely. If every single value has the exact same number of characters then maybe.
Here's what I did and it worked (I created a macro for the codes instead of listing them out and did a "where also" statement to exclude those codes in addition to excluding OP Procedures):
data HospOPDept;
set OUTPAT;
real_date=input(eventdate,YYMMDD10.);
/*format real_date date9.;*/
format real_date yyq6.; /*year-qtr*/
where rptGrouper not in ("OP Procedures");
where also cpt not in &EMCODES;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.