Tips & Tricks: Decimal Alignments

Align by decimal separator  

This is an example of how to produce an RTF file with numbers with decimals, where the numbers are aligned by the decimal separator.     

The magic is in this bit in the PROC REPORT:
    just=dec pretext="^R'\tqdec\tx650\tab '"    

It sets the justification to dec (for decimal) and adds a decimal tab stop followed by a tab character right before the numeric values. 

It only works if the separator is the same as the decimal separator as configured in your OS. An RTF produced on your system may align properly on your system but not on someone else's if they use a different decimal separator. 

This sample program generates a dataset with random words with random numbers, where the numbers are aligned by their separator (which is configurable).    

Source:  *******************************************************************************/      
%let numberoflines=20;
%let decsep=,;  

/* This webpage provides random words in a JSON string. */ filename words url "";   

/* This step reads the JSON string of words and creates a record for each. Each record is then also given a variable that contains a number (in text format) with 1 to 3 digits    for the integer part and 1 to 3 digits for the fractional part. */ 

data work.words;  
infile words;  
length line $ 1000;  
input line $;   
line = compress(line, "[]""");  
do i = 1 to count(line, ",")+1;    
    word = propcase(scan(line, i, ","));      

/* Create a random number with 1 to 3 digits before and after the decimal separator. */
   number = cats(substr(strip(put(ranuni(i)*1000, 8.)), 1, ceil(ranuni(i)*3)), "&decsep.", substr(strip(put(ranuni(i)*1000, 8.)), 1,     
keep word number;

filename words;  

/* Start the RTF and PDF output. */
options orientation=portrait nodate nonumber printerpath=pdf;
ods rtf style=journal file='%userprofile%\Desktop\dectab.rtf';
ods pdf style=journal file='%userprofile%\Desktop\dectab.pdf';
ods escapechar="^";  

title1 "Example of decimal alignment";  

proc report data=work.words;  
   column word number;    

   define word    / display"Word"   style(column)={cellwidth=30mm just=left}  style(header)={just=left};  
   define number  / display "Number" style(column)={cellwidth=25mm just=dec pretext="^R'\tqdec\tx650\tab '"} style(header)=
   {just=center}; run;  

ods rtf close;
ods pdf close;

Interested in more Tips & Tricks? Contact us.