BookmarkSubscribeRSS Feed
r3570
Obsidian | Level 7

Hi All,

 

I have the below logic and confused on how to include 'AND 'and 'OR' within same if statement. 

 

If
((HAT = 'Yes' Or SAT = 'Yes' Or TBP = 'Yes') Or (TBI = 'Yes' And REC = 'Yes') Or COT = Yes) And
RESULT = 'Pos' And Does not satisfy (CAT = 'XCL' and TEST = 27) then output

 

The above variables with given values should satisfy so that i get the desired output from the given raw data.

 

Any help with how to write the program to satisfy the logic?

5 REPLIES 5
PeterClemmensen
Tourmaline | Level 20

Questions like this is much easier to answer if you provide a minimum of sample data and your desired result.

r3570
Obsidian | Level 7

Thanks for your reply  @PeterClemmensen 

 

Please consider the below data as the raw data. Consider the first column as ID and rest are the variables in order HAT SAT TBP TBI REC COT RESULT CAT TEST with corresponding values

 

202359 No   No  No No           Yes Pos XCL 26.0
202360 Yes Yes Yes Yes Yes Yes Pos XCL 27.0
202360 Yes Yes Yes Yes Yes Yes Pos ICL  27.0

 

My output should be the below record not the rest.

202359 No   No  No No           Yes Pos XCL 26.0

 

mkeintz
PROC Star

@r3570 wrote:

 

Please consider the below data as the raw data. Consider the first column as ID and rest are the variables in order HAT SAT TBP TBI REC COT RESULT CAT TEST with corresponding values

 

202359 No   No  No No           Yes Pos XCL 26.0
202360 Yes Yes Yes Yes Yes Yes Pos XCL 27.0
202360 Yes Yes Yes Yes Yes Yes Pos ICL  27.0

 

My output should be the below record not the rest.

202359 No   No  No No           Yes Pos XCL 26.0

 


In your first post, your criteria included

And Does not satisfy (CAT = 'XCL' and TEST = 27)

which, as @ballardw said, would be expressed as

   NOT (CAT = 'XCL' and TEST = 27)

But that would not produce a single output as you want.  There would be 2 output observations from this condition.  So what do you mean by

And Does not satisfy (CAT = 'XCL' and TEST = 27)

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Amir
PROC Star

Hi,

 

If I've understood your requirements, for the record you want I have created the following code. Is this what you're looking for?

 

data have;
  input
    @01 id     $char6.
    @08 HAT    $char3.
    @12 SAT    $char3.
    @16 TBP    $char3.
    @20 TBI    $char3.
    @24 REC    $char3.
    @28 COT    $char3.
    @32 RESULT $char3.
    @36 CAT    $char3.
    @40 TEST        8.
  ;
  
  datalines;
202359 No  No  No  No      Yes Pos XCL 26.0
202360 Yes Yes Yes Yes Yes Yes Pos XCL 27.0
202360 Yes Yes Yes Yes Yes Yes Pos ICL 27.0
;


data want;
  set have;

  if (
         (HAT = 'Yes' or SAT = 'Yes' or TBP = 'Yes')
      or (TBI = 'Yes' and REC = 'Yes')
      or COT = 'Yes'
     )
     and RESULT = 'Pos'
     and CAT = 'XCL'
     and TEST = 26
  ;
run;

 

 

 

Thanks & kind regards,

Amir.

ballardw
Super User

"And Does not satisfy (CAT = 'XCL' and TEST = 27)"

would be

 

And Not (CAT = 'XCL' and TEST = 27) )

 

NOT is an operator to negate the result of a logical operation. If (CAT = 'XCL' and TEST = 27) is "true" then not(CAT = 'XCL' and TEST = 27) is "false".

 

 

With out know the expected result for some given values it is hard to tell if the rest of your logic is correct or not.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 499 views
  • 0 likes
  • 5 in conversation