DATA Step, Macro, Functions and more

Rounding

Reply
Occasional Contributor
Posts: 8

Rounding

I wantto round 6274.5679 to the nearest integer to get 6275

Here is what I wrote data want ; set have ; format newvar 6. ; newvar = Round(oldvar,1); run; the error i get is Variable is oldvar uninitialized.
What mistake am I making.

Randy
Super User
Posts: 5,499

Re: Rounding

Posted in reply to RandyStan

Evidently, "oldvar" is the incorrect spelling.  Run a PROC CONTENTS on  your incoming data set to find the correct spelling of the variable names.

PROC Star
Posts: 735

Re: Rounding

Posted in reply to RandyStan

If you want the integer portion of a variable, the INT function is what you are looking for

 

data have;
   value = 6274.5679;
   round_value = round(value);
   int_value = int(value);

   put round_value;
   put int_value;
run;
Valued Guide
Posts: 505

Re: Rounding

 data _null_;

  x=6274.4999;
  put x hex16.;

  /* here is the problem non-terminating mantissa
     cannot represent the number exactly - you have lost already.
     
  6274.4999
  40B8827FF9724745

  like 1/3 in decimal (never is exact)
   0.3333333333333
  */

  x=62744999;
  put x hex16.;

  /* got it exactly
  62744999
  418DEB4D38000000
  */

  x=6274.5;
  put x hex16.;

  /* got it exactly (when mantissa is a power of 2**(-n))
  6274.5
  40B8828000000000
  */

  /* possible solution;
   summing in the integer domain is exact (given no overflow);
   I don not think it has to be this complex;
  */

  input numchr $9.;
  put numchr;
  numchr=compress(numchr,'.');
  exact_integer=input(numchr,9.);
  put exact_integer hex16.;
  * in microcode this should be a bit shifting operation and should be exact?;
  res=round(exact_integer,10000);
  res=res/10000;
  put res 9.4;

cards4;
6274.4999
6274.5000
6274.5001
;;;;
run;quit;



Ask a Question
Discussion stats
  • 3 replies
  • 157 views
  • 1 like
  • 4 in conversation