LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (March 2005, week 1)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Thu, 3 Mar 2005 15:13:16 +0000
Reply-To:     toby dunn <tobydunn@HOTMAIL.COM>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         toby dunn <tobydunn@HOTMAIL.COM>
Subject:      Re: Tricky conditional execution of do block
Comments: To: murff@byu.edu
In-Reply-To:  <MC3-F27ii2dnft1vgvo0006e1df@mc3-f27.hotmail.com>
Content-Type: text/plain; format=flowed

Mike,

Glad I could help, but I wouldn't be too embarressed I am the strike out king as Ron puts, but the more mistakes we make the more we learn. SAS-L the place where the SAS giants walk and the best SAS school around....

Toby Dunn

From: "Michael Murff" <mjm33@msm1.byu.edu> Reply-To: <murff@byu.edu> To: "'toby dunn'" <tobydunn@hotmail.com> CC: <SAS-L@LISTSERV.UGA.EDU> Subject: RE: Tricky conditional execution of do block Date: Thu, 3 Mar 2005 08:06:55 -0700

Okay, now I see. I have used Boolean logic before but I always put a space such as not (var ge 100), which evaluates to one if true. The proximity of the () was a style feature I've never seen. I could be embarrassed by my mistake (again) but I'm having too much fun learning from you guys.

Thanks for the clarification, Toby.

Michael Murff

-----Original Message----- From: toby dunn [mailto:tobydunn@hotmail.com] Sent: Thursday, March 03, 2005 7:15 AM To: mjm33@MSM1.BYU.EDU Subject: Re: Tricky conditional execution of do block

Mike,

the use of not() below is not a function but a operator, which is used in comparisions. I found this in the online docs thought you might be interested.

Logical (Boolean) Operators and Expressions

Logical operators, also called Boolean operators, are usually used in expressions to link sequences of comparisons. The logical operators are shown in the following table:

Logical Operators Symbol Mnemonic Equivalent Example & AND (a>b & c>d) | OR (table note 1) (a>b or c>d) ! OR

¦ OR

¬ NOT (table note 2) not(a>b) ˆ NOT

~ NOT

TABLE NOTE 1: The symbol you use for OR depends on your operating environment.

TABLE NOTE 2: The symbol you use for NOT depends on your operating environment.

See Order of Evaluation in Compound Expressions for the order in which SAS evaluates these operators.

In addition, a numeric expression without any logical operators can serve as

a Boolean expression. For an example of Boolean numeric expressions, see Boolean Numeric Expressions.

---------------------------------------------------------------------------- ----

The AND Operator

If both of the quantities linked by AND are 1 (true), then the result of the

AND operation is 1; otherwise, the result is 0. For example, in the following comparison:

a<b & c>0 the result is true (has a value of 1) only when both A<B and C>0 are 1 (true): that is, when A is less than B and C is positive.

Two comparisons with a common variable linked by AND can be condensed with an implied AND. For example, the following two subsetting IF statements produce the same result:

if 16<=age and age<=65;

if 16<=age<=65;

---------------------------------------------------------------------------- ----

The OR Operator

If either of the quantities linked by an OR is 1 (true), then the result of the OR operation is 1 (true); otherwise, the OR operation produces a 0. For example, consider the following comparison:

a<b|c>0 The result is true (with a value of 1) when A<B is 1 (true) regardless of the value of C. It is also true when the value of C>0 is 1 (true), regardless of the values of A and B. Therefore, it is true when either or both of those relationships hold.

Be careful when using the OR operator with a series of comparisons (in an IF, SELECT, or WHERE statement, for instance). Remember that only one comparison in a series of OR comparisons must be true to make a condition true, and any nonzero, nonmissing constant is always evaluated as true (see Boolean Numeric Expressions). Therefore, the following subsetting IF statement is always true:

if x=1 or 2; SAS first evaluates X=1, and the result can be either true or false; however, since the 2 is evaluated as nonzero and nonmissing (true), the entire expression is true. In this statement, however, the condition is not necessarily true because either comparison can evaluate as true or false:

if x=1 or x=2;

---------------------------------------------------------------------------- ----

The NOT Operator

The prefix operator NOT is also a logical operator. The result of putting NOT in front of a quantity whose value is 0 (false) is 1 (true). That is, the result of negating a false statement is 1 (true). For example, if X=Y is

0 (false) then NOT(X=Y) is 1 (true). The result of NOT in front of a quantity whose value is missing is also 1 (true). The result of NOT in front

of a quantity with a nonzero, nonmissing value is 0 (false). That is, the result of negating a true statement is 0 (false).

For example, the following two expressions are equivalent:

not(name='SMITH')

name ne 'SMITH'

Furthermore, NOT(A&B) is equivalent to NOT A|NOT B, and NOT(A|B) is the same

as NOT A & NOT B. For example, the following two expressions are equivalent:

not(a=b & c>d)

a ne b | c le d

---------------------------------------------------------------------------- ----

Boolean Numeric Expressions

In computing terms, a value of true is a 1 and a value of false is a 0. In SAS, any numeric value other than 0 or missing is true, and a value of 0 or missing is false. Therefore, a numeric variable or expression can stand alone in a condition. If its value is a number other than 0 or missing, the condition is true; if its value is 0 or missing, the condition is false.

