DATA Step, Macro, Functions and more

Formatting numbers.

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

Formatting numbers.

Perhaps it quite simple, but I need to easily convert my data in multiples of 100 only like 619 should be 600, 754 should be 700,1238976 should be 1238900. Any easy fix? Thanks a lot.


Accepted Solutions
Solution
‎02-28-2018 02:00 PM
Super User
Posts: 24,027

Re: Formatting numbers.

Posted in reply to thepushkarsingh

Is the round down what you want? or just round? 

 

If it's ROUND, then use the ROUND() Function with 100 as the second parameter.

 

If it's floor, you need to divide by 100, floor it, and then multiply by 100.

 

answer1 = round(original, 100);
answer2 = floor(original/100)*100;

 

 

View solution in original post


All Replies
Solution
‎02-28-2018 02:00 PM
Super User
Posts: 24,027

Re: Formatting numbers.

Posted in reply to thepushkarsingh

Is the round down what you want? or just round? 

 

If it's ROUND, then use the ROUND() Function with 100 as the second parameter.

 

If it's floor, you need to divide by 100, floor it, and then multiply by 100.

 

answer1 = round(original, 100);
answer2 = floor(original/100)*100;

 

 

Frequent Contributor
Posts: 93

Re: Formatting numbers.

I have small doubt on the

answer1 = round(619, 100);
answer1=600;

how we are getting 600 can someone explain it...

can you please explain it..

 

I have understand in this method
x=round(223.456,.3);

Result:

x=223.5

In the above I have understand that

first step: 223.456*0.3=67.0368
second step :round(67.0368)=67.368
third step : 67.368/0.3=223.5

 

Super User
Posts: 24,027

Re: Formatting numbers.

Posted in reply to rajeshalwayswel

 

@rajeshalwayswel

 

Rounds the first argument to the nearest multiple of the second argument, or to the nearest integer when the second argument is omitted.

 

 

So in 619, the nearest 100 is 600 and 700. Because 600 is closer, it's rounded to 600. 

 

Your interpretation/logic appears incorrect - run the code. 

first step: 223.456*0.3=67.0368
second step :round(67.0368)=67.368 -> This would result in 67, not 67.368. 
third step : 67.368/0.3=223.5

 

My interpretation was always, figure out how many exact values of the second parameter fit in and then round up or down as necessary. 

 

 

The logic in my head is, assuming ROUND(P1, P2) is equivalent to:

 

Value = round(P1/P2,1) * P2 ;

 

 

At least for positive values. Here's some demo code that can help illustrate this. 

 

data demo;
 x=round(223.456, .3);
 y=223.456/0.3;
 z=round(y, 1);
 w=z*0.3;
 x1=round(67.0368);
 test=round(223.456/0.3)*0.3;
 test2=round(619, 100);
run;

rajeshalwayswel wrote:

I have small doubt on the

answer1 = round(619, 100);
answer1=600;

how we are getting 600 can someone explain it...

can you please explain it..

 

I have understand in this method
x=round(223.456,.3);

Result:

x=223.5

In the above I have understand that

first step: 223.456*0.3=67.0368
second step :round(67.0368)=67.368
third step : 67.368/0.3=223.5

 


 

Frequent Contributor
Posts: 93

Re: Formatting numbers.

Thank you.. It very clear..

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 144 views
  • 2 likes
  • 3 in conversation