BookmarkSubscribeRSS Feed
sasuser1031
Calcite | Level 5

how can I fix if there are zeros in b? I want to change b=1 when b=0 and run this code when b>0. 

 

Code: 
if (a < 1000 OR

(a/b <.1 & c < 5000000 & d IN (0,.)) OR

(a/b < .1 & c < 10000000 & d IN (0,.)) OR

(a < 500000 & c < 5000000 & d IN (0,.)) OR

(a < 1000000 & c < 10000000 & d IN (0,.))) then e = 1;

 

Log:

 

NOTE: Division by zero detected at line 1393 column 29.
NOTE: Division by zero detected at line 1394 column 29.

5 REPLIES 5
PaigeMiller
Diamond | Level 26

@sasuser1031 wrote:

how can I fix if there are zeros in b? I want to change b=1 when b=0 and run this code when b>0. 

 

Code: 
if (a < 1000 OR

(a/b <.1 & c < 5000000 & d IN (0,.)) OR

(a/b < .1 & c < 10000000 & d IN (0,.)) OR

(a < 500000 & c < 5000000 & d IN (0,.)) OR

(a < 1000000 & c < 10000000 & d IN (0,.))) then e = 1;

 

Log:

 

NOTE: Division by zero detected at line 1393 column 29.
NOTE: Division by zero detected at line 1394 column 29.


Before this IF statement, use

 

b=b*(b>0) + (b=0);

So if b>0, we use the value of b. If b=0 we use the value 1.

--
Paige Miller
ed_sas_member
Meteorite | Level 14

Hi @sasuser1031 

 

Once you've changed b=0 to b=1, do you want to run the code for this new value of b, or do you want to run this code only when b is initially different from 0?

If it is the first cas, you can try the below code.

Best,

data want;
	set have;
	if b=0 then b=1;
	if b>0 then do;
		if (a < 1000) OR
	  	 (a/b <.1 & c < 5000000 & d IN (0,.)) OR
	  	 (a/b < .1 & c < 10000000 & d IN (0,.)) OR
	  	 (a < 500000 & c < 5000000 & d IN (0,.)) OR
	  	 (a < 1000000 & c < 10000000 & d IN (0,.)) then e = 1;
	   end;
run;
sasuser1031
Calcite | Level 5

I want to run the  code for both. 

Ksharp
Super User

Using function DIVIDE() instead.

 

if (a < 1000 OR

( divide(a,b) <.1 & c < 5000000 & d IN (0,.)) OR

( divide(a,b) < .1 & c < 10000000 & d IN (0,.)) OR

(a < 500000 & c < 5000000 & d IN (0,.)) OR

(a < 1000000 & c < 10000000 & d IN (0,.))) then e = 1;

 

Tom
Super User Tom
Super User

So you want to change the zeros in B to ones?

 

if b=0 then b=1;

If you do first that then your code will run without any divide by zeros.

 

 

if (a<1000)
  or (((a/b)<0.1) and (c< 5000000) and d in (., 0))
  or (((a/b)<0.1) and (c<10000000) and d in (., 0))
  or ((a< 500000) and (c< 5000000) and d in (., 0))
  or ((a<1000000) and (c<10000000) and d in (., 0))
  then e=1
;

If you only want to do it only when B is > 0 then add that to the code.  Note that if you test after you have change the zeros to ones then B will now be > 0.

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

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