This sample illustrates how to remove comments from a program without changing the executable code.
Note: This program can be adjusted to make a copy of the original program without the comment statements included by editing the PUT statements.
Comments can be on a single line or span multiple lines. If only the 'open' is found on a line, the comment is removed and a flag is set to indicate that a block comment has been started. Nothing is written to the new file until the 'close' is found.
The program also checks each line to see whether it begins with an asterisk. If an asterisk is the first character, the statement is not written out. Note this could present a problem depending on individual coding style.
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
data _null_;
infile datalines4 truncover;
input line $char100.;
file print;
put "Original Line: " line;
length tmpline $80;
retain tmpline ' ' flag 0;
beg_=index(line,'/*');
end_=index(line,'*/');
/* Comment statement begins and ends on the same line */
if beg_ > 0 and end_ > 0 then do;
if substr(trim(line),1,2) = '/*' and
substr(trim(line),length(line)-1,2) = '*/' then;
else do;
/* if comment at begining of line */
if beg_-1=0 then line=substr(line,end_+2);
/* else comment in middle of line */
else line=substr(line,1,beg_-1)||substr(line,end_+2);
if line ne ' ' then put @6 'New Line: ' line $char100. /;
end; /* else do */
end;
/* Comment spans multiple lines */
else if beg_ > 0 and end_ = 0 then do;
if beg_ > 1 then tmpline=substr(line,1,beg_-1);
else tmpline=' ';
flag=1;
end;
else if beg_ = 0 and end_ > 0 then do;
line=trim(tmpline)||substr(line,end_+2);
if line ne ' ' then put @6 'New Line: ' line $char100. /;
flag=0;
end;
else if flag=0 then do;
/* If first byte is an asterisk */
if substr(left(line),1,1) = '*' then do;
line = substr(line,index(line,';')+1);
end;
put @6 'New Line: ' line $char100. /;
end;
datalines4;
/* this line is all a comment line so no new line is output */
/* beginning comment line */ text line to keep!
Start text/*embedded comment*/-end text to keep
/* this is a PL/1-style comment */ text line to keep
This line contained an /* this is a comment */embedded comment
This line contains asterisks **** but is not a comment
* this is another style comment line; text line to keep
;;;;
run;
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
Original Line: /* this line is all a comment line so no new line is output */ Original Line: /* beginning comment line */ text line to keep! New Line: text line to keep! Original Line: Start text/*embedded comment*/-end text to keep New Line: Start text-end text to keep Original Line: /* this is a PL/1-style comment */ text line to keep New Line: text line to keep Original Line: This line contained an /* this is a comment */embedded comment New Line: This line contained an embedded comment Original Line: This line contains asterisks **** but is not a comment New Line: This line contains asterisks **** but is not a comment Original Line: * this is another style comment line; text line to keep New Line: text line to keep
Type: | Sample |
Topic: | SAS Reference ==> DATA Step SAS Reference ==> Functions ==> Character |
Date Modified: | 2019-06-11 14:59:34 |
Date Created: | 2004-09-30 14:09:08 |
Product Family | Product | Host | SAS Release | |
Starting | Ending | |||
SAS System | Base SAS | All | n/a | n/a |