Date: Wed, 29 Sep 2004 16:51:58 -0400
Reply-To: sashole@bellsouth.net
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: "Paul M. Dorfman" <sashole@BELLSOUTH.NET>
Organization: Sashole of Florida
Subject: Re: %IF with IN condition?
In-Reply-To: <200409281635.i8SGZjCW010452@listserv.cc.uga.edu>
Content-Type: text/plain; charset="US-ASCII"
TMK,
The expression
%sysfunc (indexw (&in_list, &srchfor)))
will evaluate SAS-true if the (resolved) second argument is found in the
(resolved) in_list, and SAS-false otherwise. It also cannot be fooled by the
argument being a substring of one of the in_list's tokens. You can make it
into a macro if thou desirest, but since it is so simple on its face, my
vote would be not to bother.
Kind regards,
----------------
Paul M. Dorfman
Jacksonville, FL
----------------
> -----Original Message-----
> From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On
> Behalf Of Talbot Michael Katz
> Sent: Tuesday, September 28, 2004 12:36 PM
> To: SAS-L@LISTSERV.UGA.EDU
> Subject: %IF with IN condition?
>
> Hi gang!
>
> Is it possible to use an IN condition with %IF? I haven't
> found a way to get it to work in SAS 8.2.
>
> Here are some examples:
>
> 1. Regular data step IF with IN:
>
> 701 %let nx = 4 ;
> 702 data _null_ ;
> 703 if &nx. in (4,5) then do ;
> 704 put "Success nx = &nx." ;
> 705 end ;
> 706 else do ;
> 707 put "Failure nx = &nx." ;
> 708 end ;
> 709 run ;
>
> Success nx = 4
> NOTE: DATA statement used:
> real time 0.00 seconds
> cpu time 0.00 seconds
>
>
> 710 %let nx = 6 ;
> 711 data _null_ ;
> 712 if &nx. in (4,5) then do ;
> 713 put "Success nx = &nx." ;
> 714 end ;
> 715 else do ;
> 716 put "Failure nx = &nx." ;
> 717 end ;
> 718 run ;
>
> Failure nx = 6
> NOTE: DATA statement used:
> real time 0.01 seconds
> cpu time 0.01 seconds
>
>
> 2. Same thing with OR
>
> 719 %let nx = 4 ;
> 720 data _null_ ;
> 721 if &nx. = 4 or &nx. = 5 then do ;
> 722 put "Success nx = &nx." ;
> 723 end ;
> 724 else do ;
> 725 put "Failure nx = &nx." ;
> 726 end ;
> 727 run ;
>
> Success nx = 4
> NOTE: DATA statement used:
> real time 0.00 seconds
> cpu time 0.00 seconds
>
>
> 728 %let nx = 6 ;
> 729 data _null_ ;
> 730 if &nx. = 4 or &nx. = 5 then do ;
> 731 put "Success nx = &nx." ;
> 732 end ;
> 733 else do ;
> 734 put "Failure nx = &nx." ;
> 735 end ;
> 736 run ;
>
> Failure nx = 6
> NOTE: DATA statement used:
> real time 0.01 seconds
> cpu time 0.01 seconds
>
>
> 3. MACRO with OR
>
> 737
> 738 %macro sofor(nx) ;
> 739 %if &nx. = 4 or &nx. = 5 %then %do ;
> 740 %put Success nx = &nx. ;
> 741 %end ;
> 742 %else %do ;
> 743 %put Failure nx = &nx. ;
> 744 %end ;
> 745 %mend sofor ;
> 746
> 747 %sofor(4) ;
> Success nx = 4
> 748
> 749
> 750 %sofor(6) ;
> Failure nx = 6
>
>
> 4. MACRO with IN
>
> 751
> 752 %macro sofin(nx) ;
> 753 %if &nx. in (4,5) %then %do ;
> 754 %put Success nx = &nx. ;
> 755 %end ;
> 756 %else %do ;
> 757 %put Failure nx = &nx. ;
> 758 %end ;
> 759 %mend sofin ;
> 760
> 761 %sofin(4) ;
> ERROR: Required operator not found in expression: &nx. in (4,5)
> ERROR: The macro SOFIN will stop executing.
> 762
> 763
> 764 %sofin(6) ;
> ERROR: Required operator not found in expression: &nx. in (4,5)
> ERROR: The macro SOFIN will stop executing.
>
>
> I tried to use sysexec and sysevalf with the IN condition in
> the %IF statement to see if I could coax it to work, but
> haven't had any success.
> Is there a way to do it?
>
> Thanks!
>
> -- TMK --
>
|