<?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: How to pass macro variable in Proc SQL functions in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-pass-macro-variable-in-Proc-SQL-functions/m-p/831515#M328606</link>
    <description>&lt;P&gt;I don't understand this. What is your desired result given this sample data? Please show us.&lt;/P&gt;</description>
    <pubDate>Fri, 02 Sep 2022 08:56:23 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2022-09-02T08:56:23Z</dc:date>
    <item>
      <title>How to pass macro variable in Proc SQL functions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-pass-macro-variable-in-Proc-SQL-functions/m-p/831506#M328599</link>
      <description>&lt;P&gt;Hello SAS Community,&lt;/P&gt;&lt;P&gt;I am facing problem while I am passing the macro variable in mean or any other aggregation function in SAS SQL. It is providing mean of first variable only but I want mean of all the variable and group by with names. Below is the code for your reference:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data new; set sashelp.class;&lt;BR /&gt;if name = 'Alfred' then name = 'Alice';&lt;BR /&gt;If name = 'Janet' then name = 'Judy';&lt;BR /&gt;if name = 'Robert' then name = 'Ronald';&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data a;&lt;BR /&gt;set sashelp.class(keep=Height Weight);&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;proc contents data=a out=b(keep=name);&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;select Name into: Name_4 separated by ','&amp;nbsp; from b;&lt;BR /&gt;quit&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table new_1 as select Name, mean(&amp;amp;Name_4.) as M_&amp;amp;Name_4.&lt;BR /&gt;from new group by name;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;A quick help is much appreciated.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Sep 2022 07:18:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-pass-macro-variable-in-Proc-SQL-functions/m-p/831506#M328599</guid>
      <dc:creator>vishalrajpoot3</dc:creator>
      <dc:date>2022-09-02T07:18:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to pass macro variable in Proc SQL functions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-pass-macro-variable-in-Proc-SQL-functions/m-p/831515#M328606</link>
      <description>&lt;P&gt;I don't understand this. What is your desired result given this sample data? Please show us.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Sep 2022 08:56:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-pass-macro-variable-in-Proc-SQL-functions/m-p/831515#M328606</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-09-02T08:56:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to pass macro variable in Proc SQL functions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-pass-macro-variable-in-Proc-SQL-functions/m-p/831521#M328609</link>
      <description>&lt;P&gt;If you want to calculate means (or other statistics) for multiple variables, PROC SUMMARY is the tool of choice.&lt;/P&gt;
&lt;P&gt;If you have a macro variable containing your variable names (separated by blanks), it's just&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=new nway;
class name;
var &amp;amp;names.;
output out=want mean()=;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 02 Sep 2022 09:23:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-pass-macro-variable-in-Proc-SQL-functions/m-p/831521#M328609</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-09-02T09:23:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to pass macro variable in Proc SQL functions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-pass-macro-variable-in-Proc-SQL-functions/m-p/831540#M328618</link>
      <description>&lt;P&gt;Your current code results in a macro variable &amp;amp;Name_4 with the value: Height,Weight.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table new_1 as select Name, mean(&amp;amp;Name_4.) as M_&amp;amp;Name_4.
from new group by name;
quit ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;After the macro substitution your code is:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table new_1 as select Name, mean(Height,Weight) as M_Height,Weight
from new group by name;
quit ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That code will not calculate the mean of Height.&amp;nbsp; It calculates the mean of height and weight, for each record.&amp;nbsp; Note you should have a warning in your log, because the the expression does not have a summary function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;WARNING: A GROUP BY clause has been transformed into an ORDER BY clause because neither the SELECT
         clause nor the optional HAVING clause of the associated table-expression referenced a
         summary function.
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;If you would approach this with the macro language, you will need a macro to generate SQL code like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table new_1 as select Name
, mean(Height) as M_Height
, mean(Weight) as M_Weight
from new group by name;
quit ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This is a straight forward macro exercise, because the macro language is good at processing lists.&amp;nbsp; So you can take a list of variable names, and use it to generate a list of SQL statements.&amp;nbsp; But as Kurt mentioned, PROC SUMMARY will cost you less code to write.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Sep 2022 12:40:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-pass-macro-variable-in-Proc-SQL-functions/m-p/831540#M328618</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2022-09-02T12:40:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to pass macro variable in Proc SQL functions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-pass-macro-variable-in-Proc-SQL-functions/m-p/831561#M328626</link>
      <description>&lt;P&gt;There are multiple variables I have and multiple aggregation needed. After that I need to group by on names.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Sep 2022 15:16:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-pass-macro-variable-in-Proc-SQL-functions/m-p/831561#M328626</guid>
      <dc:creator>vishalrajpoot3</dc:creator>
      <dc:date>2022-09-02T15:16:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to pass macro variable in Proc SQL functions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-pass-macro-variable-in-Proc-SQL-functions/m-p/831584#M328627</link>
      <description>&lt;P&gt;Note that PROC SQL is NOT the right tool for generating the mean of multiple variables.&amp;nbsp; Use PROC MEANS (aka PROC SUMMARY).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But to fix your current code you need to do two things.&lt;/P&gt;
&lt;P&gt;1) Figure out what SQL code you need to generate.&lt;/P&gt;
&lt;P&gt;2) Change your macro variable to contain that code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data new ;
  set sashelp.class;
  if name = 'Alfred' then name = 'Alice';
  if name = 'Janet' then name = 'Judy';
  if name = 'Robert' then name = 'Ronald';
run;

proc contents noprint data=new(keep=height weight) out=names(keep=name);
run;


proc sql noprint;
select catx(' ','mean(',Name,') as',cats('m_',name))
  into :sql_code separated by ','  
  from names
;
%put &amp;amp;=sql_code ;
create table new_1 as
  select Name, &amp;amp;sql_code
  from new 
  group by name
;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 02 Sep 2022 15:45:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-pass-macro-variable-in-Proc-SQL-functions/m-p/831584#M328627</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-09-02T15:45:06Z</dc:date>
    </item>
  </channel>
</rss>

