title: SAS - Using variable in table name
body: <p>Relatively new to SAS but I have come across an unusual issue. I use some proc sql statements to dynamically create a variable based on the latest month. The tables I want to retrieve from have a month name in the middle of them and I want to loop through. 
  E.g. LIBRARY.TABLE_JAN17_ALL
       LIBRARY.TABLE_DEC16_ALL etc.</p>
  
  <p><strong>Example Code:</strong></p>
  
  <pre><code> DATA qtrMonth;
   INPUT vDay vMonth vMonthName $;
   DATALINES;
  31 01 JAN
  28 02 FEB
  31 03 MAR
  30 04 APR
  31 05 MAY
  30 06 JUN
  31 07 JUL
  31 08 AUG
  30 09 SEP
  31 10 OCT
  30 11 NOV
  31 12 DEC
  ;
  
  DATA year;
  INPUT vYear ;
  DATALINES;
  14
  15
  16
  17
  ;
  run;
  
  /* Next step is to do a cartesian join to populate
     all possible Month &amp; Year combos in the dataset  */
  PROC SQL;
      create table popCalendar as
      SELECT 
      mdy(a.vMonth,a.vDay, b.vYear) as MyDate format  DATE9.
      ,CAT(TRIM(a.vMonthName), b.vYear) as MonthName
      FROM qtrMonth a
      CROSS JOIN year b
      ;
  quit;
  
  PROC SQL;
  create table tmpMax as 
  Select Max(MyDate) as MaxDate
  FROM popCalendar
  WHERE MyDate &lt; today();
  
  /*select max monthName into vMonth variable */
  SELECT trim(MonthName) into :vMonth
  FROM popCalendar a
  inner join tmpMAX b on a.MyDate = b.MaxDate;
  quit;
  
  /*Select from table using variable*/
  proc sql;
  create table abc as 
  select * from LIBRARY.TABLE_&amp;vMonth._ALL;
  </code></pre>
  
  <p>Previously, I used this logic for table names where the ending was the MonthName i.e. LIBRARY_TABLEACC_JAN17 -> LIBRARY.TABLEACC_&amp;vMonth.
  I had no issues with this but now that the variable is in the middle of the table name I get an error. 
  <strong><em>ERROR: File LIBRARY.TABLE_JAN17.DATA does not exist.</em></strong></p>
  
  <p>If I use the following code it works fine, but I want to loop through 120 months of data so don't want to individually name each month &amp; year. </p>
  
  <pre><code>%Let vMonth2 = JAN17;
  
  proc sql;
  create table abc as 
  select * from LIBRARY.TABLE_&amp;vMonth2._ALL ;
  </code></pre>
  
  <p>Can anyone advise as to what the issue is? </p>
  
  <p>Thanks. </p>