<?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: SQL Select using Macro to Resolve Columns to Include in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SQL-Select-using-Macro-to-Resolve-Columns-to-Include/m-p/74555#M16062</link>
    <description>The following solution uses your table of column names which I called "datasetofcolnames" in the code below:&lt;BR /&gt;
[pre]&lt;BR /&gt;
%macro sumCols( _collist );&lt;BR /&gt;
   %local return n_cols colname i;&lt;BR /&gt;
   %let n_cols = %sysfunc( countw( &amp;amp;_collist, %str( )));&lt;BR /&gt;
   %do i=1 %to &amp;amp;n_cols;&lt;BR /&gt;
      %let colname = %scan( &amp;amp;_collist, &amp;amp;i, %str( ));&lt;BR /&gt;
      %let return = &amp;amp;return sum(&amp;amp;colname) as &amp;amp;colname;&lt;BR /&gt;
      %if ( &amp;amp;i &amp;lt; &amp;amp;n_cols ) %then&lt;BR /&gt;
         %let return = &amp;amp;return,;&lt;BR /&gt;
   %end;&lt;BR /&gt;
&amp;amp;return&lt;BR /&gt;
%mend;&lt;BR /&gt;
proc sql noprint;&lt;BR /&gt;
   *-- first get a list of the column names ;&lt;BR /&gt;
   select distinct colname into : collist separated by ' '&lt;BR /&gt;
   from datasetofcolnames&lt;BR /&gt;
   order by colname;&lt;BR /&gt;
   *-- then extract your data ;&lt;BR /&gt;
   create table testMacro as&lt;BR /&gt;
      select row_labels, month, %sumCols( &amp;amp;collist )&lt;BR /&gt;
      from Me&lt;BR /&gt;
      group by row_labels, month;&lt;BR /&gt;
quit;&lt;BR /&gt;
[/pre]</description>
    <pubDate>Fri, 24 Sep 2010 17:44:39 GMT</pubDate>
    <dc:creator>WaltSmith</dc:creator>
    <dc:date>2010-09-24T17:44:39Z</dc:date>
    <item>
      <title>SQL Select using Macro to Resolve Columns to Include</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Select-using-Macro-to-Resolve-Columns-to-Include/m-p/74554#M16061</link>
      <description>I would like to rewrite the following macro "sumCol" so it can handle the 300+/- columns that I'd like to sum.  There is probably a much more eloquent way of handling this, but I am having trouble finding it because I am new to SAS.  If it helps you to redirect me on the right path, the columns I would like to sum all begin with the same three letters "Col".  There are typically approx 10 additional columns in the original table that I don't want to sum.&lt;BR /&gt;
&lt;BR /&gt;
One direction I explored was to use Dictionary.Columns to create a table the column names I would like to sum.  This step worked; however I couldn't determine how to iterate the resulting observations to create the desired end result.&lt;BR /&gt;
&lt;BR /&gt;
Thank you,&lt;BR /&gt;
Chad&lt;BR /&gt;
&lt;BR /&gt;
/*BEGIN Code the works (at least for the three hand-keyed columns)*/&lt;BR /&gt;
%macro sumCol;&lt;BR /&gt;
	sum (Col10599) as Sum10599, sum (Col10608) as Sum30608, sum (Col05393) as Sum05393&lt;BR /&gt;
%mend;&lt;BR /&gt;
&lt;BR /&gt;
proc sql;&lt;BR /&gt;
	Create table testMacro as				&lt;BR /&gt;
	select row_labels, %sumCol			&lt;BR /&gt;
	from Me						&lt;BR /&gt;
	group by row_labels month;					&lt;BR /&gt;
