<?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 Dynamically create tables based on string values in macro variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-create-tables-based-on-string-values-in-macro/m-p/689679#M209679</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm attempting to create a number of tables with a macro as such, however it doesn't produce any results:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro my_loop(table_nme);
proc sql;
  select distinct auth_cd into :codes separated by ' ' from pth.code_names;
quit;

%do i=1 %to %sysfunc(countw(&amp;amp;codes));
  %let my_codes=%scan(&amp;amp;codes,&amp;amp;i,%str());
  proc sql;
  create table &amp;amp;table_nme as
  select distinct  
          auth_id, 
          prd_rate
  from aop.tracked_codes
  where auth_id = &amp;amp;my_codes;
quit;
%end;
%mend;
%my_loop(first_table);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;So basically the goal is to create one table for each value in the &amp;amp;my_codes list, so I am trying to loop through each of the values in the list and create tables for each.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 07 Oct 2020 19:33:31 GMT</pubDate>
    <dc:creator>PegaZeus</dc:creator>
    <dc:date>2020-10-07T19:33:31Z</dc:date>
    <item>
      <title>Dynamically create tables based on string values in macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-create-tables-based-on-string-values-in-macro/m-p/689679#M209679</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm attempting to create a number of tables with a macro as such, however it doesn't produce any results:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro my_loop(table_nme);
proc sql;
  select distinct auth_cd into :codes separated by ' ' from pth.code_names;
quit;

%do i=1 %to %sysfunc(countw(&amp;amp;codes));
  %let my_codes=%scan(&amp;amp;codes,&amp;amp;i,%str());
  proc sql;
  create table &amp;amp;table_nme as
  select distinct  
          auth_id, 
          prd_rate
  from aop.tracked_codes
  where auth_id = &amp;amp;my_codes;
quit;
%end;
%mend;
%my_loop(first_table);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;So basically the goal is to create one table for each value in the &amp;amp;my_codes list, so I am trying to loop through each of the values in the list and create tables for each.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Oct 2020 19:33:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-create-tables-based-on-string-values-in-macro/m-p/689679#M209679</guid>
      <dc:creator>PegaZeus</dc:creator>
      <dc:date>2020-10-07T19:33:31Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically create tables based on string values in macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-create-tables-based-on-string-values-in-macro/m-p/689709#M209683</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/320380"&gt;@PegaZeus&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm attempting to create a number of tables with a macro as such, however it doesn't produce any results:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro my_loop(table_nme);
proc sql;
  select distinct auth_cd into :codes separated by ' ' from pth.code_names;
quit;

%do i=1 %to %sysfunc(countw(&amp;amp;codes));
  %let my_codes=%scan(&amp;amp;codes,&amp;amp;i,%str());
  proc sql;
  create table &amp;amp;table_nme as
  select distinct  
          auth_id, 
          prd_rate
  from aop.tracked_codes
  where auth_id = &amp;amp;my_codes;
quit;
%end;
%mend;
%my_loop(first_table);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So basically the goal is to create one table for each value in the &amp;amp;my_codes list, so I am trying to loop through each of the values in the list and create tables for each.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Since your example uses the same name value in the create table clause then you overwrite the table each time you loop.&lt;/P&gt;
&lt;P&gt;You don't provide any example of what the MY_codes values may be so we can't tell if they could be used for table names or not due to length or invalid characters.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;create table &amp;amp;table_nme.&amp;amp;i as&lt;/LI-CODE&gt;
&lt;P&gt;would place a suffix of the loop counter into the name.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You likely also need to have&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;  where auth_id = "&amp;amp;my_codes";&lt;/LI-CODE&gt;
&lt;P&gt;so the value of auth_id is compared to a character value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When your code creates errors, which I strongly suspect yours did, then copy from the log the submitted code and any messages, then paste into a code box on the forum opened with the &amp;lt;/&amp;gt; icon.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When debugging macros you will want to learn about the options involved for creating a more complete log.&lt;/P&gt;
&lt;P&gt;Start with placing:&lt;/P&gt;
&lt;P&gt;Options mprint;&lt;/P&gt;
&lt;P&gt;before the macro submission line..&lt;/P&gt;
&lt;P&gt;Then turn off by placing after&lt;/P&gt;
&lt;P&gt;Options nomprint;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Other useful options are SYMBOLGEN, if it have issues with combining things to make variable names and such, or MLOGIC if the concern seems to be the result of comparisons in the macro language.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hint: Did you have a Proc SQL code that worked for a single value before attempting this macro? If not, that is always the first step. Get non-macro code working, Then make changes to incorporate the macro elements.&lt;/P&gt;</description>
      <pubDate>Wed, 07 Oct 2020 20:00:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-create-tables-based-on-string-values-in-macro/m-p/689709#M209683</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-10-07T20:00:22Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically create tables based on string values in macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-create-tables-based-on-string-values-in-macro/m-p/690083#M209871</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;&amp;nbsp;Thank you for these details. Okay so, this is the actual code I'm using (I had mocked up that example to try and make it simpler) as well as the log. What I'm running into I think is on that where filter as well as the create table, it puts in the whole list of strings.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro client_loop();
