DATA Step, Macro, Functions and more

How to factor the conditions with and or?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 99
Accepted Solution

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
Respected Advisor
Posts: 4,927

Re: How to factor the conditions with and or?

Posted in reply to afiqcjohari

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

View solution in original post


All Replies
Respected Advisor
Posts: 4,927

Re: How to factor the conditions with and or?

[ Edited ]
Posted in reply to afiqcjohari

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
Respected Advisor
Posts: 4,927

Re: How to factor the conditions with and or?

Posted in reply to afiqcjohari

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?
Respected Advisor
Posts: 4,927

Re: How to factor the conditions with and or?

Posted in reply to afiqcjohari

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.

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

Discussion stats
  • 7 replies
  • 143 views
  • 2 likes
  • 2 in conversation