Search code examples
sascomments

Is there a way to create a box of asterisks around a SAS comment?


I was wondering if there was a way to quickly create a box around a comment in SAS. Currently you can use a command Ctrl + Shift + / to create comments such as this

/*This is a comment*/
/*This is the second line of the comment*/

I would like to know if anyone has a solution to box in multiple lines of comments, like this:

/******************************************/
/* This is a comment                      */
/* This is the second line of the comment */
/******************************************/

Currently the only way I know how to create boxes like this is to manually type in asterisks and add spaces until the code lines up. I'm hoping there is a more efficient solution.


Solution

  • This macro will create the comment box in the log. You can then copy and paste it into your code.

    It uses '/' as the default delimiter to split the lines. But this can be changed when calling the macro, as shown below.

    %macro comment_box(comment, delimiter='/');
        /* count the number of line using the delimiter */ 
        %let line_count = %sysevalf(%sysfunc(countc(%str(&comment.), &delimiter.)) + 1);
        %let max_line_len = 0;
        /* loop through to split the text into lines measure the line length including leading&trailing blanks */
        %do x=1 %to &line_count.;
            %let line&x. = %scan(%str(&comment.), &x., &delimiter.);
            %let line&x._len = %sysevalf(%length(%str(&&line&x..)) + 2);
            %if &&line&x._len. > &max_line_len. %then %let max_line_len = &&line&x._len.;
        %end;
    
        /* Create the top/bottom box line matching to the max text line length */
        /* Add the comment box to the log using PUT statements. */
        option nonotes;
        data _null_;
            line_count = &line_count.;
            max_line_len = &max_line_len.;
            box_line = cat('/*', repeat('*', max_line_len - 1), '*/');
            %do x=1 %to &line_count.;
                %if &max_line_len. = &&line&x._len. %then %do;
                    line&x. = cat('/* ', "&&line&x..", ' */');
                %end;
                %else %do;
                    line&x. = cat('/* ', "&&line&x..", repeat(' ', %sysevalf(&max_line_len. - &&line&x._len. - 1)), ' */');
                %end;
            %end;
            /* add the comment box to the log */
            put box_line;
            %do x=1 %to &line_count.;
                put line&x.;
            %end;
            put box_line;
        run;
        option notes;
    %mEnd;
    
    
    %comment_box(%str(This is a comment/This is the second line of the comment));
    %comment_box(%str(This is a comment+This is the second line of the comment), delimiter= '+');