<?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 Question in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Macro-Question/m-p/622606#M19802</link>
    <description>&lt;P&gt;I have below macro in my SAS code. I have total of 2,000 customers that I run a report for. I have assigned them values from 1,000 to 3,000. When I run the code, at the end, it creates a table called ‘file_&amp;amp;i_allyrs’. I do not get data for same number of customers every time I run it. Sometimes, I get this table for only 1,000 customers and sometimes for 1,800 customers. The ‘i’ below is customer number assigned between 1000 and 3000.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;%macro&lt;/STRONG&gt; &lt;STRONG&gt;&lt;EM&gt;Read_ALL&lt;/EM&gt;&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;%do i = &amp;amp;beg %to &amp;amp;end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data cont_&amp;amp;i;&lt;/P&gt;&lt;P&gt;set file_&amp;amp;i_allyrs;&lt;/P&gt;&lt;P&gt;cust = “&amp;amp;i”;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/* appending rest of the file_&amp;amp;i._allyrs tables */&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc append base = all_files_allyears&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; data = cont_&amp;amp;i force;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %end;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;%mend&lt;/STRONG&gt; ;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;%macro&lt;/STRONG&gt; byyr(beg, end);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %&lt;STRONG&gt;&lt;EM&gt;Read_ALL&lt;/EM&gt;&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;%mend&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;%&lt;STRONG&gt;&lt;EM&gt;byyr&lt;/EM&gt;&lt;/STRONG&gt;(1000, 3000);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, first it reads 'file_1000_allyrs' table. If this file does not exist, it gives an error and goes to read 'file_1001_allyrs' table..and so on.&lt;/P&gt;&lt;P&gt;Every time the file does not exist, it gives an error. Below is part of the error it gives.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ERROR: File FILES_1000_ALLYRS.DATA does not exist.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like the macro to skip the file it does not exist OR I would like to stop getting an error if the file does not exist.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How do accomplish that?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you in advance&lt;/P&gt;</description>
    <pubDate>Thu, 06 Feb 2020 04:54:11 GMT</pubDate>
    <dc:creator>SASMom2</dc:creator>
    <dc:date>2020-02-06T04:54:11Z</dc:date>
    <item>
      <title>Macro Question</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Macro-Question/m-p/622606#M19802</link>
      <description>&lt;P&gt;I have below macro in my SAS code. I have total of 2,000 customers that I run a report for. I have assigned them values from 1,000 to 3,000. When I run the code, at the end, it creates a table called ‘file_&amp;amp;i_allyrs’. I do not get data for same number of customers every time I run it. Sometimes, I get this table for only 1,000 customers and sometimes for 1,800 customers. The ‘i’ below is customer number assigned between 1000 and 3000.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;%macro&lt;/STRONG&gt; &lt;STRONG&gt;&lt;EM&gt;Read_ALL&lt;/EM&gt;&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;%do i = &amp;amp;beg %to &amp;amp;end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data cont_&amp;amp;i;&lt;/P&gt;&lt;P&gt;set file_&amp;amp;i_allyrs;&lt;/P&gt;&lt;P&gt;cust = “&amp;amp;i”;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/* appending rest of the file_&amp;amp;i._allyrs tables */&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc append base = all_files_allyears&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; data = cont_&amp;amp;i force;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %end;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;%mend&lt;/STRONG&gt; ;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;%macro&lt;/STRONG&gt; byyr(beg, end);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %&lt;STRONG&gt;&lt;EM&gt;Read_ALL&lt;/EM&gt;&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;%mend&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;%&lt;STRONG&gt;&lt;EM&gt;byyr&lt;/EM&gt;&lt;/STRONG&gt;(1000, 3000);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, first it reads 'file_1000_allyrs' table. If this file does not exist, it gives an error and goes to read 'file_1001_allyrs' table..and so on.&lt;/P&gt;&lt;P&gt;Every time the file does not exist, it gives an error. Below is part of the error it gives.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ERROR: File FILES_1000_ALLYRS.DATA does not exist.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like the macro to skip the file it does not exist OR I would like to stop getting an error if the file does not exist.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How do accomplish that?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you in advance&lt;/P&gt;</description>
      <pubDate>Thu, 06 Feb 2020 04:54:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Macro-Question/m-p/622606#M19802</guid>
      <dc:creator>SASMom2</dc:creator>
      <dc:date>2020-02-06T04:54:11Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Question</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Macro-Question/m-p/622610#M19803</link>
      <description>&lt;P&gt;If the numbering of the files would be at the end then you could reference the files simply by using the colon modifier like:&lt;/P&gt;
&lt;P&gt;set file_allyrs_:;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming that there could be a table starting with name FILE_... which you don't want included, things become slightly harder.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data file_1000_allyrs file_1555_allyrs file_2987_allyrs file_2987_notallyrs;
  set sashelp.class;
run;

proc sql noprint;
  select catx('.',libname,memname) into :tbl1-:tbl9999
  from dictionary.tables
  where libname='WORK' and memtype='DATA'
    and prxmatch('/^file_\d{4}_allyrs$/oi',strip(memname))&amp;gt;0
  ;
quit;
%macro tables();
  %do i=1 %to &amp;amp;sqlobs;
    &amp;amp;&amp;amp;tbl&amp;amp;i
  %end;
%mend;

data all_files_allyears;
  length _curr_file $32. cust $4;
  retain cust;
  set %tables() indsname=_curr_file;
  if lag(_curr_file) ne _curr_file then 
    cust = compress(_curr_file,,'kd');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;....and I can't help myself to also write: You probably should revisit your upstream processing so that you don't end up with multiple tables in first place. Given the power of SAS By-Group processing there is almost never a good reason to split up tables the way it's done here.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Using your code here how this could work:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%macro Read_ALL;
  %do i = &amp;amp;beg %to &amp;amp;end;
    %if %sysfunc(exist(file_&amp;amp;i._allyrs,data)) %then
      %do;
        data cont_&amp;amp;i;
          set file_&amp;amp;i._allyrs;
          cust = "&amp;amp;i";
        run;

        /* appending rest of the file_&amp;amp;i._allyrs tables */
        proc append base = all_files_allyears
          data = cont_&amp;amp;i force;
        run;
      %end;
  %end;
%mend;

%macro byyr(beg, end);
  %Read_ALL;
%mend;

%byyr(1000, 3000);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Feb 2020 23:18:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Macro-Question/m-p/622610#M19803</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-02-06T23:18:01Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Question</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Macro-Question/m-p/622611#M19804</link>
      <description>&lt;P&gt;Add function EXIST() to judge if it existed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%macro Read_ALL;

%do i = &amp;amp;beg %to &amp;amp;end;

%if %sysfunc(exist(file_&amp;amp;i_allyrs)) %then %do;
data cont_&amp;amp;i;
set file_&amp;amp;i_allyrs;
cust = “&amp;amp;i”;
run;
proc append base = all_files_allyears
           data = cont_&amp;amp;i force;
run;
%end;

       %end;

%mend ;&lt;/PRE&gt;</description>
      <pubDate>Thu, 06 Feb 2020 06:03:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Macro-Question/m-p/622611#M19804</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-02-06T06:03:54Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Question</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Macro-Question/m-p/622874#M19859</link>
      <description>&lt;P&gt;Thank you for your quick reply. That worked!&lt;/P&gt;</description>
      <pubDate>Thu, 06 Feb 2020 21:18:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Macro-Question/m-p/622874#M19859</guid>
      <dc:creator>SASMom2</dc:creator>
      <dc:date>2020-02-06T21:18:43Z</dc:date>
    </item>
  </channel>
</rss>