proc sql;
  select distinct clnt_cd into :clients separated by ' ' from com.triessent_fee_schedule;
quit;

%do i=1 %to %sysfunc(countw(&amp;amp;clients));
  %let my_client=%scan(&amp;amp;clients,&amp;amp;i,%str());
  proc sql;
  create table &amp;amp;my_client as
  select distinct  
          t1.ndc_num as ndc, 
          t1.exclrate as exclusive_rate format=percent9.4,
		  lmtddistr as ldd_flag, 
		  (0) as quality_network_rate, 
          (0) as non_exclusive_rate
  from com.specialty_fee_schedule t1
  where t1.clnt_cd = "&amp;amp;my_client" and t1.exclrate &amp;gt; 0 and fee_schedule_dml_based_flag="YES";
quit;
%end;
%mend;
options mprint;
%client_loop();&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;You'll notice that I put &amp;amp;my_client in the create table. The values in the client list are things like: BCBSMN, BCBSIL, BCBSFL...they are not too long for a table name and there are 22 of them.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is what I'm running into in the log from running the above code:&lt;/P&gt;&lt;PRE&gt;NOTE: Implicit SQL:  select distinct TXT_1.Clnt_CD from TRIESSENT_FEE_SCHEDULE TXT_1
WARNING: No output destinations active.
MPRINT(CLIENT_LOOP):   quit;
NOTE: PROCEDURE SQL used (Total process time):
2                                                          The SAS System                            11:15 Thursday, October 8, 2020

      real time           4.08 seconds
      user cpu time       0.01 seconds
      system cpu time     0.00 seconds
      memory              5261.00k
      OS Memory           20900.00k
      Timestamp           10/08/2020 11:16:06 AM
      Step Count                        2  Switch Count  58
      Page Faults                       1
      Page Reclaims                     204
      Page Swaps                        0
      Voluntary Context Switches        201
      Involuntary Context Switches      2
      Block Input Operations            0
      Block Output Operations           0
      

MPRINT(CLIENT_LOOP):   proc sql;
NOTE: Line generated by the macro variable "MY_CLIENT".
37          BCBSA BCBSAL BCBSFL BCBSIL BCBSKS BCBSMN BCBSMT BCBSNC BCBSND BCBSNE BCBSNJ BCBSNM BCBSOK BCBSRI BCBSTX BCBSTX09 BCBSWY
                  ______
                  22
                  76
