<?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 looping through each column with macro in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/looping-through-each-column-with-macro/m-p/571994#M12267</link>
    <description>&lt;P&gt;%macro minmaxchar(Var);&lt;BR /&gt;proc sql;&lt;BR /&gt;select min(&amp;amp;Var) as Minimum,max(&amp;amp;Var) as Maximum from&lt;BR /&gt;work.result;&lt;BR /&gt;quit;&lt;BR /&gt;%mend minmaxchar;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;%macro minmaxloop();&lt;BR /&gt;data work.sc;&lt;BR /&gt;set work.result;&lt;BR /&gt;array Var _character_;&lt;BR /&gt;do over Var;&lt;BR /&gt;%minmaxchar(Var);&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;BR /&gt;%mend minmaxloop;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;this is my input (work.result):&lt;BR /&gt;A B C D&lt;BR /&gt;--------&lt;BR /&gt;1 dv 4 t&lt;BR /&gt;3 aa 5 zz&lt;BR /&gt;2 cs 6 ac&lt;/P&gt;&lt;P&gt;this is my wanted output&lt;BR /&gt;Max Min&lt;BR /&gt;-------&lt;BR /&gt;3 1&lt;BR /&gt;dv aa&lt;BR /&gt;6 4&lt;BR /&gt;zz ac&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am looking for a way to get the minimum and the maximum of each column (I want to make it work for both char and numeric columns), but I need to use the sql min() and max() instead of sas because sql min() max() can receive char as a parameter and compute the min and max as in ASCII order. The only problem with sql min()/max() is that it can only receive one argument at a time, so I would need to enter the column name in order for it to work. However, I want the code to be as dynamic as possible so I want a way to put in a macro variable as a parameter in the min/max function and have the macro variable be the iterator through all the columns. My code above does not work because the proc sql cannot be used within the looping data step. Please enlightened me experts.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 09 Jul 2019 10:35:56 GMT</pubDate>
    <dc:creator>sirapolw</dc:creator>
    <dc:date>2019-07-09T10:35:56Z</dc:date>
    <item>
      <title>looping through each column with macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/looping-through-each-column-with-macro/m-p/571994#M12267</link>
      <description>&lt;P&gt;%macro minmaxchar(Var);&lt;BR /&gt;proc sql;&lt;BR /&gt;select min(&amp;amp;Var) as Minimum,max(&amp;amp;Var) as Maximum from&lt;BR /&gt;work.result;&lt;BR /&gt;quit;&lt;BR /&gt;%mend minmaxchar;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;%macro minmaxloop();&lt;BR /&gt;data work.sc;&lt;BR /&gt;set work.result;&lt;BR /&gt;array Var _character_;&lt;BR /&gt;do over Var;&lt;BR /&gt;%minmaxchar(Var);&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;BR /&gt;%mend minmaxloop;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;this is my input (work.result):&lt;BR /&gt;A B C D&lt;BR /&gt;--------&lt;BR /&gt;1 dv 4 t&lt;BR /&gt;3 aa 5 zz&lt;BR /&gt;2 cs 6 ac&lt;/P&gt;&lt;P&gt;this is my wanted output&lt;BR /&gt;Max Min&lt;BR /&gt;-------&lt;BR /&gt;3 1&lt;BR /&gt;dv aa&lt;BR /&gt;6 4&lt;BR /&gt;zz ac&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am looking for a way to get the minimum and the maximum of each column (I want to make it work for both char and numeric columns), but I need to use the sql min() and max() instead of sas because sql min() max() can receive char as a parameter and compute the min and max as in ASCII order. The only problem with sql min()/max() is that it can only receive one argument at a time, so I would need to enter the column name in order for it to work. However, I want the code to be as dynamic as possible so I want a way to put in a macro variable as a parameter in the min/max function and have the macro variable be the iterator through all the columns. My code above does not work because the proc sql cannot be used within the looping data step. Please enlightened me experts.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Jul 2019 10:35:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/looping-through-each-column-with-macro/m-p/571994#M12267</guid>
      <dc:creator>sirapolw</dc:creator>
      <dc:date>2019-07-09T10:35:56Z</dc:date>
    </item>
    <item>
      <title>Re: looping through each column with macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/looping-through-each-column-with-macro/m-p/571996#M12268</link>
      <description>&lt;P&gt;First, macros are not necessary, as SAS has many built-in ways of finding minimums and maximums of a column.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Next, even if you were to run your macro, it would never work because you cannot run PROC SQL inside a DATA step as you are trying to do. Further, in order to get any macro to work, the advice (which you should be following) is to get the code to work on one variable &lt;EM&gt;without using macros&lt;/EM&gt;, before you try to turn it into a macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For your numeric variables, one way to get the min and max of the columns simply is via PROC SUMMARY&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=have;
    var _numeric_;
    output out=want min= max=/autoname;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Since there is no such thing as a minimum of character variables, you'll have to define what you mean. If you just want the character variable levels counted, you can use PROC FREQ.&lt;/P&gt;</description>
      <pubDate>Tue, 09 Jul 2019 12:06:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/looping-through-each-column-with-macro/m-p/571996#M12268</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-07-09T12:06:36Z</dc:date>
    </item>
    <item>
      <title>Re: looping through each column with macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/looping-through-each-column-with-macro/m-p/572023#M12272</link>
      <description>PROC SUMMARY can find min/max of character variables using IDGROUP statement.</description>
      <pubDate>Tue, 09 Jul 2019 12:52:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/looping-through-each-column-with-macro/m-p/572023#M12272</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2019-07-09T12:52:32Z</dc:date>
    </item>
  </channel>
</rss>

