Lorem Ipsum
Lorem Ipsum

Reputation: 4534

SAS: Output numeric values rounded to one decimal place unless integer

I need to take a value like 18.433333333 and output it as 18.4. At the same time, I need a value like 5 to be output as 5.

How can I do this?

All my numbers fall within the range [0, 100). So, a width of 4 seems appropriate. Three for each number character and one for the decimal. The best choices for format appear to be BESTDw.p or w.d.

If I use BESTD4.1 then 5 is 5 as desired, but 18.433333333 is 18.

If I use 4.1 then 18.433333333 is 18.4 as desired, but 5 is 5.00.

This appears in the context of PROC REPORT.

data example;
  input 
    ID $ 
    wd 
    bestdwp
    ;

  datalines;
  1 18.433333333 18.433333333
  2 5 5
  ;
run;

proc report data = example;
  column  
    ID
    wd
    bestdwp
    ;

  define ID      / 'ID';
  define wd      / 'w.d'       format = 4.1;
  define bestdwp / 'BESTDw.p'  format = bestd4.1;
run;

The output is then:

                                           BEST
                           ID         w.d  Dw.p
                           1         18.4    18
                           2          5.0     5

Upvotes: 2

Views: 3640

Answers (2)

Reeza
Reeza

Reputation: 21274

Use BEST4.1 instead of BESTD4.1

From the documentation

Integers are written without decimals.

Numbers with decimals are written with as many digits to the left and right of the decimal point as needed or as allowed by the width.

Upvotes: 1

user667489
user667489

Reputation: 9569

You can do this using a custom picture format. I'm not sure whether there is a simpler way, other than converting your numeric variable to text before passing it to proc report:

proc format;
picture mixed
  %macro loop;
    %do i = 0 %to 99;
      &i = '99'
      &i <-< %eval(&i+1) = '99.9'
    %end;
  %mend;
  %loop;
run;

data _null_;
  do i = 18.33, 18;
    put i mixed.;
  end;
run;

Output:

18.3
  18

A similarly constructed non-picture format also works:

proc format;
value mixed
  %macro loop;
    %do i = 0 %to 99;
      &i = [2.]
      &i <-< %eval(&i+1) = [4.1]
    %end;
  %mend;
  %loop;
run;

Upvotes: 0

Related Questions