BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
DavidPhillips2
Rhodochrosite | Level 12

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
Tom
Super User Tom
Super User

%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

7 REPLIES 7
PaigeMiller
Diamond | Level 26

all you need is a semicolon

A=5/2;

--
Paige Miller
DavidPhillips2
Rhodochrosite | Level 12

I am using SAS 9.2

 

I want a result of 2.5 not 2. 

Dividing two integers in SAS automatically rounds.

Tom
Super User Tom
Super User

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

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;

Tom
Super User Tom
Super User

%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__
Jade | Level 19

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

You can use %SYSEVALF to do floating point arithmetic in macro language.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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