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

Dear all,

 

I am new to SAS and I am wondering how come the code below can only create the last table instead of three. Here attached the code below for anyone could give me a helping hand. I have tried many times but it can only be able to create the last table 'countrytbl' instead of all of three tables that are storing inside the dataset. 

 

 

data dtables;
input tablename $;
datalines;
flightnotbl
customertbl
countrytbl
run;

 

%MACRO createtbl(tname);
proc sql;
   create table _&tname
   (field1 char(4),
   field2 char(1),
   field3 char(3));
quit;
%MEND;

 

data _null_;
set dtables nobs=last;
    call symput('tblname',tablename);
     %createtbl(&tblname);
run;

 

Thank you very much!

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

 

Yes, you have fallen into the same problems as almost everyone else doing macro programming.  Let me explain.  Macro code is not executable - on its own is does nothing.  All its function is is to create text which then gets passed to the Base SAS compiler which is where code gets executed.  It does not interact with datastep, so you can't use it as part of a datastep - it only  creates text.  I have fixed your code given below, however I question why you are doing this at all.  You can simply create all three tables in one step:

data flightnotbl customertbl countrytbl;
  length field1 $4 field2 $1 field3 $2;
  if _n_ < 0 then output;
run;

No need for any of your code.  As a tip, learn Base SAS which is the programming language.  Forget macro language until you actually have a use for it - 99% of the time things can be done in Base SAS, and only if your developing tools, or some specific function would you ever need to use macro.

 

Fixed your code - note call execute pushes text out to after datastep ends:

data dtables;
input tablename $;
datalines;
flightnotbl
customertbl
countrytbl
run;
 
%MACRO createtbl(tname);
proc sql;
   create table _&tname
   (field1 char(4),
   field2 char(1),
   field3 char(3));
quit;
%MEND;
 
data _null_;
  set dtables;
  call execute('%createtbl('||strip(tablename)||');');
run;

View solution in original post

2 REPLIES 2
gamotte
Rhodochrosite | Level 12

Hi,

 

Your macro contains a proc sql. Calling it from a data step means executing the proc sql inside the datastep.

 

Also, when you create a macrovariable with a call symput, you cannot use the macrovariable in the data step that generates it.

 

So the solution would be to call the macro after the data step.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

 

Yes, you have fallen into the same problems as almost everyone else doing macro programming.  Let me explain.  Macro code is not executable - on its own is does nothing.  All its function is is to create text which then gets passed to the Base SAS compiler which is where code gets executed.  It does not interact with datastep, so you can't use it as part of a datastep - it only  creates text.  I have fixed your code given below, however I question why you are doing this at all.  You can simply create all three tables in one step:

data flightnotbl customertbl countrytbl;
  length field1 $4 field2 $1 field3 $2;
  if _n_ < 0 then output;
run;

No need for any of your code.  As a tip, learn Base SAS which is the programming language.  Forget macro language until you actually have a use for it - 99% of the time things can be done in Base SAS, and only if your developing tools, or some specific function would you ever need to use macro.

 

Fixed your code - note call execute pushes text out to after datastep ends:

data dtables;
input tablename $;
datalines;
flightnotbl
customertbl
countrytbl
run;
 
%MACRO createtbl(tname);
proc sql;
   create table _&tname
   (field1 char(4),
   field2 char(1),
   field3 char(3));
quit;
%MEND;
 
data _null_;
  set dtables;
  call execute('%createtbl('||strip(tablename)||');');
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 5707 views
  • 0 likes
  • 3 in conversation