title: Automated Sorting in SAS
body: <p>I have lots of tables which I would like to sort with Proc Sort. (The names of the tables are written in a text file.) To avoid repeating the same code all over again I have tried creating a macro that would import the text file, create an array consisting of those table names and finally sort all the tables. However, I came across a few problems. In Python, I would easily be able to loop through an array. But in SAS, I am not sure how to do it.</p>
  
  <pre><code>%MACRO SORT_TABLES();
  
  PROC IMPORT 
    DATAFILE = 'TABLES_LIST.txt'
    OUT = WORK.TABLES_LIST (RENAME = VAR1 = TABLE_NAME)
    DBMS = TAB
    REPLACE;
    GETNAMES = NO;
  QUIT;
  
  /* GET THE LIST OF TABLE NAMES: */
  PROC SQL NOPRINT;
    SELECT 
      DISTINCT TABLE_NAME
      INTO :TABLEVAR1 - :TABLEVAR&amp;SYSMAXLONG
    FROM 
      WORK.TABLES_LIST;
    QUIT;
  
  DATA _NULL_;
    ARRAY TABLE_NAMES $ &amp;TABLEVAR1 - &amp;TABLEVAR&amp;SYSMAXLONG;
  RUN;
  
  %DO %OVER TABLE_NAMES
    PROC SORT 
      DATA = &amp;TABLEVAR1 /* how can I iterate here???? */ 
      OUT = 'WORK.'||&amp;TABLEVAR1;
      BY A B C;
    QUIT; 
  %END;
  
  %MEND;
  </code></pre>