BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Phil_NZ
Barite | Level 11

Hi SAS Users,

 

Normally I treated two ways of comments in SAS (/*   */ and *    ;) similarly. I normally use the shortcut Ctr /  to make the comment line so did not notice the difference between these two.

 

However, today I want to separate the comment way to make my code aesthetically. For example, I want to let the comment outside the datastep as /* */ while the comments inside the datastep is * ;

 

It turns out as below

data filter_;
	set my.keepvar_1999_2001_1;
	*Sufficient 3 componenst t calculate return and adjusted price;
	 if n(ajexdi,prccd,trfd)=3 or n(prccd,prchd,prcld)=3
	 /**/
	 and
	 ;
run;

My97_0-1614651564912.png

As you can see from the picture, the word and turned red and * ; seem not to work while they are still black (I did not post the code because the code here did not show the red and black colors of what I want to impress)

 

I am wondering if I fall into any fallacy.

 

Warmest regards.

Thank you for your help, have a fabulous and productive day! I am a novice today, but someday when I accumulate enough knowledge, I can help others in my capacity.
1 ACCEPTED SOLUTION

Accepted Solutions
ChrisHemedinger
Community Manager

Someone else will reply with all of the various comment mechanisms in SAS, but I'll refer you to this doc.

 

The "* ;" style of comment must be expressed as a separate statement, so you cannot embed it inside another statement as you did in your example.  You could place a "/* */" style of comment in there.

 

So not this:

if A or B
* my comment;
and C
;

But this is okay:

if A or B
/* my comment */
and C
;

 

You can also get very explicit with the COMMENT statement instead of that asterisk, but that's rarely done and not documented any longer, I think:

 

data _null_;
 comment This is a comment;
run;

 

 

 

SAS Hackathon registration is open! Build your skills. Make connections. Enjoy creative freedom. Maybe change the world.

View solution in original post

7 REPLIES 7
ChrisHemedinger
Community Manager

Someone else will reply with all of the various comment mechanisms in SAS, but I'll refer you to this doc.

 

The "* ;" style of comment must be expressed as a separate statement, so you cannot embed it inside another statement as you did in your example.  You could place a "/* */" style of comment in there.

 

So not this:

if A or B
* my comment;
and C
;

But this is okay:

if A or B
/* my comment */
and C
;

 

You can also get very explicit with the COMMENT statement instead of that asterisk, but that's rarely done and not documented any longer, I think:

 

data _null_;
 comment This is a comment;
run;

 

 

 

SAS Hackathon registration is open! Build your skills. Make connections. Enjoy creative freedom. Maybe change the world.
Phil_NZ
Barite | Level 11

Hi @ChrisHemedinger 

 

I am totally impressed by the last method, you are right, I have never seen such an explicit way of comment before.

I did some searches but did not find out how to use this comment way.

 

Can I ask whether I can use it interchangeably with /*  */ ?

Warm regards.

Thank you for your help, have a fabulous and productive day! I am a novice today, but someday when I accumulate enough knowledge, I can help others in my capacity.
ChrisHemedinger
Community Manager

No, COMMENT...; is a statement. And technically *....; is a statement -- so you cannot place these within other statements.

 

/* ... */ is the most flexible (and familiar to those who may have encountered this style in other programming languages). But long-time SAS programmers may have their favorite methods of using different comment styles to adorn their code.

SAS Hackathon registration is open! Build your skills. Make connections. Enjoy creative freedom. Maybe change the world.
Phil_NZ
Barite | Level 11

Hi @ChrisHemedinger 

 

I want to argue about this part,

I am wondering why I can put COMMENT inside another statement as below

 

My97_0-1614719474947.png

 

(This code is run successfully)

That is why I asked if COMMENT...; is interchangeable with /*...*/

 

Warmest regards.

Thank you for your help, have a fabulous and productive day! I am a novice today, but someday when I accumulate enough knowledge, I can help others in my capacity.
ChrisHemedinger
Community Manager

In SAS, a statement is a keyword, perhaps followed by some arguments/data, terminated by a semicolon.

 

The DATA step is a "step" (just like PROC steps are steps), and may contain multiple statements. Here's a crude diagram.

 

2021-03-02_16-19-17.png

You cannot add a "statement-style comment" inside a statement (like between "set" on line 2 and its terminating semicolon).  But you could insert a /* */ block in there.

SAS Hackathon registration is open! Build your skills. Make connections. Enjoy creative freedom. Maybe change the world.
Phil_NZ
Barite | Level 11

Hi @ChrisHemedinger 

 

Sorry for my mistake about the misunderstanding between the statement and data step, it is clear now.

So I can wrap up things like that if I want to adorn my code:

*...; for comment outside datastep

 

comment statement for inside datastep but outside statement

 

/*...*/ for inside data statement.

 

Well, it looks great then.

Warm regards.

Thank you for your help, have a fabulous and productive day! I am a novice today, but someday when I accumulate enough knowledge, I can help others in my capacity.
qoit
Pyrite | Level 9

There are three types of comments: PL/1 style, asterisk style, and macro style.

 

1.PL/1 style comments are processed in the SAS word scanner, character by character. /* This comment's bytes are processed character by character. */

 

2. Asterisk style comments are processed in the tokenizer, token by token, until a semicolon is reached. However, single and double quotation marks are treated as special tokens, so that string literal tokens are NOT built in this case. * Asterisk comments are read token by token but quotes are treated like single tokens.; (However, inside a macro definition, they are gathered as constant text. So the constant text is built with tokenization rules, including string literal tokens.)

 

3. Macro style comments are processed by the macro facility, token by token, until a semicolon is reached. Single and double quotation marks build string literals, so every open quotation mark must have a closing quotation mark. %* This macro comment is read token by token until the semicolon.;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 1323 views
  • 4 likes
  • 3 in conversation