Does SAS run a macro without a semicolon?

I run the macro several times in SAS as follows:

%mymac(a,b); %mymac(a,c); . %mymac(a,a) %mymac(a,w); . 

My program / macro is like:

 /* begin program here */ data original_data; set mylib.mydata; run; %macro mymac(x,y); data mydata1; set original_data; where school_district="&x"; run; proc means data=mydata1; var income; run; %mend mymac; 

I realized that I forgot the semicolon (as in (a, a)), and the SAS did not seem to mind. He ran all macro calls without any problems. I compared the output when I added a semicolon and I don't see the difference.

Is it normal that SAS does not give an error with a missing semicolon?

+5
source share
3 answers

The semicolons are not necessary for macro calls.

They are often included, as people are accustomed to seeing semicolons as a way to “terminate a statement” or end a line. I personally prefer to include them whenever possible, because I think this makes my code more readable.

Remember that macros simply evaluate themselves and return what they resolve to, which may be a block of code that looks like a number, a string, or something else ...

Take this example where the semicolon is not used:

 %macro field_list(); name, age, sex, height %mend; proc sql; select %field_list() from sashelp.class ; quit; 

Try running it on your machine with option mprint; . The result of running a macro simply returns a block of code inside it. This leads to the execution of the following code:

 proc sql; select name, age, sex, height from sashelp.class ; quit; 

If after calling our macro we had a semicolon, then the code that SAS tries to run would include a semicolon, which would be invalid syntax (note the semicolon after the height):

 proc sql; select name, age, sex, height ; from sashelp.class ; quit; 

This is due to the fact that a semicolon is NOT required to call macros, so it simply remains left and is included in the execution of the stage.

When you call a macro, as you do in the example you give above, it should contain a semicolon, because your macro is a completely autonomous step. And in open source, there is no harm having extraneous semicolons, for example:

 %macro example2(inDS=, outDs=); data &outDs; set &inDs; run; %mend; %example2(inDS=sashelp.class, outDs=tmp_class); 

This basically means:

 data tmp_class; set sashelp.class; run;; 

Pay attention to the extra semicolon at the end to the left of our call? We could have as much as we wanted, and the code would still work fine, i.e.

 %example2(inDS=sashelp.class, outDs=tmp_class);;;;;; 

Allows:

 data tmp_class; set sashelp.class; run;;;;;;; 

Which will still work fine, since this is a valid syntax.

+4
source

This is normal. The main thing you need to know about SAS Macro is that it is a code generation tool, not a real programming language. Although %my_mac(x,y); Looks like a traditional C-type function call where you want to end each statement with ; ending here ; doesn't really matter. Most likely, this is the SAS code that is generated inside %my_mac(x,y) . In your case, this code is grammatical SAS and in order. If you mentally replace the call to %my_mac(x,y) code that it generates (i.e. data mydata.....run; ), you will see that it is grammatical even without the end of %my_mac(x,y) using ; . If you wrote %my_mac(x,y); instead, it would just generate data mydate.....run;; It’s normal, but ; is redundant.

In fact, skipping a half-time is also a hack for the return value for a macro. For instance:

 %MACRO x_plus_n (x = , y= ); %SYSEVALF (&x + &y) %MEND; %LET x = 3 ; %LET y = 4 ; %LET z = %x_plus_n (x = &x , y= &y) is the result; %PUT ------------- &z; 

Note the absence ; in line% SYSEVALF. This allows the macro to not end until the end of the line %LET z = .... where there is ; . The result created by x_plus_n becomes the kind of return value for the macro.

+3
source

Macros are just text replacements. They are usually not executable statements (although sometimes they can execute statements using %SYSFUNC ). Thus, a semicolon is not required after the macro is called, unless it is required after the text contained in the macro (and actually not contained in the macro itself).

IE:

 %macro test; proc freq data=sashelp.class; run %mend test; 

should run as

 %test; 

because run does not have a semicolon. If you put a semicolon after run inside a macro, you can run it as %test without a semicolon.

Follow the first sentence above: Macros are just text replacements. Regardless of what text is created by the macro, it is then transferred to the SAS program and executed as if you typed it (perhaps you used %do i=1 %to 100; ), so it puts 100 copies of what is in loop - but it’s as if you had typed all 100). Consequently, the need for ; based on whether you need to enter it.

+2
source

Source: https://habr.com/ru/post/1205198/


All Articles