<?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: Using output of proc contents to generate list of field names to store in a macro variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-output-of-proc-contents-to-generate-list-of-field-names-to/m-p/694903#M211973</link>
    <description>&lt;P&gt;Use the INTO clause of PROC SQL SELECT statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  select name into :varlist separated by ',' from fields;
  create table my_new_data as
  select &amp;amp;varlist
       , id
       , ind_code
    from auth_data
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 28 Oct 2020 15:19:15 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2020-10-28T15:19:15Z</dc:date>
    <item>
      <title>Using output of proc contents to generate list of field names to store in a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-output-of-proc-contents-to-generate-list-of-field-names-to/m-p/694902#M211972</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm attempting to create a list of fields stored in a macro variable to be used in a subsequent proc sql. I start the process with a proc contents on my input table to generate the field names into a table called "fields" like such:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc contents data=egtask.my_data out=fields;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;What I'm getting stuck on is converting that to a list that you could put in a macro variable, and then use that in a proc sql;&lt;/P&gt;
&lt;PRE&gt;%let field_list =  t1.system_id,  t1.system_name, t1.core_id, t1.core_measure

proc sql;
  create table my_new_data as
  select 
         &amp;amp;field_list,
         t1.id,
         t1.ind_code
from auth_data;
quit;&lt;/PRE&gt;
&lt;P&gt;So like that except I want &amp;amp;field_list to be populated with the fields in "fields" automatically and not have to hard code the variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Oct 2020 15:16:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-output-of-proc-contents-to-generate-list-of-field-names-to/m-p/694902#M211972</guid>
      <dc:creator>PegaZeus</dc:creator>
      <dc:date>2020-10-28T15:16:27Z</dc:date>
    </item>
    <item>
      <title>Re: Using output of proc contents to generate list of field names to store in a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-output-of-proc-contents-to-generate-list-of-field-names-to/m-p/694903#M211973</link>
      <description>&lt;P&gt;Use the INTO clause of PROC SQL SELECT statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  select name into :varlist separated by ',' from fields;
  create table my_new_data as
  select &amp;amp;varlist
       , id
       , ind_code
    from auth_data
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 28 Oct 2020 15:19:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-output-of-proc-contents-to-generate-list-of-field-names-to/m-p/694903#M211973</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-10-28T15:19:15Z</dc:date>
    </item>
    <item>
      <title>Re: Using output of proc contents to generate list of field names to store in a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-output-of-proc-contents-to-generate-list-of-field-names-to/m-p/694912#M211980</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select name into :field_list separated by ", "
from sashelp.vcolumns
where libname = 'EGTASK' and memname = 'MY_DATA';
quit;

%put &amp;amp;field_list.;

%put Number of Entries : &amp;amp;sql_obs;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 28 Oct 2020 15:39:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-output-of-proc-contents-to-generate-list-of-field-names-to/m-p/694912#M211980</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-10-28T15:39:22Z</dc:date>
    </item>
    <item>
      <title>Re: Using output of proc contents to generate list of field names to store in a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-output-of-proc-contents-to-generate-list-of-field-names-to/m-p/694913#M211981</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/320380"&gt;@PegaZeus&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can also pull the variable names from DICTIONARY.COLUMNS. Then you don't need a separate PROC step, nor a temporary dataset to store the names:&lt;/P&gt;
&lt;PRE&gt;&lt;FONT color="#999999"&gt;proc sql&lt;/FONT&gt; &lt;STRONG&gt;noprint;&lt;/STRONG&gt;
&lt;STRONG&gt;select name into :field_list separated by ','
from dictionary.columns
where libname='EGTASK' &amp;amp; memname='MY_DATA';&lt;/STRONG&gt;

&lt;FONT color="#999999"&gt;create table my_new_data as
select &amp;amp;field_list,
       ...
quit;&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;If the variable names need to be prefixed, e.g., with "t1." for the CREATE TABLE statement, add the prefix in the first SELECT statement:&lt;/P&gt;
&lt;PRE&gt;&lt;FONT color="#999999"&gt;select&lt;/FONT&gt; &lt;STRONG&gt;'t1.'||&lt;/STRONG&gt;&lt;FONT color="#999999"&gt;name into ...&lt;/FONT&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 28 Oct 2020 15:40:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-output-of-proc-contents-to-generate-list-of-field-names-to/m-p/694913#M211981</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2020-10-28T15:40:00Z</dc:date>
    </item>
  </channel>
</rss>

