DATA Step, Macro, Functions and more

If statement with and or

Accepted Solution Solved
Reply
Contributor
Posts: 37
Accepted Solution

If statement with and or

Hello,

 

I'm trying the below, which isn't the correct syntax

 

   if Funded_Trips =6 or Funded_Trips =4 then
     if TripsCompleted < IMTarget or  if TripsCompleted < Target  then TargetMet= 'N'
  else 'Y' ;

 

I was hoping someone could give me the correct snytax.

 

Cheers

Haydn


Accepted Solutions
Solution
‎10-20-2016 11:38 PM
Super User
Posts: 17,937

Re: If statement with and or

   if Funded_Trips in (4, 6) 
      and (TripsCompleted < IMTarget or  TripsCompleted < Target ) then TargetMet= 'N';
  else TargetMet='Y' ;

View solution in original post


All Replies
Super User
Posts: 5,096

Re: If statement with and or

Here's a possibility:

 

  if (Funded_Trips =6 or Funded_Trips =4) and (TripsCompleted < IMTarget or TripsCompleted < Target) then TargetMet= 'N';
  else TargetMet='Y' ;

 

But the right answer depends on when the ELSE condition should kick in.  

Solution
‎10-20-2016 11:38 PM
Super User
Posts: 17,937

Re: If statement with and or

   if Funded_Trips in (4, 6) 
      and (TripsCompleted < IMTarget or  TripsCompleted < Target ) then TargetMet= 'N';
  else TargetMet='Y' ;
SAS Super FREQ
Posts: 8,744

Re: If statement with and or

Hi:

  I suggest you activate the free Programming 1 course and review the DATA step language for conditional processing and how IF statements work.

 

  Without data, it is hard to know what you want to do, my tendency is to use AND and then use parentheses to group the OR conditions like this:

 

if (Funded_Trips =6 or Funded_Trips =4) and
   (TripsCompleted < IMTarget or TripsCompleted < Target)  then TargetMet= 'N';
else TargetMet='Y' ;

 

on the other hand, you could want:


if (Funded_Trips =6 or Funded_Trips =4) then do;
   if (TripsCompleted < IMTarget or TripsCompleted < Target) then do;
       TargetMet= 'N';
   end;
end;
else TargetMet='Y' ;  /* any other value for Funded_Trips gets TargetMet = Y */

 

but, as I said, without data, and seeing what you have and what you want, it is hard to speculate. The above codes might or might not generate the desired results.

 

cynthia

Super User
Super User
Posts: 7,430

Re: If statement with and or

Binary choices can be simplified with ifn/ifc functions:

targetmet=ifc(funded_trips in (4,6) and tripscompleted < max(imtarget,target),'N','Y');
Respected Advisor
Posts: 3,777

Re: If statement with and or

@RW9 consider default length.  You would probably want to define the length of "TARGETMET" and not let it default.

 

25         data _null_;
26            x = ifc(1,'Y','N');
27            lx = vlength(x);
28            put _all_;
29            run;

x=Y lx=200 _ERROR_=0 _N_=1
☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 330 views
  • 2 likes
  • 6 in conversation