Search code examples
sasdatastep

How do I place a tab character in a PUT statement within a DATA step?


How do I place a tab character in a PUT statement within a DATA step?

I am using SAS to output a processing log :

if first.ref then
    PUT 'PROCESSING: ' ref ;

if InceptionDate > LossDate then 
    do; 
        if first.ref then
            PUT 'WARNING: ' ref ' inception date prior to the loss date!' / ref= / InceptionDate= / LossDate=; 
            ... do some stuff...
    end;

I want the newline in the PUT, after the /, to indent. How can I insert a tab character?


Solution

  • Here are a few possible approaches:

    data _null_;
        ref = 001;
        inceptiondate = '01jan1960'd;
        lossdate = '01jun1960'd;
        format inceptiondate lossdate yymmdd10.;
    
        /*Without indent*/
      PUT 'WARNING: ' ref ' inception date prior to the loss date!' / ref= / InceptionDate= / LossDate=;
    
        /*Move the pointer to the right by 1 before printing*/
      PUT 'WARNING: ' ref ' inception date prior to the loss date!' /  +1 ref= / +1 InceptionDate= / +1 LossDate=;
    
        /*Move the pointer to column 2 before printing*/
      PUT 'WARNING: ' ref ' inception date prior to the loss date!' /  @2 ref= / @2 InceptionDate= / @2 LossDate=;
    
    
        /*# of spaces seems to depend on where you put the tab characters in the line containing the put statement*/
      PUT 'WARNING: ' ref ' inception date prior to the loss date!' / ' ' ref= / '  ' InceptionDate= / '    ' LossDate=;
    
        /*Works in external text editor but not in the SAS log window*/
      PUT 'WARNING: ' ref ' inception date prior to the loss date!' / '09'x ref= / '09'x InceptionDate= / '09'x LossDate=;
    
    run;
    

    Notes

    I'm not sure how to get this site to display tab characters properly - the third method involves writing code that contains tab characters in single quotes. If you copy and paste the code as displayed above, you get spaces instead. In SAS, the tab characters are converted to spaces before the code is run, so the amount by which you indent in the log depends on where your tab is in the code, and the log contains spaces rather than tabs.

    If you use the '09'x method, this works as expected if you redirect the log to an external file via proc printto log = "c:\temp\my.log"; run; and view it in your favourite text editor, but the SAS log window (in 9.1.3 at least) doesn't support tab characters - they're treated as single unprintable characters, displayed as rectangles.