|
Data;
t=20;
b=0;
* 0 divide case -- Missing if denominator = 0 ;
y=( t + ordinal((b=0)+1,.,0 ) ) / b;
put y=;
t=20;
b=4;
* this works -- adds zero to numerator ;
y=( t + ordinal((b not = 0)+1,.,0 ) ) / b;
put y=;
run;
HERMANS1@WESTAT.COM (Sigurd Hermansen) wrote in message news:<08B08C9FA5EBD311A2CC009027D5BF810222B81B@remailnt2-re01.westat.com>...
> For those that cannot wait for SAS V9.1, SAS SQL offers a fairly robust
> implementation of the IIF for the division by zero test:
>
> data test;
> x=1;
> y=1;
> do i=1 to 1e4;
> y=y/2;
> output;
> end;
> run;
> proc sql;
> create view testVW as
> select x/(case when y ne 0 then y else . end) as z
> from test
> ;
> quit;
>
> For those who hate to waste those extra keystrokes, try
>
> %macro IIFno0Div(__n,__d);
> &__n/(case when &__d ne 0 then &__d else . end)
> %mend IIFno0Div;
>
> proc sql;
> create view testVW as
> select %IIFno0Div(x,y) as z
> from test
> ;
> quit;
>
> Sig
> -----Original Message-----
> From: Dale McLerran [mailto:stringplayer_2@YAHOO.COM]
> Sent: Friday, July 19, 2002 5:28 PM
> To: SAS-L@LISTSERV.UGA.EDU
> Subject: Immediate IF functions
>
>
> Folks,
>
> Thought you might be interested in seeing this. We asked for an
> immediate IF and have received. Note the version number at the top
> of the log. Though it does state version 8.2, the word that I have
> had from SI is that 9.01 is really the targeted version for release
> of these functions. Note, too, that some additional functions
> beyond immediate if have been developed. I should also point out
> that these functions, while they run under WIN_NT, may not be
> available under version 8.2 for all platforms.
>
> There is one other observation to be made. The immediate if was
> promoted as a solution to the divide by zero/missing problem. Well,
> it turns out that the function IFN does not avoid the problem of
> the divide by zero. The SAS supervisor evaluates all the arguments
> to the function. The way that the immediate if was promoted for
> avoidance of the divide by zero was code like the following:
>
> x=0;
> y=10;
> z = ifn(x, y/x, .);
>
> Well, all the arguments are evaluated by the SAS supervisor prior
> to the execution of the function. Thus, a divide by zero still
> occurs. This is demonstrated at the end of the log below. Jack
> Hamilton was the first person to point this out.
>
>
>
> 1 The SAS System 13:59 Friday, July 19, 2002
>
> NOTE: Copyright (c) 1999-2001 by SAS Institute Inc., Cary, NC, USA.
> NOTE: SAS (r) Proprietary Software Release 8.2 (TS2M0)
> Licensed to FRED HUTCHINSON CANCER RESEARCH CENTER, Site
> 0013576001.
> NOTE: This session is executing on the WIN_NT platform.
>
>
>
> NOTE: SAS initialization used:
> real time 1.88 seconds
> cpu time 0.12 seconds
>
> 1 options ls=70 ps=max;
> 2
> 3 /*
> 4
> 5 num = IFN( test, if_true, if_false <, if_missing> );
> 6 chr = IFC( test, if_true, if_false <, if_missing> );
> 7
> 8 num = CHOOSEC ( which_slot, slot1, slot2, slot3, ... );
> 9 chr = CHOOSEC ( which_slot, slot1, slot2, slot3, ... );
> 10
> 11 choose uses the value of the first argument to
> 12 select from the following arguments. negative
> 13 counts slots from "the end"
> 14
> 15 */
> 16
> 17
> 18 data iftest;
> 19
> 20 n1 = ifn( 1, 2, 3 );
> 21 put 's/be 2:' n1;
> 22 n1 = ifn( 0, 2, 3 );
> 23 put 's/be 3:' n1;
> 24 n1 = ifn( ., 2, 3 );
> 25 put 's/be 3:' n1;
> 26 n1 = ifn( ., 2, 3, 4 );
> 27 put 's/be 4:' n1;
> 28 n1 = ifn( 0, 2, 3, 4 );
> 29 put 's/be 3:' n1;
> 30 n1 = ifn( 5*11, 2, 3 );
> 31 put 's/be 2:' n1;
> 32
> 33 c1 = ifc( 1, 'tru', 'false' );
> 34 put 's/be tru:' c1;
> 35 c1 = ifc( 0, 'tru', 'false' );
> 36 put 's/be false:' c1;
> 37 c1 = ifc( ., 'tru', 'false' );
> 38 put 's/be false:' c1;
> 39 c1 = ifc( ., 'tru', 'false', 'miss' );
> 40 put 's/be miss:' c1;
> 41 c1 = ifc( 0, 'tru', 'false', 'miss' );
> 42 put 's/be false:' c1;
> 43 c1 = ifc( 21*2, 'tru', 'false' );
> 44 put 's/be tru:' c1;
> 45
> 46 run;
>
> s/be 2:2
> s/be 3:3
> s/be 3:3
> s/be 4:4
> s/be 3:3
> s/be 2:2
> s/be tru:tru
> s/be false:false
> s/be false:false
> s/be miss:miss
> s/be false:false
> s/be tru:tru
> NOTE: The data set WORK.IFTEST has 1 observations and 2 variables.
> NOTE: DATA statement used:
> real time 0.30 seconds
> cpu time 0.02 seconds
>
>
> 47
> 48 /* how to set the retlen of ifc() */
> 49 proc contents; run;
>
> NOTE: PROCEDURE CONTENTS used:
> real time 0.28 seconds
> cpu time 0.07 seconds
>
> NOTE: The PROCEDURE CONTENTS printed page 1.
>
> 50
> 51
> 52 data _null_;
> 53
> 54 n1=choosen(1,1,2,3,4,5);
> 55 put 's/be 1: ' n1=;
> 56 n1=choosen(2,1,2,3,4,5);
> 57 put 's/be 2: ' n1=;
> 58 n1=choosen(3,1,2,3,4,5);
> 59 put 's/be 3: ' n1=;
> 60 n1=choosen(4,1,2,3,4,5);
> 61 put 's/be 4: ' n1=;
> 62 n1=choosen(5,1,2,3,4,5);
> 63 put 's/be 5: ' n1=;
> 64
> 65 n1=choosen(-5,1,2,3,4,5);
> 66 put 's/be 1: ' n1=;
> 67 n1=choosen(-4,1,2,3,4,5);
> 68 put 's/be 2: ' n1=;
> 69 n1=choosen(-3,1,2,3,4,5);
> 70 put 's/be 3: ' n1=;
> 71 n1=choosen(-2,1,2,3,4,5);
> 72 put 's/be 4: ' n1=;
> 73 n1=choosen(-1,1,2,3,4,5);
> 74 put 's/be 5: ' n1=;
> 75
> 76
> 77 c1=choosec(1,'a','b','c','d');
> 78 put 's/be a: ' c1=;
> 79 c1=choosec(2,'a','b','c','d');
> 80 put 's/be b: ' c1=;
> 81 c1=choosec(3,'a','b','c','d');
> 82 put 's/be c: ' c1=;
> 83 c1=choosec(4,'a','b','c','d');
> 84 put 's/be d: ' c1=;
> 85
> 86 c1=choosec(-4,'a','b','c','d');
> 87 put 's/be a: ' c1=;
> 88 c1=choosec(-3,'a','b','c','d');
> 89 put 's/be b: ' c1=;
> 90 c1=choosec(-2,'a','b','c','d');
> 91 put 's/be c: ' c1=;
> 92 c1=choosec(-1,'a','b','c','d');
> 93 put 's/be d: ' c1=;
> 94
> 95 run;
>
> s/be 1: n1=1
> s/be 2: n1=2
> s/be 3: n1=3
> s/be 4: n1=4
> s/be 5: n1=5
> s/be 1: n1=1
> s/be 2: n1=2
> s/be 3: n1=3
> s/be 4: n1=4
> s/be 5: n1=5
> s/be a: c1=a
> s/be b: c1=b
> s/be c: c1=c
> s/be d: c1=d
> s/be a: c1=a
> s/be b: c1=b
> s/be c: c1=c
> s/be d: c1=d
> NOTE: DATA statement used:
> real time 0.02 seconds
> cpu time 0.01 seconds
>
>
> 96
> 97
> 98
> 99
> 100 /* Examine the divide by zero issue */
> 101
> 102 data _null_;
> 103 y=9; x=3; ratio = ifn(x,y/x,.);
> 104 put 's/be 3:' ratio;
> 105 y=9; x=0; ratio = ifn(x,y/x,.);
> 106 put 's/be .:' ratio;
> 107 y=9; x=.; ratio = ifn(x,y/x,.);
> 108 put 's/be .:' ratio;
> 109 run;
>
> s/be 3:3
> NOTE: Division by zero detected at line 105 column 30.
> s/be .:.
> s/be .:.
> y=9 x=. ratio=. _ERROR_=1 _N_=1
> NOTE: Missing values were generated as a result of performing an
> operation on missing values.
> Each place is given by: (Number of times) at (Line):(Column).
> 1 at 107:30
> NOTE: Mathematical operations could not be performed at the following
> places. The results of the operations have been set to missing
> values.
> Each place is given by: (Number of times) at (Line):(Column).
> 1 at 105:30
> NOTE: DATA statement used:
> real time 0.01 seconds
> cpu time 0.01 seconds
>
>
> 110
>
> NOTE: SAS Institute Inc., SAS Campus Drive, Cary, NC USA 27513-2414
> NOTE: The SAS System used:
> real time 2.60 seconds
> cpu time 0.25 seconds
>
>
>
> =====
> ---------------------------------------
> Dale McLerran
> Fred Hutchinson Cancer Research Center
> mailto: dmclerra@fhcrc.org
> Ph: (206) 667-2926
> Fax: (206) 667-5977
> ---------------------------------------
>
> __________________________________________________
> Do You Yahoo!?
> Yahoo! Autos - Get free new car price quotes
|