<?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 group by problem when using select as columns in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SQL-group-by-problem-when-using-select-as-columns/m-p/962310#M375077</link>
    <description>&lt;P&gt;You left the BY keyword out.&amp;nbsp; &amp;nbsp;And you don't need to include a constant variable like your new WAY in the BY group.&amp;nbsp; But those are not the issue I think you are asking about.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Basically SAS is saying that just changing the name is not really making a new variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So you have two choices.&amp;nbsp; Remove the CALCULATED keyword.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select 'way 3' as way
     , id
     , t as s_new
     , count(*) as freq 
from have
group by id, s_new
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you are worried that there is possible ambiguity in the new name, then use the old name in the GROUP BY clause.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select 'way 3' as way
     , id
     , t as s_new
     , count(*) as freq format=4. 
from have
group by id, have.t
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or add some none destructive operation, like TRIM() for character variables or SUM(var,.) for numeric variables, so that it will treat it as calculated.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select 'way 3' as way
     , id
     , trim(t) as s_new
     , count(*) as freq format=4. 
from have
group by id, calculated s_new
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And if all you wanted was the change the variable name you could just use the RENAME= dataset option on the source dataset.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select 'way 3' as way
     , id
     , s_new
     , count(*) as freq format=4. 
from have(rename=(t=s_new))
group by id, s_new
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 20 Mar 2025 03:12:46 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2025-03-20T03:12:46Z</dc:date>
    <item>
      <title>SQL group by problem when using select as columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-group-by-problem-when-using-select-as-columns/m-p/962309#M375076</link>
      <description>&lt;P&gt;Consider the common SQL task of counting the count(*) over a select group.&lt;/P&gt;
&lt;PRE&gt;data have;
  length s $1 t $3; format id 1. ;
  do id = 1 to 4;
    do rep = 1 to id;
      s = cats (id);
      t = 't_' || cats (id);
      output;
    end;
  end;
run;&lt;/PRE&gt;
&lt;PRE&gt;proc sql; select id, s, count(*) as freq format=4. from have group by id, s;&lt;/PRE&gt;
&lt;PRE&gt;id  s  freq
-----------
 1  1     1
 2  2     2
 3  3     3
 4  4     4&lt;/PRE&gt;
&lt;P&gt;Now add the twist that one of the selects is mapping a non-select column &lt;STRONG&gt;t&lt;/STRONG&gt; as select column &lt;STRONG&gt;s.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;So, there is now some ambiguation of what &lt;STRONG&gt;s&lt;/STRONG&gt; is.&amp;nbsp; Is it the original &lt;STRONG&gt;want.s&lt;/STRONG&gt; or the &lt;STRONG&gt;as s&lt;/STRONG&gt;.&lt;/P&gt;
&lt;PRE&gt;* s in the group is the non-select s and causes automatic remerging;&lt;BR /&gt;proc sql; 
  select 'way 1' as way, id, t as s, count(*) as freq format=4. from have
  group way, id, s;&lt;/PRE&gt;
&lt;PRE&gt;way    id  s        freq
------------------------
way 1   1  1 other     1
way 1   2  2 other     2
way 1   2  2 other     2
way 1   3  3 other     3
way 1   3  3 other     3
way 1   3  3 other     3
way 1   4  4 other     4
way 1   4  4 other     4
way 1   4  4 other     4
way 1   4  4 other     4&lt;/PRE&gt;
&lt;P&gt;Grouping by column numbers disambiguates the role of &lt;STRONG&gt;t as s&lt;/STRONG&gt;&lt;/P&gt;
&lt;PRE&gt;proc sql; select 'way 2' as way, id, t as s, count(*) as freq format=4. from have 
  group 1, 2, 3;
&lt;/PRE&gt;
&lt;PRE&gt;way    id  s    freq
--------------------
way 2   1  t_1     1
way 2   2  t_2     2
way 2   3  t_3     3
way 2   4  t_4     4&lt;/PRE&gt;
&lt;P&gt;Selecting &lt;STRONG&gt;t as&lt;/STRONG&gt; new column &lt;STRONG&gt;s_new&amp;nbsp;&lt;/STRONG&gt;also disambiguates&lt;/P&gt;
&lt;PRE&gt;proc sql; select 'way 3' as way, id, t as s_new, count(*) as freq format=4. from have
  group way, id, s_new;&lt;/PRE&gt;
&lt;PRE&gt;way    id  &lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;s_new &lt;/STRONG&gt;&lt;/FONT&gt; freq
----------------------
way 3   1  t_1       1
way 3   2  t_2       2
way 3   3  t_3       3
way 3   4  t_4       4&lt;/PRE&gt;
&lt;P&gt;Trying to remain in the context of column names, &lt;STRONG&gt;calculated s&lt;/STRONG&gt; causes an ERROR&lt;/P&gt;
&lt;PRE&gt;proc sql; select 'way 4' as way, id, t as s, count(*) as freq format=4. from have
  group way, id, &lt;STRONG&gt;calculated s&lt;/STRONG&gt;;
&lt;/PRE&gt;
&lt;PRE&gt;&lt;FONT color="#FF0000"&gt;ERROR: The following columns were not found as CALCULATED references in the immediate query: s.&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;My code will have to go the 1,2,3 way.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regardless, is there a syntax modification to have the &lt;STRONG&gt;as&amp;nbsp;s&lt;/STRONG&gt;&amp;nbsp;column in the group clause using the column name &lt;STRONG&gt;s &lt;/STRONG&gt;(in other words something like &lt;STRONG&gt;calculated s&lt;/STRONG&gt; that does not cause an ERROR)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Mar 2025 02:38:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-group-by-problem-when-using-select-as-columns/m-p/962309#M375076</guid>
      <dc:creator>RichardAD</dc:creator>
      <dc:date>2025-03-20T02:38:51Z</dc:date>
    </item>
    <item>
      <title>Re: SQL group by problem when using select as columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-group-by-problem-when-using-select-as-columns/m-p/962310#M375077</link>
      <description>&lt;P&gt;You left the BY keyword out.&amp;nbsp; &amp;nbsp;And you don't need to include a constant variable like your new WAY in the BY group.&amp;nbsp; But those are not the issue I think you are asking about.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Basically SAS is saying that just changing the name is not really making a new variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So you have two choices.&amp;nbsp; Remove the CALCULATED keyword.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select 'way 3' as way
     , id
     , t as s_new
     , count(*) as freq 
from have
group by id, s_new
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you are worried that there is possible ambiguity in the new name, then use the old name in the GROUP BY clause.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select 'way 3' as way
     , id
     , t as s_new
     , count(*) as freq format=4. 
from have
group by id, have.t
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or add some none destructive operation, like TRIM() for character variables or SUM(var,.) for numeric variables, so that it will treat it as calculated.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select 'way 3' as way
     , id
     , trim(t) as s_new
     , count(*) as freq format=4. 
from have
group by id, calculated s_new
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And if all you wanted was the change the variable name you could just use the RENAME= dataset option on the source dataset.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select 'way 3' as way
     , id
     , s_new
     , count(*) as freq format=4. 
from have(rename=(t=s_new))
group by id, s_new
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Mar 2025 03:12:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-group-by-problem-when-using-select-as-columns/m-p/962310#M375077</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-03-20T03:12:46Z</dc:date>
    </item>
  </channel>
</rss>