quit;&lt;BR /&gt;
/*END Code the works (at least for the three hand-keyed columns)*/&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
/* The proc below successfully identifies each column name i'd like to sum */&lt;BR /&gt;
/* I'm stumped on how to iterate through the results to achieve results */&lt;BR /&gt;
/* Goal: sum (obs1) as obs1, sum (obs2) as obs2, ... sum (obsN) as obsN */&lt;BR /&gt;
&lt;BR /&gt;
*Creates a table listing all column names begining with 'TVA' inside table TEST2;&lt;BR /&gt;
proc sql;&lt;BR /&gt;
create table meterNames as&lt;BR /&gt;
select name&lt;BR /&gt;
from dictionary.columns&lt;BR /&gt;
WHERE memname = "TEST2"&lt;BR /&gt;
and name like 'Col%';&lt;BR /&gt;
quit;</description>
      <pubDate>Fri, 24 Sep 2010 16:57:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Select-using-Macro-to-Resolve-Columns-to-Include/m-p/74554#M16061</guid>
      <dc:creator>lowjack</dc:creator>
      <dc:date>2010-09-24T16:57:18Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Select using Macro to Resolve Columns to Include</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Select-using-Macro-to-Resolve-Columns-to-Include/m-p/74555#M16062</link>
      <description>The following solution uses your table of column names which I called "datasetofcolnames" in the code below:&lt;BR /&gt;
[pre]&lt;BR /&gt;
%macro sumCols( _collist );&lt;BR /&gt;
   %local return n_cols colname i;&lt;BR /&gt;
   %let n_cols = %sysfunc( countw( &amp;amp;_collist, %str( )));&lt;BR /&gt;
   %do i=1 %to &amp;amp;n_cols;&lt;BR /&gt;
      %let colname = %scan( &amp;amp;_collist, &amp;amp;i, %str( ));&lt;BR /&gt;
      %let return = &amp;amp;return sum(&amp;amp;colname) as &amp;amp;colname;&lt;BR /&gt;
      %if ( &amp;amp;i &amp;lt; &amp;amp;n_cols ) %then&lt;BR /&gt;
         %let return = &amp;amp;return,;&lt;BR /&gt;
   %end;&lt;BR /&gt;
&amp;amp;return&lt;BR /&gt;
%mend;&lt;BR /&gt;
proc sql noprint;&lt;BR /&gt;
   *-- first get a list of the column names ;&lt;BR /&gt;
   select distinct colname into : collist separated by ' '&lt;BR /&gt;
   from datasetofcolnames&lt;BR /&gt;
   order by colname;&lt;BR /&gt;
   *-- then extract your data ;&lt;BR /&gt;
   create table testMacro as&lt;BR /&gt;
      select row_labels, month, %sumCols( &amp;amp;collist )&lt;BR /&gt;
      from Me&lt;BR /&gt;
      group by row_labels, month;&lt;BR /&gt;
quit;&lt;BR /&gt;
[/pre]</description>
      <pubDate>Fri, 24 Sep 2010 17:44:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Select-using-Macro-to-Resolve-Columns-to-Include/m-p/74555#M16062</guid>
      <dc:creator>WaltSmith</dc:creator>
      <dc:date>2010-09-24T17:44:39Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Select using Macro to Resolve Columns to Include</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Select-using-Macro-to-Resolve-Columns-to-Include/m-p/74556#M16063</link>
      <description>Chad,&lt;BR /&gt;
&lt;BR /&gt;
Unless you are just trying to learn how to write macros, I would avoid using a macro for this problem.  Instead, you can use proc sql to create a macro variable that does what you want.  For example:&lt;BR /&gt;
&lt;BR /&gt;
/* Create some test data */&lt;BR /&gt;
data test2;&lt;BR /&gt;
  set sashelp.class (rename=(&lt;BR /&gt;
   name=row_labels&lt;BR /&gt;
   age=Col0001&lt;BR /&gt;
   height=Col0002&lt;BR /&gt;
   weight=Col0003&lt;BR /&gt;
   ));&lt;BR /&gt;
  month=mod(_n_,3);&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
