  DavidPhillips2
Rhodochrosite | Level 12

## How Do you Divide two Numbers and Return the Result as a Decimal?

I am trying to do division on whole numbers and return them as a decimal. I’m getting confused on what the right cast or parse in SAS is.

1. E.g.

A = 5/2

.. A has a value of 2.5

1 ACCEPTED SOLUTION

Accepted Solutions

## Re: How Do you Divide two Numbers and Return the Result as a Decimal?

%EVAL() is a MACRO function.  Not really SAS.

To evaluate macro values using floating point you need to %SYSEVALF() not %EVAL().

You probably also want to use %SYSEVALF() in your %IF statement instead of the implied %EVAL() for the same reason.

7 REPLIES 7

## Re: How Do you Divide two Numbers and Return the Result as a Decimal?

all you need is a semicolon

A=5/2;

--
Paige Miller  DavidPhillips2
Rhodochrosite | Level 12

## Re: How Do you Divide two Numbers and Return the Result as a Decimal?

I am using SAS 9.2

I want a result of 2.5 not 2.

Dividing two integers in SAS automatically rounds.

## Re: How Do you Divide two Numbers and Return the Result as a Decimal?

SAS stores all numbers as floating point and does not automatically round.  If the variable you wrote the result into has a format attached then the value might appear as rounded because of the format being applied.

try

put x= best32. ;

Are you perhaps pulling from a database and SAS has pushed the division into the database to execute?  DavidPhillips2
Rhodochrosite | Level 12

## Re: How Do you Divide two Numbers and Return the Result as a Decimal?

Tom,

This is the same query that you helped me with earlier.  I’m querying a total and a subtotal and displaying the result if the subtotal is >65% of the total.  When I divide my result acts and looks like 0.

/*query the max degree level*/

SELECT total, &enrRepTy, &degOrStudIDTy into :max_level_total2, :max_level_name2 SEPARATED BY '', :max_deg_Or_stud_id2

from

( select sum(students_enrolled) as total, &enrRepTy, &degOrStudIDTy

from S_or_D_enroll_indexed_OffCampus

group &enrRepTy, &degOrStudIDTy)

having total = max(total);

/*query total for degree level*/

SELECT sum(students_enrolled) into :total_level_total2

from S_or_D_enroll_indexed_OffCampus

/*functionally should be %let subgroupRatio = %eval((2683 / 8314);*/

%let subgroupRatio = %eval((max_level_total2 / total_level_total2);

format subgroupRatio Numeric 12.2;

%if (&subgroupRatio > .65  %then %do;

proc sgrender data=S_or_D_enroll_indexed_OffCampus template=ColorByIndexWDataLab;

where &degOrStudIDTy EQ "&max_deg_Or_stud_id2";

dynamic title1=" ";

/*subgroupRatio acts like 0 and displays 0*/";

dynamic title2="&max_level_name2 only &subgroupRatio

run;

%end;

## Re: How Do you Divide two Numbers and Return the Result as a Decimal?

%EVAL() is a MACRO function.  Not really SAS.

To evaluate macro values using floating point you need to %SYSEVALF() not %EVAL().

You probably also want to use %SYSEVALF() in your %IF statement instead of the implied %EVAL() for the same reason.  data_null__

## Re: How Do you Divide two Numbers and Return the Result as a Decimal?

You left off the part about I'M USING MACRO LANGUAGE.

You can use %SYSEVALF to do floating point arithmetic in macro language.  DavidPhillips2
Rhodochrosite | Level 12

## Re: How Do you Divide two Numbers and Return the Result as a Decimal?

Sysevalf worked.

Discussion stats
• 7 replies
• 11151 views
• 6 likes
• 4 in conversation