<?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: Proc sql for all columns in a table in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-for-all-columns-in-a-table/m-p/509763#M137089</link>
    <description>&lt;P&gt;Hello ,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;sorry for the delayed response.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have added an attachment here that show input and output data.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the input data - there are 4 M variables &amp;amp; year . Each M contains value either from 10,20,30,40,50.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now on this input data column M1 if i use proc sql query I provided earlier , I get output data ( which is basically a table for M1 only)&lt;/P&gt;&lt;P&gt;&amp;nbsp; I need to create such tables for M2, M3 &amp;amp; M4 in one go without writing this query 3 more times.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;These tables will be later exported into excel.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help on this will be great.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
    <pubDate>Fri, 02 Nov 2018 02:54:57 GMT</pubDate>
    <dc:creator>pa4</dc:creator>
    <dc:date>2018-11-02T02:54:57Z</dc:date>
    <item>
      <title>Proc sql for all columns in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-for-all-columns-in-a-table/m-p/508916#M136723</link>
      <description>&lt;P&gt;Hi ,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am fairly new to SAS.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a table in below format.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Table Name - main_table&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; M1 M2 M3 ..... M200&amp;nbsp; year&lt;/P&gt;&lt;P&gt;&amp;nbsp; 1&amp;nbsp; &amp;nbsp; 45&amp;nbsp; 32 .....&amp;nbsp; &amp;nbsp;45&amp;nbsp; &amp;nbsp; &amp;nbsp; 2001&lt;/P&gt;&lt;P&gt;&amp;nbsp; 6&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; 4&amp;nbsp; &amp;nbsp; 23 .....&amp;nbsp; &amp;nbsp;48&amp;nbsp; &amp;nbsp; &amp;nbsp; 2002&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;..... .....................................&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;..........................................&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;The table has 200 columns that are from M1 to M200 &amp;amp; last column is year column.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;All these 200 columns have numeric values from 1 to 50.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I already have a proc sql that does job for me to get counts for one M column.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I want to iterate this proc sql for all columns in this table.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Below is proc sql i have -&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;proc sql;&lt;BR /&gt;create table M1 as&lt;BR /&gt;SELECT&lt;BR /&gt;M1,&lt;BR /&gt;SUM(CASE WHEN (year='2001') THEN 1 ELSE 0 END) AS M1_2001,&lt;BR /&gt;SUM(CASE WHEN (year='2002') THEN 1 ELSE 0 END) AS M2_2002,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;...&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;...&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;from main_table&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;group by M1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;order by M1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;ASC;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Quit;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Please help me with iterating this proc sql through for all M1 to M200.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;The sql query is giving me results i needed just for one column. Need those results for all columns.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thanks&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Oct 2018 19:57:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-for-all-columns-in-a-table/m-p/508916#M136723</guid>
      <dc:creator>pa4</dc:creator>
      <dc:date>2018-10-30T19:57:24Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql for all columns in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-for-all-columns-in-a-table/m-p/508919#M136726</link>
      <description>&lt;P&gt;You are going about it the hard way. Something like this is a lot easier:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data = main_table nway;
  class year;
  var M1 - M200;
  output out = want
         sum = 
         ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 30 Oct 2018 20:07:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-for-all-columns-in-a-table/m-p/508919#M136726</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2018-10-30T20:07:04Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql for all columns in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-for-all-columns-in-a-table/m-p/508923#M136730</link>
      <description>&lt;P&gt;It gives me error as -&amp;nbsp;&lt;/P&gt;&lt;P&gt;ERROR: Variable M1 in list does not match type prescribed for this list.&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;ERROR: Variable M2 in list does not match type prescribed for this list.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;so on.. for all variables.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Oct 2018 20:12:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-for-all-columns-in-a-table/m-p/508923#M136730</guid>
      <dc:creator>pa4</dc:creator>
      <dc:date>2018-10-30T20:12:26Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql for all columns in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-for-all-columns-in-a-table/m-p/508929#M136734</link>
      <description>&lt;P&gt;Are you wanting to count the M variables rather than sum them?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If so how to you want them counted - by each distinct value of M? Please supply sample input data and wanted output data.&lt;/P&gt;</description>
      <pubDate>Tue, 30 Oct 2018 20:39:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-for-all-columns-in-a-table/m-p/508929#M136734</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2018-10-30T20:39:20Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql for all columns in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-for-all-columns-in-a-table/m-p/508931#M136736</link>
      <description>You're aware that all you're doing there is counting the year, not anything related to the M values?</description>
      <pubDate>Tue, 30 Oct 2018 20:39:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-for-all-columns-in-a-table/m-p/508931#M136736</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-10-30T20:39:49Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql for all columns in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-for-all-columns-in-a-table/m-p/508937#M136738</link>
      <description>&lt;P&gt;Yes , I want them counted by distinct values of M.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Basically all my proc sql doing is - it is creating catching how many times a number which is 1 to 50 came for M1 in every year.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So my output data would look something like this for the proc sql i have now for M1.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Index&amp;nbsp; 2001 2002 2003 ....&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 25&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;34&amp;nbsp; &amp;nbsp; 67&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 10&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;45&amp;nbsp; &amp;nbsp; 88&lt;/P&gt;&lt;P&gt;3&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;50&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;That means 1 came 25 times for 2001 , 2 came 88 times in 2003 &amp;amp; so on.&amp;nbsp;&lt;/P&gt;&lt;P&gt;please note this is just for M1 using individual proc sql i provided in question.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want it to create a table like above for all M1 to M200.&amp;nbsp;&lt;/P&gt;&lt;P&gt;So basically it should create 200 tables.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Oct 2018 20:58:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-for-all-columns-in-a-table/m-p/508937#M136738</guid>
      <dc:creator>pa4</dc:creator>
      <dc:date>2018-10-30T20:58:38Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql for all columns in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-for-all-columns-in-a-table/m-p/508938#M136739</link>
      <description>&lt;P&gt;You likely want this, but it's a single table. You'll need to reformat it for your final outcome, though I suspect this will be easier to work with overall.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=have;
