hi,
I am using proc sql to divide two variables where the numerator is zero and the answer should be zero but I get a non zero number.
The value of 'Sum_weight' is zero for a particular country and when I divide it by sum_pieces, the result should be zero: 0/number=0
PROC SQL;
CREATE TABLE TEST AS
SELECT ORIG_COUNTRY,QUARTER,
SUM(GROSS_WEIGHT_KG) AS SUM_WEIGHT LABEL="SUM OF KG_WEIGHT" FORMAT=8.0,
CALCULATED SUM_WEIGHT/ CALCULATED SUM_PIECES AS AWPP FORMAT=8.2
FROM OAPQ1Q2
GROUP BY ORIG_COUNTRY ,QUARTER;
QUIT;
Thank you in advance.
If sum_weight is smaler than 0.5 it will show as zero with format 8.0. Change the format to BEST. to see what the real value is.
Mathematically you can't divide by 0 - SAS is giving you the mathematical answer of a missing value as the result of an invalid operation.
If you want to have the result as 0 by ignoring the maths then try a CASE statement:
case
when CALCULATED SUM_PIECES = 0 then 0
else CALCULATED SUM_WEIGHT / CALCULATED SUM_PIECES
end
as AWPP format 8.2,
If sum_weight is smaler than 0.5 it will show as zero with format 8.0. Change the format to BEST. to see what the real value is.
I removed the format and it works. The real value of sum_weight was not zero. Thank you.
@TARASK wrote:
hi,
I am using proc sql to divide two variables where the numerator is zero and the answer should be zero but I get a non zero number.
The value of 'Sum_weight' is zero for a particular country and when I divide it by sum_pieces, the result should be zero: 0/number=0
PROC SQL;
CREATE TABLE TEST AS
SELECT ORIG_COUNTRY,QUARTER,
SUM(GROSS_WEIGHT_KG) AS SUM_WEIGHT LABEL="SUM OF KG_WEIGHT" FORMAT=8.0,
CALCULATED SUM_WEIGHT/ CALCULATED SUM_PIECES AS AWPP FORMAT=8.2
FROM OAPQ1Q2
GROUP BY ORIG_COUNTRY ,QUARTER;
QUIT;
Thank you in advance.
sum_pieces is never created in your code.
And stop shouting at the computer (coding in all uppercase) 😉
Lowercase code is easier to read.
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!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.