BookmarkSubscribeRSS Feed
deleted_user
Not applicable
I tried the following code:

data testo; orig=0.54999999999999; output; Run;
Data testo; Set testo; rnd= round(orig,0.1); Run;

Could you please explain me what happened?? It should be rounded to 0.5 and not 0.6!!

Thanks for your help,
Regards,
13 REPLIES 13
Cynthia_sas
SAS Super FREQ
Hi:
You might look at the documentation on ROUND, the ROUNDE and the ROUNDZ functions...they operate a bit differently when rounding. According to the doc they operate this way:
"Computing Results When the Value Is Halfway between Multiples of the Rounding Unit
When the value to be rounded is approximately halfway between two multiples of the rounding unit, the ROUND function rounds up the absolute value and restores the original sign.
<<skip long program example>>
The ROUND function is the same as the ROUNDE function except that when the first argument is halfway between the two nearest multiples of the second argument, ROUNDE returns an even multiple. ROUND returns the multiple with the larger absolute value.

The ROUNDZ function returns a multiple of the rounding unit without trying to make the result match the result that is computed with decimal arithmetic."


You might experiment and see whether ROUNDZ gives you the kind of rounding your want.

cynthia
deleted_user
Not applicable
Thank you very much for your great help!

Best regards,
hpl104
Calcite | Level 5

Hi Cynthia,

I tried ROUND, ROUNDE, and ROUNDZ functions and my results still do not turn out correctly. I welcome any suggestion(s) you may have for me.

Thanks,

Hui

Doc_Duke
Rhodochrosite | Level 12

Hui,

When I did roundz, I got the answer Cynthia described.  (SAS 9.3 on Windows).  Maybe you need to show your log.

Doc Muhlbaier

Duke

data testo; orig=0.54999999999999; output; Run;

Data testo; Set testo;

rnd= round(orig,0.1);

rnde= rounde(orig,0.1);

rndz= roundz(orig,0.1);

FILE LOG;  PUT orig rnd rnde rndz;

Run;

---------

16   Data testo; Set testo;

17   rnd= round(orig,0.1);

18   rnde= rounde(orig,0.1);

19   rndz= roundz(orig,0.1);

20   FILE LOG;  PUT orig rnd rnde rndz;

21   Run;

0.55 0.6 0.6 0.5

hpl104
Calcite | Level 5

Thanks Dr. Muhlbaier. Those functions work on single numbers but not when multiplication/division is involved. Do you happen to know the equivalent SAS syntax/functon for ROUND(+B50/J50,0)) in MS Excel? I assume that the plus(+)  sign indicate a positive number and not a ceiling function but I could be wrong. Below is part of my syntax:

result(i)= round(numerator(i)/denominator(i));

or should I use result(i)= round((ceiling(numerator(i)))/(denominator(i))) instead?

I wonder if there is anything missing in my code .  . .

Thanks again,

Hui

PS: I have to convert various Excel pivot tables into SAS datasets and my results differ.

Doc_Duke
Rhodochrosite | Level 12

Unfortunately, I don't know enough about Excel to help.

Reeza
Super User

AFAIK and from a very quick test in Excel the + doesn't do anything.

I've seen people use it as a shortcut instead of typing equals because then excel treats it as a number/formula, and then the round was possibly introduced later on and then doesn't make sense.
Or apparently and old habit from a Lotus user http://www.excelbanter.com/showthread.php?t=178349

From a quick look at how the function works my guess would be you're looking for

round(numerator(i)/denominator(i), 1);

hpl104
Calcite | Level 5

Thanks alot Reeza. Regardless of how I write the ROUND function, I find that rounding seems to 'operate' differently in SAS and Excel. For instance 46.54421 is rounded to 47 in Excel and 46 in SAS and 46.4645 is rounded to 46 in Excel and 47 in SAS. I will check the link out.

Reeza
Super User

No it doesn't...46.544 rounds to 47 and 46.4645 rounds to 46 as expected in both SAS 9.2 and 9.3

I couldn't find a consistent way to get your results either, so you may want to post the code and the numbers you're trying to replicate.

2119  data _null_;

2120  x= round(46.54421, 1);

2121  y= round(46.4645, 1);

2122

2123  put x y;

2124  run;

47 46

hpl104
Calcite | Level 5

Not when I do divisions. 46.54421 and 46.4645 are elements of result(i).

result(i)= round(numerator(i)/denominator(i))

Thanks, Reeza.

Reeza
Super User

What are numerator(i) and denominator(i)? And what format is applied to the numbers

I still can't get those numbers you're getting, so there's something missing somewhere:

data test;

x= 46.54;

y= 46.46;

do i =1 to 5;

    numerator1=4654*i;

    numerator2=4646*i;

    denominator=i*100;

    unround1=numerator1/denominator;

    unround2=numerator2/denominator;

    round1=round(numerator1/denominator);

    round2=round(numerator2/denominator);

    output;

end;

run;

proc print data=test;

run;

hpl104
Calcite | Level 5

demoninator = 49.2333333333333333

numerator = 1.057775751

Thanks again, Reeza

1.057775751
1.057775751
Reeza
Super User

It works fine for me still, rounds to 47 as expected, there's something else in code that's causing this issue, not the round function, provide the full code for the data step.

183  data _null_;

184      x= round(49.23333333/1.0577775751, 1);

185      x2=49.233333/1.0577775751;

186      put x x2;

187  run;

47 46.54412625

NOTE: DATA statement used (Total process time):

      real time           0.00 seconds

      cpu time

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 13 replies
  • 3354 views
  • 0 likes
  • 5 in conversation