Pages

SyntaxHighlighter

Wednesday, November 2, 2011

Display ASCII codes

I found myself using the collating sequence of ASCII codes to force things to sort a certain way.  As a result, I wrote the below ASCII macro in SAS to display the printable ASCII characters.



<pre name="code" >
/*********************************************************
*     Program: ascii.sas
*      Author: Tom Bellmer
*     Created: 21OCT2011 
*     Purpose: Display printable ASCII characters
**********************************************************/
 
%macro ascii
  (  
      dsn    = ascii_table
    , showit = Y
  ) ;
 
  data &dsn ( drop = i j k );  
    attrib num1  length =  3 format =  5. label = 'Dec'
           char1 length = $1 format = $5. label = 'Chr'
           num2  length =  3 format =  5. label = 'Dec'
           char2 length = $1 format = $5. label = 'Chr'
           num3  length =  3 format =  5. label = 'Dec'
           char3 length = $1 format = $5. label = 'Chr'
           num4  length =  3 format =  5. label = 'Dec'
           char4 length = $1 format = $5. label = 'Chr' ;
 
    array n[ 0 : 3 ] _numeric_ ;
    array c[ 0 : 3 ] _character_ ;
 
    do i = 32 to 55 ;
      do j = 0 to 72 by 24 ;
        k = i + j ;
        n[ j / 24 ] = k ;
        c[ j / 24 ] = byte( k ) ;
      end ;
      output ;
    end ;
 
  run ;
 
  %if &sysenv = FORE 
    and %substr( &sysprocessname, 1, 3 ) = DMS 
    and &showit = Y %then %do ;
    dm "viewtable &dsn. view=table colheading=label" 
        viewtable  ;
  %end ;
 
%mend ;
 
/* EOF: ascii.sas  */
</pre>