Remove Comment Lines From Script/Input File

Hello,

I have a SAS code that predominantly has comments line and the real code like below and i want to remove ONLY THE COMMENTS from the code in the single line or spanned across multiple lines.

/********************************************************************
*** This Is a Comment Spanned Across Multiple Lines  ***
*********************************************************************/
data output;
  set input;
run;

* This Is a Comment But Only On a Single Line, Which Starts With Single or Multiple Asterisk and Ends With SemiColon;

data output2;
    set input2; /* This Is Another Comment In a Line which has Valid code and I do not want to remove this actual code but just want to remove this comment alone */
run;

Please let me know any options to achieve this.

I expect my output to be like below (without any of the above format of comment lines).

data output;
  set input;
run;

data output2;
    set input2; 
run;

Thanks,
Arun.

Why would anybody would want to remove comments in source code? Usually the person who wrote the code leave comments for the understanding of the code...
Do you believe it will run faster after?
If so you have a very little of how SAS works...
Ive seen people having to rewrite code because not understanding how it worked...

(P.S. I have more than 20 years of SAS installation, administration, I started with SAS6 on HP-UX, now running 9.4...on AIX)

Hello VBE,

That's not my intention and I am never going to overwrite the program. I am scanning a program and want to remove the comments while processing so that the keywords my scanning programs looks for will avoid looking through the comment lines.

Thanks for your response!

It is quite simple: you need a (recursive) parser for this, the same way the language is interpreted (i suppose?) by one. I suggest to retrieve your yacc utility and start writing...

If you are looking for a regex-solution ( grep , sed , awk , ...): none of these will work correctly, because languages are context-sensitive. Consider i.e. :

program_text /* this is a comment */ more program_text

and:

program_text "/* this is not a comment any more */" more program_text

and that is just one of the myriads of possibilities to lead a regexp astray.

1 Like

It is indeed difficult to give a complete answer for all corner cases, however, for simple cases, you could try how far something like:

perl -p0777e 's#/\*.*?\*/|\*.*?\;##sg' file

would get you. This could possibly work for one-off cases, but it will not be fool proof and can not be relied upon...