<?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 when rename variable, does group use old or new variable name in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-when-rename-variable-does-group-use-old-or-new-variable/m-p/851761#M336682</link>
    <description>&lt;P&gt;With the SAS SQL flavor both syntax options are valid. Other SQL flavors don't allow to reference an alias name (renamed column) in a group by clause.&lt;/P&gt;
&lt;P&gt;As long as the values of the column name and the alias name are the same the result will be the same - but be aware of the difference it can make if that's not the case.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  select 
    -round(age,10) as age2
    ,avg(height) as avg_height
  from sashelp.class
  group by age2
  order by age2
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1672609703500.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/78960i849028C61C72C627/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1672609703500.png" alt="Patrick_0-1672609703500.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  select 
    -round(age,10) as age2
    ,avg(height) as avg_height
  from sashelp.class
  group by age
  order by age
  ;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_1-1672609924790.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/78961i1F32EA0B0FCB4E0B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_1-1672609924790.png" alt="Patrick_1-1672609924790.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  select 
    -round(age,10) as age2
    ,avg(height) as avg_height
  from sashelp.class
  group by age2
  order by age
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1672610471257.png" style="width: 622px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/78964iE120467493F85EDA/image-dimensions/622x27?v=v2" width="622" height="27" role="button" title="Patrick_0-1672610471257.png" alt="Patrick_0-1672610471257.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1672610098852.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/78962i6BFADF1273D5F26E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1672610098852.png" alt="Patrick_0-1672610098852.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With other SQL flavors that don't allow to use an alias name in a group by clause you would likely need to formulate your SQL along the line of below.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  select 
    age2,
    avg(height) as avg_height
  from
  (
    select 
      -round(age,10) as age2
      ,height
    from sashelp.class
  )
  group by age2
  order by age2
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1672610365028.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/78963i81C4317EBC5C8C31/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1672610365028.png" alt="Patrick_0-1672610365028.png" /&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;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 01 Jan 2023 22:01:11 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2023-01-01T22:01:11Z</dc:date>
    <item>
      <title>proc sql when rename variable, does group use old or new variable name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-when-rename-variable-does-group-use-old-or-new-variable/m-p/851757#M336680</link>
      <description>&lt;P&gt;Hi, when renaming a variable in a proc sql, is the correct technique to reference the old or new variable name (see the two variables that have been renamed in the select statement) in the group statement (see sc.book1 and sc.scoreband_new that are currently the variables names in the group statement below)?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Should it be:&lt;/P&gt;
&lt;P&gt;1)&amp;nbsp;group by sc.scorecard_num, book, scoreband&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2) group by sc.scorecard_num, sc.book1, sc.scoreband_new&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
		create table testing as
			select sc.scorecard_num
			          ,sc.book1 as book
                                  ,sc.scoreband_new as scoreband
				,count(*) as Volume
	from arr_banded sc
		left join (select scorecard_num, count(*) as total
                from arr_banded group by scorecard_num) as tot
			on sc.scorecard_num = tot.scorecard_num
		group by sc.scorecard_num, &lt;FONT color="#FF0000"&gt;sc.book1, sc.scoreband_new&lt;/FONT&gt; 
			order by sc.scorecard_num;
	quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 01 Jan 2023 21:24:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-when-rename-variable-does-group-use-old-or-new-variable/m-p/851757#M336680</guid>
      <dc:creator>Justin9</dc:creator>
      <dc:date>2023-01-01T21:24:10Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql when rename variable, does group use old or new variable name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-when-rename-variable-does-group-use-old-or-new-variable/m-p/851761#M336682</link>
      <description>&lt;P&gt;With the SAS SQL flavor both syntax options are valid. Other SQL flavors don't allow to reference an alias name (renamed column) in a group by clause.&lt;/P&gt;
&lt;P&gt;As long as the values of the column name and the alias name are the same the result will be the same - but be aware of the difference it can make if that's not the case.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  select 
    -round(age,10) as age2
    ,avg(height) as avg_height
  from sashelp.class
  group by age2
  order by age2
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1672609703500.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/78960i849028C61C72C627/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1672609703500.png" alt="Patrick_0-1672609703500.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  select 
    -round(age,10) as age2
    ,avg(height) as avg_height
  from sashelp.class
  group by age
  order by age
  ;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_1-1672609924790.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/78961i1F32EA0B0FCB4E0B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_1-1672609924790.png" alt="Patrick_1-1672609924790.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  select 
    -round(age,10) as age2
    ,avg(height) as avg_height
  from sashelp.class
  group by age2
  order by age
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1672610471257.png" style="width: 622px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/78964iE120467493F85EDA/image-dimensions/622x27?v=v2" width="622" height="27" role="button" title="Patrick_0-1672610471257.png" alt="Patrick_0-1672610471257.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1672610098852.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/78962i6BFADF1273D5F26E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1672610098852.png" alt="Patrick_0-1672610098852.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With other SQL flavors that don't allow to use an alias name in a group by clause you would likely need to formulate your SQL along the line of below.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  select 
    age2,
    avg(height) as avg_height
  from
  (
    select 
      -round(age,10) as age2
      ,height
    from sashelp.class
  )
  group by age2
  order by age2
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1672610365028.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/78963i81C4317EBC5C8C31/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1672610365028.png" alt="Patrick_0-1672610365028.png" /&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;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 01 Jan 2023 22:01:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-when-rename-variable-does-group-use-old-or-new-variable/m-p/851761#M336682</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-01-01T22:01:11Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql when rename variable, does group use old or new variable name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-when-rename-variable-does-group-use-old-or-new-variable/m-p/851764#M336683</link>
      <description>Thank you for your reply and explanation!</description>
      <pubDate>Sun, 01 Jan 2023 21:59:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-when-rename-variable-does-group-use-old-or-new-variable/m-p/851764#M336683</guid>
      <dc:creator>Justin9</dc:creator>
      <dc:date>2023-01-01T21:59:52Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql when rename variable, does group use old or new variable name</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-when-rename-variable-does-group-use-old-or-new-variable/m-p/851769#M336684</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/321018"&gt;@Justin9&lt;/a&gt;&amp;nbsp;To add to above: If interfacing with a database you would need the ORDER BY clause in the SQL to ensure that you receive the data sorted on the SAS side. When GROUP BY executes on the SAS side that's not required because GROUP BY will already return the data sorted.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql _method;
  create table test as
  select 
    age as age2
    ,avg(height) as avg_height
  from sashelp.class
  group by age
  order by age
  ;
quit;

proc contents data=test;
run;quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1672611301475.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/78965i08A38ABB1FECF625/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1672611301475.png" alt="Patrick_0-1672611301475.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;IF you still want to use an ORDER BY clause for clarity then use the aliased names so SAS doesn't sort the data twice.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql _method;
  create table test as
  select 
    age as age2
    ,avg(height) as avg_height
  from sashelp.class
  group by age2
  order by age2
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_1-1672611411324.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/78966iA75EA16715B236F8/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_1-1672611411324.png" alt="Patrick_1-1672611411324.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql _method;
  create table test as
  select 
    age as age2
    ,avg(height) as avg_height
  from sashelp.class
  group by age
  order by age
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_2-1672611474167.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/78967i803507704570384E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_2-1672611474167.png" alt="Patrick_2-1672611474167.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 01 Jan 2023 22:24:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-when-rename-variable-does-group-use-old-or-new-variable/m-p/851769#M336684</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-01-01T22:24:49Z</dc:date>
    </item>
  </channel>
</rss>

