Macros are useful as interfaces
for table creation. You can use the SAS macro facility to help you
create new tables and add rows to existing tables.
The following example
creates a table that lists people to serve as referees for reviews
of academic papers. No more than three people per subject are allowed
in a table. The macro that is defined in this example checks the number
of referees before it inserts a new referee's name into the table.
The macro has two parameters: the referee's name and the subject matter
of the academic paper.
libname sql 'SAS-library';
proc sql;
create table sql.referee
(Name char(15),
Subject char(15));
/* define the macro */
%macro addref(name,subject);
%local count;
/* are there three referees in the table? */
reset noprint;
select count(*)
into :count
from sql.referee
where subject="&subject";
%if &count ge 3 %then %do;
reset print;
title "ERROR: &name not inserted for subject – &subject..";
title2 " There are 3 referees already.";
select * from sql.referee where subject="&subject";
reset noprint;
%end;
%else %do;
insert into sql.referee(name,subject) values("&name","&subject");
%put NOTE: &name has been added for subject – &subject..;
%end;
%mend;
Submit the %ADDREF()
macro with its two parameters to add referee names to the table. Each
time you submit the macro, a message is written to the SAS log.
%addref(Conner,sailing);
%addref(Fay,sailing);
%addref(Einstein,relativity);
%addref(Smythe,sailing);
%addref(Naish,sailing);
Defining Macros to Create Tables
34 %addref(Conner,sailing);
NOTE: 1 row was inserted into SQL.REFEREE.
NOTE: Conner has been added for subject - sailing.
35 %addref(Fay,sailing);
NOTE: 1 row was inserted into SQL.REFEREE.
NOTE: Fay has been added for subject - sailing.
36 %addref(Einstein,relativity);
NOTE: 1 row was inserted into SQL.REFEREE.
NOTE: Einstein has been added for subject - relativity.
37 %addref(Smythe,sailing);
NOTE: 1 row was inserted into SQL.REFEREE.
NOTE: Smythe has been added for subject - sailing.
38 %addref(Naish,sailing);
The output has a row
added with each execution of the %ADDREF() macro. When the table contains
three referee names, it is displayed in SAS output with the message
that it can accept no more referees.
Result Table and Message Created with SAS Macro Language Interface