How to center data in a column and align decimals

Reply
Occasional Contributor
Posts: 8

How to center data in a column and align decimals

I'm trying to figure out how to center data in a column in proc report for ods pdf output and have the decimal points aligned.  I've been using just=d to align on decimals (which justifies to the right) but can't figure out how to also center the values.

This is what I'm getting:

Header

    17.5

      5.2

This is what I'm looking for:

Header

  17.5

    5.2

Any ideas?

Super Contributor
Posts: 1,636

Re: How to center data in a column and align decimals

Posted in reply to kmorrowvt

Is this helpful?

proc template;

define style styles.center;

  parent=styles.printer;

  /* Control PROC PRINT's headers     */

   style header from header /

    cellwidth=1in

   just=c;

  /* Control PROC PRINT's data values */

   style data from data /

   just=d;

  /* Control the OBS column           */

   style rowheader from header /

    cellwidth=1in

    just=c;

end;

run;

Occasional Contributor
Posts: 8

Re: How to center data in a column and align decimals

This doesn't seem to center the data.  The output is the same was using simply just=d.

SAS Super FREQ
Posts: 8,866

Re: How to center data in a column and align decimals

Posted in reply to kmorrowvt

Hi:

  Inside PROC REPORT, you can also use JUST=C and JUST=D -- but no matter whether you're using PROC PRINT, PROC REPORT, PROC TABULATE or a custom TABLE template -- you can only have 1 justification value for your cells. 1cell=1justification

  But, if you look at the attached screenshot (red lines are my highlight), what you can do is approximate JUST=D and centered by fiddling with the right margin of the cell. You won't be able to have a "one size fits all" solution, because the width= and rightmargin= value may need to change for different data or even different fonts or font sizes, but this gets you closer to what you want. You might also play around with cellpadding to see whether that might look better. But cellpadding increases ALL the white space around your font characters whereas right margin just leaves white space on the right side of the cell/column.

cynthia

data class;

  set sashelp.class;

  height = height+.8;

  weight = weight+.9;

  if name in('Alfred' 'Carol' 'James')

     then do;

     height = 88.88;

     weight =77.77;

  end;

run;

   

ods listing close;

title; footnote;

ods pdf file='c:\temp\just_d.pdf';

  

proc report data=class nowd;

  column name age height weight weight=wt2;

  define height / 'ht Just=c'

         style(column)={just=c};

  define weight / 'wt just=d'

         style(column)={just=d};

  define wt2 / 'wt just=d/chg margin'

         style(column)={just=d width=1.0in rightmargin=.25in};

run;

ods _all_ close;


just_d_and_changed_margin.jpg
Occasional Contributor
Posts: 8

Re: How to center data in a column and align decimals

Posted in reply to Cynthia_sas

This is great, thanks!

N/A
Posts: 1

Re: How to center data in a column and align decimals

Posted in reply to Cynthia_sas

Hey Cynthia, is there a similar work around for RTF, as the above code didn't work in RTF. Thanks.

Super User
Posts: 10,028

Re: How to center data in a column and align decimals

A workaround is padding a character (i.e. 0) after decimal , and make it color as white.

Assuming there are only two digits after decimal.

 
data class;
  set sashelp.class;
  height = height+.8;
  weight = weight+.9;
  if name in('Alfred' 'Carol' 'James')
     then do;
     height = 88.88;
     weight =77.77;
  end;
 length char_weight $ 80;
 if mod(weight*10,1)=0 then char_weight=put(weight,best6.2)||'~S={foreground=white}0';
  else char_weight=put(weight,best6.2 );
run;
   
ods listing close;
title; footnote;
ods escapechar='~';
ods rtf file='c:\temp\just_d.rtf' style=journal;
  
proc report data=class nowd;
  column name age height weight char_weight;
  define height / 'ht Just=c'
         style(column)={just=c};
  define weight / 'wt just=d'
         style(column)={just=d};
  define char_weight / 'wt just=c with padding 0'
         style(column)={just=c width=2in asis=on};
run;
ods rtf close;
ods listing;

Xia Keshan

Ask a Question
Discussion stats
  • 6 replies
  • 5364 views
  • 5 likes
  • 5 in conversation