Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In SAS, executing a macro without a semicolon?

Tags:

sas

sas-macro

I am running a macro multiple times in SAS as follows:

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

My program/macro is similar to:

/* 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 I had forgotten a semicolon (as in the (a,a) one) and SAS didnt seem to mind. It ran all of the macro calls without a problem. I compared the output when I added a semicolon in and I couldnt see a difference.

Is this normal that SAS is not giving an error with the missing semicolon?

like image 754
user27008 Avatar asked Oct 21 '14 15:10

user27008


People also ask

Should macros have semicolons?

Macros are frequently used to make source code more readable. Macro definitions, regardless of whether they expand to a single or multiple statements, should not conclude with a semicolon.

How do you mask a semicolon in SAS?

The %STR function masks the semicolon by quoting it. %LET P=%STR(PROC PRINT DATA=DSN; RUN;); This results in the macro variable &P being correctly assigned the two statements.

How do you resolve a macro variable in SAS?

These examples using text expressions show how to assign the text generated by macro LOCATE or assign the value of the macro variable NAME: x=resolve('%locate'); x=resolve('&name'); the name of a DATA step variable whose value is a text expression.


1 Answers

Semicolons are not required for macro calls.

Often they are included as people are used to seeing semicolons as a way of "terminating the statement" or ending the line. I personally prefer to include them when possible as I believe it makes my code more readable.

Remember that macros simply evaluate themselves and return whatever it is that they resolve to which could be a block of code that looks like a number, a string, or anything else...

Take this example where no semicolon is used:

%macro field_list();
    name, 
    age, 
    sex, 
    height
%mend;

proc sql;
  select %field_list()
  from sashelp.class
  ;
quit;

Try running it on your own machine with option mprint; enabled. The result of running the macro simply returns the block of code within it. This results in the following code being executed:

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

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

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

This is because the semicolon is NOT required for calling macros, so it just gets left behind and included in the execution of the step.

When you call a macro like you do in the example you give above, it's fine to include the semicolon, because your macro is a fully self contained step. And in open code there's no harm having extraneous semicolons like so:

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

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

This basically evaluates to:

data tmp_class;
  set sashelp.class;
run;;

Note the extra semicolon at the end left over from our call? We could have as many as we wanted and the code will still run fine, ie:

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

Resolves to:

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

Which will still run fine as it is valid syntax.

like image 112
Robert Penridge Avatar answered Sep 27 '22 22:09

Robert Penridge