table year* (M1 - M200) / out=want;
run;


&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you can manipulate the 'want' data set to get your desired output, likely via PROC TRANSPOSE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Oct 2018 21:03:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-for-all-columns-in-a-table/m-p/508938#M136739</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-10-30T21:03:14Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql for all columns in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-for-all-columns-in-a-table/m-p/508970#M136755</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/151988"&gt;@pa4&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;It gives me error as -&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ERROR: Variable M1 in list does not match type prescribed for this list.&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;ERROR: Variable M2 in list does not match type prescribed for this list.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;so on.. for all variables.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This means that your statement&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;All these 200 columns have numeric values from 1 to 50.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Is false. You have character values that contain digit characters. You do not have numeric values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Hint:&lt;/STRONG&gt; Provide a data set with 10 rows of your data and only 3 of the M variables with values of only 1 to 3 or 5&amp;nbsp;plus year. That should be small enough you can completely work that data by hand. Show us what the result should be. Make sure the variables have somewhat different values in each row and column as an example with identical rows/columns might be possibly solved with approaches that don't work with general data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, does the result need to be a data set, something that will be fed into another procedure, or a report that people will read?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Oct 2018 23:01:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-for-all-columns-in-a-table/m-p/508970#M136755</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-10-30T23:01:59Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql for all columns in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-for-all-columns-in-a-table/m-p/509763#M137089</link>
      <description>&lt;P&gt;Hello ,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;sorry for the delayed response.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have added an attachment here that show input and output data.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the input data - there are 4 M variables &amp;amp; year . Each M contains value either from 10,20,30,40,50.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now on this input data column M1 if i use proc sql query I provided earlier , I get output data ( which is basically a table for M1 only)&lt;/P&gt;&lt;P&gt;&amp;nbsp; I need to create such tables for M2, M3 &amp;amp; M4 in one go without writing this query 3 more times.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;These tables will be later exported into excel.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help on this will be great.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Fri, 02 Nov 2018 02:54:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-for-all-columns-in-a-table/m-p/509763#M137089</guid>
      <dc:creator>pa4</dc:creator>
      <dc:date>2018-11-02T02:54:57Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql for all columns in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-for-all-columns-in-a-table/m-p/509765#M137090</link>
      <description>Try this, and then try modifying your ODs excel options to get one tab per table. &lt;BR /&gt;&lt;BR /&gt;ODs excel file=‘demo.xlsx’;&lt;BR /&gt;&lt;BR /&gt;Proc freq from above&lt;BR /&gt;&lt;BR /&gt;ODs excel close;</description>
      <pubDate>Fri, 02 Nov 2018 03:29:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-for-all-columns-in-a-table/m-p/509765#M137090</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-11-02T03:29:05Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql for all columns in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-for-all-columns-in-a-table/m-p/964266#M375554</link>
      <description>&lt;P&gt;Can you elaborate on why the "sum =" is open-ended. Thanks.&lt;/P&gt;</description>
      <pubDate>Mon, 14 Apr 2025 20:35:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-for-all-columns-in-a-table/m-p/964266#M375554</guid>
      <dc:creator>sas_user_1001</dc:creator>
      <dc:date>2025-04-14T20:35:46Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql for all columns in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-for-all-columns-in-a-table/m-p/964286#M375561</link>
      <description>&lt;P&gt;First a general suggestion: create your own thread and reference this one with your questions. One reason is that as the creator of a thread you have the ability to indicate an answer is "correct". Second&amp;nbsp; is to not confuse issues as to why you may be asking questions about things. Third, it is a good idea to copy and insert text or code that you want to question into a text box opened on the forum the &amp;lt;/&amp;gt; so we can see which specific text or code you are questioning. Some of these thread can have similar code and that helps create a specific reference.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In Procs Summary or Means if you use an OUTPUT statement as in (see the text box)&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;proc summary data = main_table nway;
  class year;
  var M1 - M200;
  output out = want
         sum = 
         ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;If you request a single statistic, could be any of those available from the statistic but Sum= is used here, then&amp;nbsp;&lt;STRONG&gt;every variable&lt;/STRONG&gt; referenced on the VAR statement will have that statistic value&amp;nbsp;&lt;STRONG&gt;and&lt;/STRONG&gt; the name of the variable will be that of the variable of the on the VAR statement. The general form is statistic (variable list)=(output variable name list).&lt;/P&gt;
&lt;P&gt;The output statements, yes you may have more than one if you want more than one output data set, for Proc Summary/Means can get fairly complex with ID group options, minimum id options, maximum id options, autoname for naming.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/463968"&gt;@sas_user_1001&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Can you elaborate on why the "sum =" is open-ended. Thanks.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The actual WHY is the programmers prerogative. Probably didn't want to create 200 new variable names.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Apr 2025 04:52:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-for-all-columns-in-a-table/m-p/964286#M375561</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2025-04-15T04:52:00Z</dc:date>
    </item>
  </channel>
</rss>

