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 all SAS Experts,

Because I am learning SAS so normally, I want to know many types of solutions for one problem, that is why in my code, I always try to find some alternative ways to deal with a step.

But it also comes with a cost that sometimes I do not know how to store my alternative solution quickly.

For example, these two methods below generating the same result:

*method 1;
proc means data=agg_amihud_vw noprint nway;
	class LOC;
	var agg_amh_vw;
	output out=final_amihud mean=final_amh;
run;

*method 2;
proc sql;
	create table new as 
	select LOC
		,mean(agg_amh_vw) as final_amh
	  from agg_amihud_vw
	  group by LOC
	;
run;

And when I want to put method 2 to comment, I need to delete all the semicolon, which causes a lot of trouble when I want to call it again because sometimes I just forget to put the ";" somewhere, especially in case the alternative code is long

*method 2
proc sql
	create table new as 
	select LOC
		,mean(agg_amh_vw) as final_amh
	  from agg_amihud_vw
	  group by LOC
	
run;

I know there are only three types of comment in SAS (*...; /* ....*/, and Comment....;). However, I want to ask your experience in storing the alternative method for one solution that I can learn from.

 

Many thanks and warmest regards.

Have a good week!

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
Tom
Super User Tom
Super User

In general block comments, /* .... */, work better to comment out pieces of code.

/*
*method 1;
proc means data=agg_amihud_vw noprint nway;
	class LOC;
	var agg_amh_vw;
	output out=final_amihud mean=final_amh;
run;
*/

*method 2;
proc sql;
	create table new as 
	select LOC
		,mean(agg_amh_vw) as final_amh
	  from agg_amihud_vw
	  group by LOC
	;
run;

Reserve the use of the statement comment,  * ... ;  or the macro comment, %* ... ; 

for entering comments that explain the code.  The difference between the two is whether or not you want the comment to appear in the log when the MPRINT option is turned on.  So a comment that makes the log understandable you would use statement comment.  A comment that only someone editing the code would need to see then using a macro comment will keep the log clean.

 

And if you ever need to comment out a block of code that already has block comments you can use a macro definition. 

%macro skip_this;
/*
*method 1;
proc means data=agg_amihud_vw noprint nway;
	class LOC;
	var agg_amh_vw;
	output out=final_amihud mean=final_amh;
run;
*/

*method 2;
proc sql;
	create table new as 
	select LOC
		,mean(agg_amh_vw) as final_amh
	  from agg_amihud_vw
	  group by LOC
	;
run;
%mend skip_this;

Just make sure not to call the macro by mistake. 🙂

 

 

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

In general block comments, /* .... */, work better to comment out pieces of code.

/*
*method 1;
proc means data=agg_amihud_vw noprint nway;
	class LOC;
	var agg_amh_vw;
	output out=final_amihud mean=final_amh;
run;
*/

*method 2;
proc sql;
	create table new as 
	select LOC
		,mean(agg_amh_vw) as final_amh
	  from agg_amihud_vw
	  group by LOC
	;
run;

Reserve the use of the statement comment,  * ... ;  or the macro comment, %* ... ; 

for entering comments that explain the code.  The difference between the two is whether or not you want the comment to appear in the log when the MPRINT option is turned on.  So a comment that makes the log understandable you would use statement comment.  A comment that only someone editing the code would need to see then using a macro comment will keep the log clean.

 

And if you ever need to comment out a block of code that already has block comments you can use a macro definition. 

%macro skip_this;
/*
*method 1;
proc means data=agg_amihud_vw noprint nway;
	class LOC;
	var agg_amh_vw;
	output out=final_amihud mean=final_amh;
run;
*/

*method 2;
proc sql;
	create table new as 
	select LOC
		,mean(agg_amh_vw) as final_amh
	  from agg_amihud_vw
	  group by LOC
	;
run;
%mend skip_this;

Just make sure not to call the macro by mistake. 🙂

 

 

Phil_NZ
Barite | Level 11

Hi @Tom 

 

