Your SAS programs, embedded in web apps and elsewhere

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

Accepted Solution Solved
Reply
Super Contributor
Posts: 480
Accepted Solution

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


Accepted Solutions
Solution
‎03-03-2015 11:15 AM
Super User
Super User
Posts: 6,499

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.

View solution in original post


All Replies
Trusted Advisor
Posts: 1,612

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

all you need is a semicolon

A=5/2;

Super Contributor
Posts: 480

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.

Super User
Super User
Posts: 6,499

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?

Super Contributor
Posts: 480

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;

Solution
‎03-03-2015 11:15 AM
Super User
Super User
Posts: 6,499

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.

Respected Advisor
Posts: 3,777

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.

Super Contributor
Posts: 480

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

Sysevalf worked.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

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