37       ! BOEING CBC CHP PRIME REGENCE
MPRINT(CLIENT_LOOP):   create table BCBSA BCBSAL BCBSFL BCBSIL BCBSKS BCBSMN BCBSMT BCBSNC BCBSND BCBSNE BCBSNJ BCBSNM BCBSOK 
BCBSRI BCBSTX BCBSTX09 BCBSWY BOEING CBC CHP PRIME REGENCE as select distinct t1.ndc_num as ndc, t1.exclrate as exclusive_rate 
format=percent9.4, lmtddistr as ldd_flag, (0) as quality_network_rate, (0) as non_exclusive_rate from com.specialty_fee_schedule t1 
where t1.clnt_cd = "BCBSA BCBSAL BCBSFL BCBSIL BCBSKS BCBSMN BCBSMT BCBSNC BCBSND BCBSNE BCBSNJ BCBSNM BCBSOK BCBSRI BCBSTX 
BCBSTX09 BCBSWY BOEING CBC CHP PRIME REGENCE" and t1.exclrate &amp;gt; 0 and fee_schedule_dml_based_flag="YES";
ERROR 22-322: Syntax error, expecting one of the following: (, '.', AS, LIKE.  

ERROR 76-322: Syntax error, statement will be ignored.

NOTE: PROC SQL set option NOEXEC and will continue to check the syntax of statements.
MPRINT(CLIENT_LOOP):   quit;
NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.00 seconds
      user cpu time       0.00 seconds
      system cpu time     0.00 seconds
      memory              46.71k
      OS Memory           16288.00k
      Timestamp           10/08/2020 11:16:07 AM
      Step Count                        3  Switch Count  52
      Page Faults                       1
      Page Reclaims                     12
      Page Swaps                        0
      Voluntary Context Switches        154
      Involuntary Context Switches      2
      Block Input Operations            0
      Block Output Operations           0&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Oct 2020 16:19:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-create-tables-based-on-string-values-in-macro/m-p/690083#M209871</guid>
      <dc:creator>PegaZeus</dc:creator>
      <dc:date>2020-10-08T16:19:03Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically create tables based on string values in macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-create-tables-based-on-string-values-in-macro/m-p/690088#M209876</link>
      <description>&lt;P&gt;Remove the&lt;/P&gt;
&lt;PRE&gt;,%str()&lt;/PRE&gt;
&lt;P&gt;from the %SCAN call.&lt;/P&gt;</description>
      <pubDate>Thu, 08 Oct 2020 16:28:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-create-tables-based-on-string-values-in-macro/m-p/690088#M209876</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-10-08T16:28:53Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically create tables based on string values in macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-create-tables-based-on-string-values-in-macro/m-p/690096#M209884</link>
      <description>&lt;P&gt;The %scan creating your my_client macro variable is having problems because you have told it to use an zero length string value as the delimiter. The space is a native delimiter for %scan and does not need to be provided. But you did not use a space the %str() has no character inside the quotes, leading to unexpected results. I am not sure why your result has all of the values in the result instead of an empty string, perhaps an unusual scope issue.&lt;/P&gt;
&lt;P&gt;Check the results running this code:&lt;/P&gt;
&lt;PRE&gt;%let clients= BCBSA BCBSAL BCBSFL BCBSIL BCBSKS BCBSMN;

%let my_client = %scan(&amp;amp;clients.,2);  /*&amp;lt;= use default delimiters for the %scan function */
%put second my_client is &amp;amp;my_client.;

%let my_client2 = %scan(&amp;amp;clients.,2,%str( ));
%put second my_client2 is &amp;amp;my_client2.;

%let my_client3 = %scan(&amp;amp;clients.,2,%str());
%put second my_client3 is &amp;amp;my_client3.;
&lt;/PRE&gt;
&lt;P&gt;From the documentation for %scan&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;DIV class="xis-eDocBody"&gt;
&lt;DIV class="xis-refDictEntry"&gt;
&lt;DIV class="xis-details"&gt;
&lt;DIV id="p1ak31rvg5xz6vn1hat80i9wbqpq" class="xis-topicContent"&gt;
&lt;DIV id="p0i0nkt0ue5msun1w1g29j9kxfhp" class="xis-paragraph"&gt;If you use the &lt;FONT style="background-color: #fcdec0;"&gt;%SCAN&lt;/FONT&gt; function with only two arguments, then the default delimiters depend on whether your computer uses ASCII or EBCDIC characters.
&lt;DIV class="xis-listUnordered"&gt;
&lt;UL&gt;
&lt;LI&gt;
&lt;DIV id="n0igc9w9lmxjpyn14wpzkwf0immo" class="xis-item"&gt;
&lt;DIV id="p0uu756m3la9cnn1wgxnlzeet306" class="xis-paraSimpleFirst"&gt;If your computer uses ASCII characters, then the default delimiters are as follows:&lt;/DIV&gt;
&lt;DIV id="p1ak0140p505cpn1x57nl17u3kjp" class="xis-paraSimple"&gt;blank ! $ % &amp;amp; ( ) * + , - . / ; &amp;lt; ^¦&lt;/DIV&gt;
&lt;DIV id="n016tiy2bkbxs3n1rly8huot7me3" class="xis-paraSimple"&gt;In ASCII environments that do not contain the ^ character, the &lt;FONT style="background-color: #fcdec0;"&gt;%SCAN&lt;/FONT&gt; function uses the ~ character instead.&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV id="n12s457yewkppvn11lgzakws7yvk" class="xis-item"&gt;
&lt;DIV id="n0f0dfi1kw4h9an1mhmar7udzmzq" class="xis-paraSimpleFirst"&gt;If your computer uses EBCDIC characters, then the default delimiters are as follows:&lt;/DIV&gt;
&lt;DIV id="p0a8dz8q4c3k1un1kjcuho4kjamc" class="xis-paraSimple"&gt;blank ! $ % &amp;amp; ( ) * + , - . / ; &amp;lt; ¬ | ¢¦&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/LI&gt;
&lt;/UL&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So there was no need to attempt to provide a specific delimiter in this case.&lt;/P&gt;</description>
      <pubDate>Thu, 08 Oct 2020 16:43:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-create-tables-based-on-string-values-in-macro/m-p/690096#M209884</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-10-08T16:43:38Z</dc:date>
    </item>
  </channel>
</rss>