/*Create a macro variable with proc sql */&lt;BR /&gt;
proc sql noprint;&lt;BR /&gt;
  select "Sum ("||trim(name)||") as Sum"||substr(name,4)&lt;BR /&gt;
     into :sumCol separated by ","&lt;BR /&gt;
       from dictionary.columns&lt;BR /&gt;
         WHERE libname eq "WORK"&lt;BR /&gt;
           and memname = "TEST2"&lt;BR /&gt;
            and name like 'Col%';&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
proc sql noprint;&lt;BR /&gt;
  create table testMacro as&lt;BR /&gt;
    select row_labels, &amp;amp;sumCol.&lt;BR /&gt;
      from test2&lt;BR /&gt;
        group by row_labels,month; &lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
HTH,&lt;BR /&gt;
Art</description>
      <pubDate>Fri, 24 Sep 2010 17:49:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Select-using-Macro-to-Resolve-Columns-to-Include/m-p/74556#M16063</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2010-09-24T17:49:51Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Select using Macro to Resolve Columns to Include</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Select-using-Macro-to-Resolve-Columns-to-Include/m-p/74557#M16064</link>
      <description>I find that PROCs SUMMARY or MEANS are much better suited to the task than SQL.  SQL is fine but the syntax does not make it easy like MEANS/SUMMARY.&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data have;&lt;BR /&gt;
   array _c&lt;LI&gt; Col10599 Col10608 Col05393;&lt;BR /&gt;
   do _n_ = 1 to 100;&lt;BR /&gt;
      do _i_ = 1 to dim(_c);&lt;BR /&gt;
         _c[_i_] = rannor(1)*10 + 5;&lt;BR /&gt;
         end;&lt;BR /&gt;
      output;&lt;BR /&gt;
      end;&lt;BR /&gt;
   drop _:;&lt;BR /&gt;
   run;&lt;BR /&gt;
proc summary data=have;&lt;BR /&gt;
   var col:;&lt;BR /&gt;
   output out=sum;&lt;BR /&gt;
   run;&lt;BR /&gt;
proc print;&lt;BR /&gt;
   run;&lt;BR /&gt;
[/pre]&lt;/LI&gt;</description>
      <pubDate>Fri, 24 Sep 2010 18:07:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Select-using-Macro-to-Resolve-Columns-to-Include/m-p/74557#M16064</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2010-09-24T18:07:36Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Select using Macro to Resolve Columns to Include</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Select-using-Macro-to-Resolve-Columns-to-Include/m-p/74558#M16065</link>
      <description>Thank you!  This works perfectly.</description>
      <pubDate>Fri, 24 Sep 2010 18:19:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Select-using-Macro-to-Resolve-Columns-to-Include/m-p/74558#M16065</guid>
      <dc:creator>lowjack</dc:creator>
      <dc:date>2010-09-24T18:19:44Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Select using Macro to Resolve Columns to Include</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Select-using-Macro-to-Resolve-Columns-to-Include/m-p/74559#M16066</link>
      <description>Agree completely - when I need sums or means or ... its almost always proc summary or proc means (when I want quantiles, or medians, etc). I write less code using proc summary than the equivalent code using SQL, however, SQL can sometimes do what it may take several steps otherwise.&lt;BR /&gt;
&lt;BR /&gt;
With the simple problem presented, I would prefer proc summary, but he asked in the framework of proc sql so ...</description>
      <pubDate>Fri, 24 Sep 2010 18:20:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Select-using-Macro-to-Resolve-Columns-to-Include/m-p/74559#M16066</guid>
      <dc:creator>WaltSmith</dc:creator>
      <dc:date>2010-09-24T18:20:40Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Select using Macro to Resolve Columns to Include</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Select-using-Macro-to-Resolve-Columns-to-Include/m-p/74560#M16067</link>
      <description>I was "speaking" more to the OP's statement that he is new to SAS.  Thinking that learning MEANS/SUMMARY would be more fulfilling than using SQL for summary statistics.</description>
      <pubDate>Fri, 24 Sep 2010 18:25:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Select-using-Macro-to-Resolve-Columns-to-Include/m-p/74560#M16067</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2010-09-24T18:25:35Z</dc:date>
    </item>
  </channel>
</rss>

