How to factor the conditions with and or?

Solved
Frequent Contributor
Posts: 99

How to factor the conditions with and or?

```data want;
set have;
where dTran >= '01JAN2017'd
and dTran <= '31JAN2017'd
and cPOSMode in ('07')
or (dTran >= '01JAN2017'd
and dTran <= '31JAN2017'd
and cPOSMode in ('90','02')
and cToken is not null
and cPvder is not null
and idTReq is not null);
run;```

Is it possible to simplify the conditions by factoring out the dates?

Accepted Solutions
Solution
‎03-17-2017 03:32 AM
Posts: 5,543

Re: How to factor the conditions with and or?

Maybe

``````data want;
set have;
where dTran between '01JAN2017'd and '31JAN2017'd
and ( cPOSMode in ('07')
or (cPOSMode in ('90','02')
and cmiss(cToken, cPvder, idTReq) = 0) );
run;

/* Or */

data want;
set have;
where year(dTran) = 2017
and ( cPOSMode in ('07')
or (cPOSMode in ('90','02')
and cmiss(cToken, cPvder, idTReq) = 0) );
run;``````
PG

All Replies
Posts: 5,543

Re: How to factor the conditions with and or?

[ Edited ]

Logical algebra:

A and B or A and C is equivalent to A and (B or C)

PG
Frequent Contributor
Posts: 99

Re: How to factor the conditions with and or?

```data want;
set have;
where dTran >= '01JAN2017'd and dTran <= '31JAN2017'd
and (cPOSMode in ('07')
or (cPOSMode in ('90','02')
and cToken is not null
and cPvder is not null
and idTReq is not null));
run;```

Hurm not sure whether it looks more readable.

Solution
‎03-17-2017 03:32 AM
Posts: 5,543

Re: How to factor the conditions with and or?

Maybe

``````data want;
set have;
where dTran between '01JAN2017'd and '31JAN2017'd
and ( cPOSMode in ('07')
or (cPOSMode in ('90','02')
and cmiss(cToken, cPvder, idTReq) = 0) );
run;

/* Or */

data want;
set have;
where year(dTran) = 2017
and ( cPOSMode in ('07')
or (cPOSMode in ('90','02')
and cmiss(cToken, cPvder, idTReq) = 0) );
run;``````
PG
Frequent Contributor
Posts: 99

Re: How to factor the conditions with and or?

That cmiss function does help! Thank you PG!
Frequent Contributor
Posts: 99

Re: How to factor the conditions with and or?

cmiss routine not found. Is it not part of SAS base or something?
Posts: 5,543

Re: How to factor the conditions with and or?

Looking at historical doc, cmiss was introduced in base SAS in version 9.2

In what context and with what version of SAS are you calling cmiss?

PG
Frequent Contributor
Posts: 99

Re: How to factor the conditions with and or?

I just figured out that the server is running a 9.1 version while my desktop is on 9.3

This explains why I can run the function previously.
☑ This topic is solved.