<?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: Dynamically coding using PROC SQL to select dynamic number of columns in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-coding-using-PROC-SQL-to-select-dynamic-number-of/m-p/525679#M143058</link>
    <description>&lt;P&gt;How about:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let mxlist=max(age),max(height),max(weight);
proc sql;
  select &amp;amp;mxlist from sashelp.class;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 09 Jan 2019 12:14:16 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2019-01-09T12:14:16Z</dc:date>
    <item>
      <title>Dynamically coding using PROC SQL to select dynamic number of columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-coding-using-PROC-SQL-to-select-dynamic-number-of/m-p/525674#M143055</link>
      <description>&lt;P&gt;Dear experts,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;First, I am on version 7.1.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The following simple example, I would like to make it more flexible / dynamic:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;

SELECT           MAX(Var1)
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;, MAX(Var2)
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;, Max(Var3)

FROM&amp;nbsp; &amp;nbsp;         My_Table

;

QUIT;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;To make it more dynamic:&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;%LET Variable_List = Var1 Var2 Var3;

PROC SQL;

SELECT MAX(&amp;amp;Variable_List)
FROM&amp;nbsp; &amp;nbsp;My_Table
;

QUIT;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This however doesn't work, but I'd like it to function as the first manually written statement. And it should also be flexible in the way that if I give in 4 or 5 variables (var4 var5), that it will still work. This can probably be solved by using a %Do loop, but I haven't been able to make it work.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could you give me some advice on how to do this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Wed, 09 Jan 2019 10:57:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-coding-using-PROC-SQL-to-select-dynamic-number-of/m-p/525674#M143055</guid>
      <dc:creator>Kazzie</dc:creator>
      <dc:date>2019-01-09T10:57:09Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically coding using PROC SQL to select dynamic number of columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-coding-using-PROC-SQL-to-select-dynamic-number-of/m-p/525679#M143058</link>
      <description>&lt;P&gt;How about:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let mxlist=max(age),max(height),max(weight);
proc sql;
  select &amp;amp;mxlist from sashelp.class;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 09 Jan 2019 12:14:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-coding-using-PROC-SQL-to-select-dynamic-number-of/m-p/525679#M143058</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-01-09T12:14:16Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically coding using PROC SQL to select dynamic number of columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-coding-using-PROC-SQL-to-select-dynamic-number-of/m-p/525697#M143064</link>
      <description>&lt;P&gt;If this is all you want to do, you could move away from PROC SQL:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let Variable_List = Var1 Var2 Var3;

proc means data=my_table max;
   var &amp;amp;variable_list;
run;
   &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;PROC MEANS is very flexible.&amp;nbsp; You can create an output data set instead of a report:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let Variable_List = Var1 Var2 Var3;

proc means data=my_table noprint;
   var &amp;amp;variable_list;
   output out=want (keep=&amp;amp;variable_list) max=;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;There are also ways to keep additional variables in the output if that is a requirement.&amp;nbsp; But you would need to spell out any additional requirements.&lt;/P&gt;</description>
      <pubDate>Wed, 09 Jan 2019 12:53:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-coding-using-PROC-SQL-to-select-dynamic-number-of/m-p/525697#M143064</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-01-09T12:53:23Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically coding using PROC SQL to select dynamic number of columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-coding-using-PROC-SQL-to-select-dynamic-number-of/m-p/525702#M143066</link>
      <description>&lt;P&gt;I certainly agree with &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt; who points out that in PROC MEANS (or it's twin sister PROC SUMMARY), you don't need any looping, and provides the simplest coding.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Nevertheless, in case the problem is more complicated than the original example and it needs to be done in SQL:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro dothis;
    %let variable_list=var1 var2 var3;
    proc sql;
        select 
        %do i=1 %to %sysfunc(countw(&amp;amp;variable_list));
            %let thisvar=%scan(&amp;amp;variable_list,&amp;amp;i,%str( ));
            max(&amp;amp;thisvar)
            %if &amp;amp;i&amp;lt;%sysfunc(countw(&amp;amp;variable_list)) %then %str(,);
        %end;
        from my_table;
    quit;
%mend dothis;
%dothis
        
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So again, you can see this is a lot of coding, and a lot of opportunity for errors, and I strongly urge you to consider the much simpler PROC MEANS/PROC SUMMARY solution.&lt;/P&gt;</description>
      <pubDate>Wed, 09 Jan 2019 13:17:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-coding-using-PROC-SQL-to-select-dynamic-number-of/m-p/525702#M143066</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-01-09T13:17:37Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically coding using PROC SQL to select dynamic number of columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-coding-using-PROC-SQL-to-select-dynamic-number-of/m-p/525714#M143070</link>
      <description>Thanks for the code and your advice. I indeed accepted the solution from &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&lt;BR /&gt;&lt;BR /&gt;Yours however is very useful for my overall understanding!</description>
      <pubDate>Wed, 09 Jan 2019 14:05:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-coding-using-PROC-SQL-to-select-dynamic-number-of/m-p/525714#M143070</guid>
      <dc:creator>Kazzie</dc:creator>
      <dc:date>2019-01-09T14:05:20Z</dc:date>
    </item>
  </channel>
</rss>

