<?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 Macro not working in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Macro-not-working/m-p/858169#M37897</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I am trying to create a list of table names so that I can make a do-until loop that goes through each table and creates a new table that separates specific years into new tables. The imports work, the list works, and both proc sqls work until I added the do until loop. My goal is: for every table, create a new table by year until there are no more tables left to iterate. This is what I have so far:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(There probably is a better way to make a list for the existing tables, but with the name it should still work).&lt;/P&gt;&lt;P&gt;data work.tables;&lt;BR /&gt;input table_name $;&lt;BR /&gt;cards;&lt;BR /&gt;data1&lt;BR /&gt;data2&lt;BR /&gt;data3&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;select distinct table_name into: table_list separated by ','&lt;BR /&gt;from tables;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro loop(current_table_list = table_list, obs = 3);&lt;BR /&gt;%local i next_table;&lt;BR /&gt;%let i=1;&lt;BR /&gt;%let next_table = %scan(&amp;amp;current_table_list, &amp;amp;i, %str());&lt;BR /&gt;%do %until(%superq(next_table) eq %str());&lt;BR /&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;select distinct put(year, 4.) into :yearList separated by ','&lt;BR /&gt;from &amp;amp;next_table.&lt;BR /&gt;where year &amp;gt;= 1940 and year &amp;lt;= 1980;&lt;BR /&gt;quit;&lt;BR /&gt;&lt;BR /&gt;%let column = put(year, 4.);&lt;BR /&gt;%let table = work.&amp;amp;next_table.;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql noprint;&lt;BR /&gt;select distinct&lt;BR /&gt;cat("DATA year_", &amp;amp;next_table., compress(&amp;amp;column.,,'kad'), "; set &amp;amp;table.&lt;BR /&gt;(where=(&amp;amp;column.='", &amp;amp;column., "')); run;") length = 500 into :allsteps&lt;BR /&gt;separated by ';' from &amp;amp;table. where year &amp;gt;= 1940 and year &amp;lt;= 1980;&lt;BR /&gt;quit;&lt;BR /&gt;&lt;BR /&gt;&amp;amp;allsteps.;&lt;BR /&gt;&lt;BR /&gt;%let i = %eval(&amp;amp;i + 1);&lt;BR /&gt;%let next_table = %scan(&amp;amp;current_table_list, &amp;amp;i, %str());&lt;BR /&gt;%end;&lt;BR /&gt;%mend;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the output I get&amp;nbsp;&lt;SPAN&gt;OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;No output or results.&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 10 Feb 2023 00:23:59 GMT</pubDate>
    <dc:creator>hjkaufma</dc:creator>
    <dc:date>2023-02-10T00:23:59Z</dc:date>
    <item>
      <title>Macro not working</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Macro-not-working/m-p/858169#M37897</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I am trying to create a list of table names so that I can make a do-until loop that goes through each table and creates a new table that separates specific years into new tables. The imports work, the list works, and both proc sqls work until I added the do until loop. My goal is: for every table, create a new table by year until there are no more tables left to iterate. This is what I have so far:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(There probably is a better way to make a list for the existing tables, but with the name it should still work).&lt;/P&gt;&lt;P&gt;data work.tables;&lt;BR /&gt;input table_name $;&lt;BR /&gt;cards;&lt;BR /&gt;data1&lt;BR /&gt;data2&lt;BR /&gt;data3&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;select distinct table_name into: table_list separated by ','&lt;BR /&gt;from tables;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro loop(current_table_list = table_list, obs = 3);&lt;BR /&gt;%local i next_table;&lt;BR /&gt;%let i=1;&lt;BR /&gt;%let next_table = %scan(&amp;amp;current_table_list, &amp;amp;i, %str());&lt;BR /&gt;%do %until(%superq(next_table) eq %str());&lt;BR /&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;select distinct put(year, 4.) into :yearList separated by ','&lt;BR /&gt;from &amp;amp;next_table.&lt;BR /&gt;where year &amp;gt;= 1940 and year &amp;lt;= 1980;&lt;BR /&gt;quit;&lt;BR /&gt;&lt;BR /&gt;%let column = put(year, 4.);&lt;BR /&gt;%let table = work.&amp;amp;next_table.;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql noprint;&lt;BR /&gt;select distinct&lt;BR /&gt;cat("DATA year_", &amp;amp;next_table., compress(&amp;amp;column.,,'kad'), "; set &amp;amp;table.&lt;BR /&gt;(where=(&amp;amp;column.='", &amp;amp;column., "')); run;") length = 500 into :allsteps&lt;BR /&gt;separated by ';' from &amp;amp;table. where year &amp;gt;= 1940 and year &amp;lt;= 1980;&lt;BR /&gt;quit;&lt;BR /&gt;&lt;BR /&gt;&amp;amp;allsteps.;&lt;BR /&gt;&lt;BR /&gt;%let i = %eval(&amp;amp;i + 1);&lt;BR /&gt;%let next_table = %scan(&amp;amp;current_table_list, &amp;amp;i, %str());&lt;BR /&gt;%end;&lt;BR /&gt;%mend;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the output I get&amp;nbsp;&lt;SPAN&gt;OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;No output or results.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Feb 2023 00:23:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Macro-not-working/m-p/858169#M37897</guid>
      <dc:creator>hjkaufma</dc:creator>
      <dc:date>2023-02-10T00:23:59Z</dc:date>
    </item>
    <item>
      <title>Re: Macro not working</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Macro-not-working/m-p/858187#M37898</link>
      <description>Not sure this solves everything, but it's a good start.&lt;BR /&gt;&lt;BR /&gt;You define a macro %loop, but you never execute the macro.  Where's the %loop statement?&lt;BR /&gt;&lt;BR /&gt;Adding it may generate a bunch of errors, so the debugging is just beginning.  For example, macro language will not execute the PUT function.  (Look at the statement assigning a value to &amp;amp;column.)  At any rate, start by actually executing your macro and see where that leads.&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Fri, 10 Feb 2023 04:23:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Macro-not-working/m-p/858187#M37898</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2023-02-10T04:23:40Z</dc:date>
    </item>
    <item>
      <title>Re: Macro not working</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Macro-not-working/m-p/858665#M37918</link>
      <description>&lt;P&gt;The default note about creating macros: Start with working code that solves the problem for one year. Then think about macros.&lt;/P&gt;