Amazing and hand-on suggestion, thank you very much.

I have one question here.

Regarding the macro quoted for a block of code having a block comment already, I am very impressed by it, but I am quite confuse about the sentence "Just make sure not to call the macro by mistake", why I need to call the macro then?

Because I jus ttry to run the code, and I see the code inside the macro even does not run (and that's great), so could you please explain why you note me about macro calling thingy?

proc means data=_w.vw_amihud noprint nway;
	class LOC year;
	var vw_amh;
	output out=agg_amihud_vw sum=agg_amh_vw;
run;
%macro skip_this;
/*
*method 1;
proc means data=agg_amihud_vw noprint nway;
	class LOC;
	var agg_amh_vw;
	output out=final_amihud mean=final_amh;
run;
*/

*method 2;
proc sql;
	create table new as 
	select LOC
		,mean(agg_amh_vw) as final_amh
	  from agg_amihud_vw
	  group by LOC
	;
run;
%mend skip_this;

proc means data=agg_amihud_vw noprint nway;
	class LOC;
	var agg_amh_vw;
	output out=final_amihud mean=final_amh;
	label agg_amh_vw=aggregate of vw_amh of all companies 
    in one year in one country ;
run;

And as can be seen from the log, the block of code inside the macro is not excuted

28         options mprint;
29         proc means data=_w.vw_amihud noprint nway;
30         	class LOC year;
31         	var vw_amh;
32         	output out=agg_amihud_vw sum=agg_amh_vw;
33         run;

NOTE: There were 9212 observations read from the data set _W.VW_AMIHUD.
NOTE: The data set WORK.AGG_AMIHUD_VW has 127 observations and 5 variables.
NOTE: Compressing data set WORK.AGG_AMIHUD_VW decreased size by 0.00 percent. 
      Compressed is 1 pages; un-compressed would require 1 pages.
NOTE: PROCEDURE MEANS used (Total process time):
      real time           0.02 seconds
      cpu time            0.01 seconds
      

34         %macro skip_this;
35         /*
36         *method 1;
37         proc means data=agg_amihud_vw noprint nway;
38         	class LOC;
39         	var agg_amh_vw;
40         	output out=final_amihud mean=final_amh;
41         run;
42         */
43         
44         *method 2;
45         proc sql;
46         	create table new as
47         	select LOC
48         		,mean(agg_amh_vw) as final_amh
49         	  from agg_amihud_vw
50         	  group by LOC
51         	;
52         run;
53         %mend skip_this;
54         
55         proc means data=agg_amihud_vw noprint nway;
56         	class LOC;
57         	var agg_amh_vw;
58         	output out=final_amihud mean=final_amh;
59         	label agg_amh_vw=aggregate of vw_amh of all companies
60             in one year in one country ;
61         run;

NOTE: There were 127 observations read from the data set WORK.AGG_AMIHUD_VW.
NOTE: The data set WORK.FINAL_AMIHUD has 43 observations and 4 variables.
NOTE: Compressing data set WORK.FINAL_AMIHUD decreased size by 0.00 percent. 
      Compressed is 1 pages; un-compressed would require 1 pages.

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.
Tom
Super User Tom
Super User

This is a trick, not a feature.  We are using the functionality of defining a macro to prevent the code from running.  But that means it actually has defined the macro.  So you could accidentally run this line of code:

%skip_this;

which would call the macro that was defined and so run the code it contains.  Which would most likely be a mistake since you only created the macro to prevent the code from running.

Phil_NZ
Barite | Level 11

Hi @Tom 

It is a mind-boggling trick to me, thank you for making my week. I am totally defeated by the beauty of the SAS code!!!

 

Just a further cross-check: So you mean "do not call the macro by mistake" means that I just wrote the macro there, if I want to use this macro as a comment, just leave it as it is. If I want to execute the code inside this macro, I just type 

%skip_this;

I hope and I believe that I understand what you mean correctly.

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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 4 replies
  • 676 views
  • 2 likes
  • 2 in conversation