0 | . = False 1 = True

For example, suppose that you want to fill in variable REMARKS depending on whether the value of COST is present for a given observation. You can write the IF-THEN statement as follows:

if cost then remarks='Ready to budget'; This statement is equivalent to:

if cost ne . and cost ne 0 then remarks='Ready to budget'; A numeric expression can be simply a numeric constant, as follows:

if 5 then do; The numeric value that is returned by a function is also a valid numeric expression:

if index(address,'Avenue') then do;

Toby Dunn

From: Michael Murff <mjm33@MSM1.BYU.EDU> Reply-To: Michael Murff <mjm33@MSM1.BYU.EDU> To: SAS-L@LISTSERV.UGA.EDU Subject: Re: Tricky conditional execution of do block Date: Wed, 2 Mar 2005 23:33:52 -0700 MIME-Version: 1.0 Received: from malibu.cc.uga.edu ([128.192.1.103]) by MC6-F36.hotmail.com with Microsoft SMTPSVC(6.0.3790.211); Wed, 2 Mar 2005 22:34:20 -0800 Received: from listserv.cc.uga.edu (128.192.1.75) by malibu.cc.uga.edu (LSMTP for Windows NT v1.1b) with SMTP id <0.00E8D403@malibu.cc.uga.edu>; Thu, 3 Mar 2005 1:34:20 -0500 Received: from LISTSERV.UGA.EDU by LISTSERV.UGA.EDU (LISTSERV-TCP/IP release

1.8d) with spool id 394629 for SAS-L@LISTSERV.UGA.EDU; Thu, 3 Mar

2005 01:34:21 -0500 Received: from Msm1.byu.edu (msm1.byu.edu [128.187.144.245]) by listserv.cc.uga.edu (8.12.11/8.12.11) with ESMTP id j236YKPP030528 for <SAS-L@LISTSERV.UGA.EDU>; Thu, 3 Mar 2005 01:34:20 -0500 Received: from msm-MTA by Msm1.byu.edu with Novell_GroupWise; Wed, 02 Mar 2005 23:34:20 -0700 X-Message-Info: ZLIeWzn802Qy/ZeVrPvEPfLhx8CMfIbB+rElzvwhsgY= X-Mailer: Novell GroupWise Internet Agent 6.5.1 Newsgroups: bit.listserv.sas-l Comments: To: nospam@HOWLES.COM Return-Path: owner-sas-l@LISTSERV.UGA.EDU X-OriginalArrivalTime: 03 Mar 2005 06:34:20.0446 (UTC) FILETIME=[080ECBE0:01C51FBB]

Howard et al.,

My second primer in retain in as many days (thanks Dennis). I have to say that this one took a few hours to sink in. I had never seen the likes of the not() function (in SAS) and coundnt' find it in the official online SAS docs(9.1.3), nor was it in Cody's functions text. I have to say that I laughed out loud when I tested the following code (trivial extension).

data _null_; x=1; y=0; if not not(x) then put "x=" x; if not(y) then put "y=" y; run;

Howard's trick will do the trick for this "tricky" post.

All the best,

Michael Murff

PS--I googled for "SAS boolean functions" and came up blank. Are there others of the same ilk as not() e.g. boolean and / or (no pun intended :-) undocumented? My guess is that this function may have been documented in an earlier version of SAS.

>>> "Howard Schreier <hs AT dc-sug DOT org>" <nospam@HOWLES.COM> 03/02/05 3:11 PM >>> Since Michael's data set is sorted on VAR, the extra step and extra pass are avoidable.

Try this:

data test; input var @@; cards; 5 26 26 27 28 ;

data _null_; set test; retain found252627 0; if var in (25,26,27) and not(found252627) then found252627 = var; if var=found252627 then put _all_; run;

I get:

var=26 found252627=26 _ERROR_=0 _N_=2 var=26 found252627=26 _ERROR_=0 _N_=3

On Wed, 2 Mar 2005 09:31:56 -0800, Dennis Diskin <diskin@SNET.NET> wrote:

>Michael, > >There are a number of ways to do this, but I prefer: > >proc sql noprint; >select min(var) into :minvar from yourdata where 25 <= var <= 27; >Quit; > >And then use &minvar in your if statement. > >HTH, >Dennis Diskin > > >Michael Murff <mjm33@MSM1.BYU.EDU> wrote: >Hi SAS-L, > > > >I have a dataset sorted on variable, VAR, whose values range from 1-100. I >want to execute a do block for the all observations were VAR is less than or >equal to the smallest value in the range of 25-27. So if 25 exists I only >want to execute the do block for the records having VAR le 25. But if 25 >does not exist then I want to execute the DO block on all records where VAR >le 26 or 27, whatever the smallest in the dataset may be. This seems to me >to be a rather difficult thing to code in logically. I could come up with >some kind of flagging scheme, I guess, and process the datasets before hand >and then condition on those flags, but _seems_ like something like this >could be done in a DO UNTIL or DO WHILE or DO BLOCK. > > > >Thanks for your patience with me, > > > >Michael Murff > >Provo, UT


Back to: Top of message | Previous page | Main SAS-L page