<?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: Make code run on loop in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Make-code-run-on-loop/m-p/594689#M15685</link>
    <description>&lt;P&gt;First, use &lt;FONT face="courier new,courier"&gt;OPTIONS MPRINT;&lt;/FONT&gt; as the first line of the program and then run it again.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You will have to show us the LOG (the entire log, code and error message). To show us the log, click on the &lt;FONT face="courier new,courier"&gt;{i}&lt;/FONT&gt; button (&lt;STRONG&gt;DO NOT SKIP THIS STEP&lt;/STRONG&gt;) and paste the LOG into the window that appears; this will preserve the formatting of the log and make it readable.&lt;/P&gt;</description>
    <pubDate>Tue, 08 Oct 2019 10:49:47 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2019-10-08T10:49:47Z</dc:date>
    <item>
      <title>Make code run on loop</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Make-code-run-on-loop/m-p/594466#M15670</link>
      <description>&lt;P&gt;Hello everyone,&lt;/P&gt;&lt;P&gt;please i need help on this little problem.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a number of tables Under libref X, i created libref Y.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;create table Y.table-name as&lt;/P&gt;&lt;P&gt;select * from X.table-name&lt;/P&gt;&lt;P&gt;having Period=(max(Period));&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;all i want is to run this code on all the hundred of tables Under libref X.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i will be so thankfull.&lt;/P&gt;</description>
      <pubDate>Mon, 07 Oct 2019 14:10:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Make-code-run-on-loop/m-p/594466#M15670</guid>
      <dc:creator>aloou</dc:creator>
      <dc:date>2019-10-07T14:10:45Z</dc:date>
    </item>
    <item>
      <title>Re: Make code run on loop</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Make-code-run-on-loop/m-p/594469#M15671</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Untested Code */

proc sql noprint;
    select memname into :dsnames separated by ' ' from dictionary.tables;
quit;

%macro do_all;
    %do i=1 %to %sysfunc(countw(&amp;amp;dsnames));
        %let thisname=%scan(&amp;amp;dsnames,&amp;amp;i,%str( ));
        proc sql;
           create table y.&amp;amp;thisname as select * from x.&amp;amp;thisname
                having period=max(period));
         quit;
    %end;
%mend;
%do_all&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Limitation: the macro variable that is created cannot be more than around 65000 characters. If it is more that that, you would need to use CALL EXECUTE.&lt;/P&gt;</description>
      <pubDate>Mon, 07 Oct 2019 14:29:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Make-code-run-on-loop/m-p/594469#M15671</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-10-07T14:29:49Z</dc:date>
    </item>
    <item>
      <title>Re: Make code run on loop</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Make-code-run-on-loop/m-p/594471#M15672</link>
      <description>&lt;P&gt;First you need to get the list of the datasets (what you called tables) that you want to copy.&lt;/P&gt;
&lt;P&gt;There are many ways. An easy one is to use PROC CONTENTS.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc contents data=X._all_ noprint out=contents; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;For your code to work the dataset must contain the variable PERIOD. So let's limit the list to just those datasets. The name of the dataset is in the variable MEMNAME and the name of the variable is in the variable NAME.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data list;
  set contents;
  where upcase(name)='PERIOD';
  keep memname ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now you just need to use that list to generate a separate SQL statement for each dataset.&amp;nbsp; You could try to use macro code or CALL EXECUTE. Personally I prefer to use PUT statement to write the code to a file.&amp;nbsp; That is easier to debug since you can look at the file and make sure you have generated the right code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename code temp;
data _null_;
  set list;
  file code;
  put 'create table Y.' memname 'as select * from X.' memname 
      'having Period=(max(Period));'
  ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then just use %INCLUDE to run the code in the file.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
%include code;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 07 Oct 2019 14:45:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Make-code-run-on-loop/m-p/594471#M15672</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-10-07T14:45:00Z</dc:date>
    </item>
    <item>
      <title>Re: Make code run on loop</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Make-code-run-on-loop/m-p/594686#M15684</link>
      <description>&lt;P&gt;Dear Sir,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i have tried your code and partly it is working, however the problem is that dsnames is getting all table names in one line ,&lt;/P&gt;&lt;P&gt;so when the proc sql work it will take dsnames as a whole name and won't consider them as separated names&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you so much&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2019 10:35:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Make-code-run-on-loop/m-p/594686#M15684</guid>
      <dc:creator>aloou</dc:creator>
      <dc:date>2019-10-08T10:35:02Z</dc:date>
    </item>
    <item>
      <title>Re: Make code run on loop</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Make-code-run-on-loop/m-p/594689#M15685</link>
      <description>&lt;P&gt;First, use &lt;FONT face="courier new,courier"&gt;OPTIONS MPRINT;&lt;/FONT&gt; as the first line of the program and then run it again.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You will have to show us the LOG (the entire log, code and error message). To show us the log, click on the &lt;FONT face="courier new,courier"&gt;{i}&lt;/FONT&gt; button (&lt;STRONG&gt;DO NOT SKIP THIS STEP&lt;/STRONG&gt;) and paste the LOG into the window that appears; this will preserve the formatting of the log and make it readable.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2019 10:49:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Make-code-run-on-loop/m-p/594689#M15685</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-10-08T10:49:47Z</dc:date>
    </item>
    <item>
      <title>Re: Make code run on loop</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Make-code-run-on-loop/m-p/594796#M15695</link>
      <description>&lt;P&gt;i brought some changes to the code that you gave me and now it is working well.&lt;/P&gt;&lt;P&gt;i am so grateful for your help.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2019 16:13:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Make-code-run-on-loop/m-p/594796#M15695</guid>
      <dc:creator>aloou</dc:creator>
      <dc:date>2019-10-08T16:13:16Z</dc:date>
    </item>
  </channel>
</rss>