&lt;P&gt;The default note about asking for help: Post the log. The complete log. And, if macros are used, always activate the options mprint and mlogic, so that the log show what the macro did actually.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why do you have to create that many datasets at all?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Feb 2023 06:08:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Macro-not-working/m-p/858665#M37918</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2023-02-14T06:08:42Z</dc:date>
    </item>
    <item>
      <title>Re: Macro not working</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Macro-not-working/m-p/858668#M37919</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/438988"&gt;@hjkaufma&lt;/a&gt;&amp;nbsp;wrote:
&lt;P&gt;This is the output I get&amp;nbsp;&lt;SPAN&gt;OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;No output or results.&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Hello hjkaufma!&lt;/P&gt;
&lt;P&gt;There is defenitely more in the log ... you should post that to help us helping you (as&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt; has already pointed out).&lt;/P&gt;
&lt;P&gt;You should see the sql statements being executed and the macro code being digested (but not run, as&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&amp;nbsp;has detailed).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can only agree ... you are not executing the macro, so whatever you see as/in tables are probably results from previous runs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;--FJa&lt;/P&gt;
&lt;HR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Feb 2023 06:39:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Macro-not-working/m-p/858668#M37919</guid>
      <dc:creator>fja</dc:creator>
      <dc:date>2023-02-14T06:39:47Z</dc:date>
    </item>
    <item>
      <title>Re: Macro not working</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Macro-not-working/m-p/858673#M37920</link>
      <description>&lt;P&gt;When it comes to getting lists of table names then you might consider the Dictionary tables, especially if the objective is to work with "every set" in a library.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You get the library name and data set name similar to&lt;/P&gt;
&lt;PRE&gt;Proc sql;
   create table setnames as
   select libname, memname
   from dictionary.tables
   where libname='MYLIB' and memtype='Data'
   ;
run;&lt;/PRE&gt;
&lt;P&gt;You should investigate CALL EXECUTE to write code with stuff in a data set. You use the variable names holding the bit to write the syntax. Call Execute places code into stack that executes after a data step ends. That is basically what your&lt;/P&gt;
&lt;PRE&gt;select distinct
cat("DATA year_", &amp;amp;next_table., compress(&amp;amp;column.,,'kad'), "; set &amp;amp;table.
(where=(&amp;amp;column.='", &amp;amp;column., "')); run;") &lt;/PRE&gt;
&lt;P&gt;does if I understand what you are attempting.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I suspect that you have a problem here:&lt;/P&gt;
&lt;PRE&gt;(where=(&amp;amp;column.='", &amp;amp;column., "'))&lt;/PRE&gt;
&lt;P&gt;If &amp;amp;column is put(year, 4.) . The macro definition means that is the text&lt;/P&gt;
&lt;P&gt;then the above resolves to &lt;/P&gt;
&lt;PRE&gt;(where=(put(year,4.)='", &amp;amp;column., "'))
&lt;/PRE&gt;
&lt;P&gt;and even a later resolution of &amp;amp;column is going to quotes, commas and spaces and so is very unlikely to ever result in the Where returning anything.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Once you have set candidate years instead of writing a bunch of where dataset option data steps, that will require reading each record from each data set multiple times you would be better off, if this is really needed, to write one data step that look something like&lt;/P&gt;
&lt;PRE&gt;data  table1940
         table1941
         table1942
...
         table1980;
    set table;
    select year;
        when(1940) output table1940;
        when(1941) output table1941;
        when(1942) output table1942;
...
        when(1980) output table1980;
        otherwise;
     end;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a &lt;STRONG&gt;very&lt;/STRONG&gt; hard time seeing where creating all of these data sets is an advantage.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Feb 2023 07:51:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Macro-not-working/m-p/858673#M37920</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-02-14T07:51:59Z</dc:date>
    </item>
  </channel>
</rss>

