<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: macro create table problem? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/macro-create-table-problem/m-p/318794#M69896</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yes, you have fallen into the same problems as almost everyone else doing macro programming. &amp;nbsp;Let me explain. &amp;nbsp;Macro code is not executable - on its own is does&amp;nbsp;&lt;STRONG&gt;nothing&lt;/STRONG&gt;. &amp;nbsp;All its function is is to create text which then gets passed to the Base SAS compiler which is where code gets executed. &amp;nbsp;It does not interact with datastep, so you can't use it as part of a datastep - it&amp;nbsp;&lt;STRONG&gt;only&amp;nbsp;&lt;/STRONG&gt; creates text. &amp;nbsp;I have fixed your code given below, however I question why you are doing this at all. &amp;nbsp;You can simply create all three tables in one step:&lt;/P&gt;
&lt;PRE&gt;data flightnotbl customertbl countrytbl;
  length field1 $4 field2 $1 field3 $2;
  if _n_ &amp;lt; 0 then output;
run;&lt;/PRE&gt;
&lt;P&gt;No need for any of your code. &amp;nbsp;As a tip, learn&amp;nbsp;&lt;STRONG&gt;Base SAS&lt;/STRONG&gt; which is the programming language. &amp;nbsp;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Fixed your code - note call execute pushes text out to after datastep ends:&lt;/P&gt;
&lt;PRE&gt;data dtables;
input tablename $;
datalines;
flightnotbl
customertbl
countrytbl
run;
 
%MACRO createtbl(tname);
proc sql;
   create table _&amp;amp;tname
   (field1 char(4),
   field2 char(1),
   field3 char(3));
quit;
%MEND;
 
data _null_;
  set dtables;
  call execute('%createtbl('||strip(tablename)||');');
run;&lt;/PRE&gt;</description>
    <pubDate>Wed, 14 Dec 2016 10:24:45 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2016-12-14T10:24:45Z</dc:date>
    <item>
      <title>macro create table problem?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-create-table-problem/m-p/318788#M69893</link>
      <description>&lt;P&gt;Dear all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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&amp;nbsp;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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data dtables;&lt;BR /&gt;input tablename $;&lt;BR /&gt;datalines;&lt;BR /&gt;flightnotbl&lt;BR /&gt;customertbl&lt;BR /&gt;countrytbl&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%MACRO createtbl(tname);&lt;BR /&gt;proc sql;&lt;BR /&gt;&amp;nbsp; &amp;nbsp;create table _&amp;amp;tname&lt;BR /&gt;&amp;nbsp; &amp;nbsp;(field1 char(4),&lt;BR /&gt;&amp;nbsp; &amp;nbsp;field2 char(1),&lt;BR /&gt;&amp;nbsp; &amp;nbsp;field3 char(3));&lt;BR /&gt;quit;&lt;BR /&gt;%MEND;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data _null_;&lt;BR /&gt;set dtables nobs=last;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; call symput('tblname',tablename);&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;%createtbl(&amp;amp;tblname);&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much!&lt;/P&gt;</description>
      <pubDate>Wed, 14 Dec 2016 09:52:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-create-table-problem/m-p/318788#M69893</guid>
      <dc:creator>Olime</dc:creator>
      <dc:date>2016-12-14T09:52:05Z</dc:date>
    </item>
    <item>
      <title>Re: macro create table problem?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-create-table-problem/m-p/318791#M69895</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your macro contains a proc sql. Calling it from a data step means executing the proc sql inside the datastep.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, when you create a macrovariable with a call symput, you cannot use the macrovariable in the data step that generates it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So the solution would be to call the macro after the data step.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Dec 2016 10:10:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-create-table-problem/m-p/318791#M69895</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2016-12-14T10:10:29Z</dc:date>
    </item>
    <item>
      <title>Re: macro create table problem?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-create-table-problem/m-p/318794#M69896</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yes, you have fallen into the same problems as almost everyone else doing macro programming. &amp;nbsp;Let me explain. &amp;nbsp;Macro code is not executable - on its own is does&amp;nbsp;&lt;STRONG&gt;nothing&lt;/STRONG&gt;. &amp;nbsp;All its function is is to create text which then gets passed to the Base SAS compiler which is where code gets executed. &amp;nbsp;It does not interact with datastep, so you can't use it as part of a datastep - it&amp;nbsp;&lt;STRONG&gt;only&amp;nbsp;&lt;/STRONG&gt; creates text. &amp;nbsp;I have fixed your code given below, however I question why you are doing this at all. &amp;nbsp;You can simply create all three tables in one step:&lt;/P&gt;
&lt;PRE&gt;data flightnotbl customertbl countrytbl;
  length field1 $4 field2 $1 field3 $2;
  if _n_ &amp;lt; 0 then output;
run;&lt;/PRE&gt;
&lt;P&gt;No need for any of your code. &amp;nbsp;As a tip, learn&amp;nbsp;&lt;STRONG&gt;Base SAS&lt;/STRONG&gt; which is the programming language. &amp;nbsp;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Fixed your code - note call execute pushes text out to after datastep ends:&lt;/P&gt;
&lt;PRE&gt;data dtables;
input tablename $;
datalines;
flightnotbl
customertbl
countrytbl
run;
 
%MACRO createtbl(tname);
proc sql;
   create table _&amp;amp;tname
   (field1 char(4),
   field2 char(1),
   field3 char(3));
quit;
%MEND;
 
data _null_;
  set dtables;
  call execute('%createtbl('||strip(tablename)||');');
run;&lt;/PRE&gt;</description>
      <pubDate>Wed, 14 Dec 2016 10:24:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-create-table-problem/m-p/318794#M69896</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-12-14T10:24:45Z</dc:date>
    </item>
  </channel>
</rss>

