<?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 print mean of a variable in the title of the report in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-print-mean-of-a-variable-in-the-title-of-the-report/m-p/481692#M274612</link>
    <description>&lt;P&gt;Hey, I am trying to do something similar i.e. create a report with the mean of a variable (price) in the title but I wanted to know how would you create a macro variable for the mean to reference in the title without the proc sql step. would it be easier to do with proc means or proc report?&lt;/P&gt;</description>
    <pubDate>Thu, 26 Jul 2018 21:31:03 GMT</pubDate>
    <dc:creator>mjabed600</dc:creator>
    <dc:date>2018-07-26T21:31:03Z</dc:date>
    <item>
      <title>How to print mean of a variable in the title of the report</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-print-mean-of-a-variable-in-the-title-of-the-report/m-p/360053#M274604</link>
      <description>&lt;P&gt;I am a beginner in SAS macro and using SAS University Edition. I am trying to write a macro to print a report. This macro&amp;nbsp;will print mean of the variable "Price" in the title of the report. Now there are ways to produce mean in proc report or proc summary but I am not sure how I can calculate mean within the data procedure . I am assuming that I need to calculate the mean in the data procedure and then assign a macro variable so that I can include that in the title of the report.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So my main problem is calculating the mean in a way so that I can put it in the report title. Any help, clue are hugely appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's the sample data-&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data exprev;&lt;BR /&gt;input Emp_ID $ 1-10 Sale_Type 14-16 Quantity Price Cost;&lt;/P&gt;&lt;P&gt;datalines;&lt;BR /&gt;99999999 1 2 92.60 20.70&lt;BR /&gt;99999999 2 14 51.20 12.10&lt;BR /&gt;99999999 3 25 31.10 15.65&lt;BR /&gt;99999999 2 30 123.70 59.00&lt;BR /&gt;99999999 2 8 113.40 28.45&lt;BR /&gt;99999999 2 7 41.00 9.25&lt;BR /&gt;120458 3 2 146.40 36.70&lt;BR /&gt;99999999 2 11 40.20 20.20&lt;BR /&gt;99999999 2 100 11.80 5.00&lt;BR /&gt;120454 3 20 71.00 32.30&lt;BR /&gt;120845 1 19 64.30 28.65&lt;BR /&gt;120538 1 22 110.80 29.35&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And here's my macro code-&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro newreport(type);&lt;BR /&gt;proc print data=work.exprev n='Number of observations for the order type: ';&lt;BR /&gt;sum quantity price;&lt;BR /&gt;where Sale_Type = &amp;amp;type;&lt;BR /&gt;title "Mean price for Sale_Type &amp;amp;type is : ";&lt;BR /&gt;run;&lt;BR /&gt;%mend;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%newreport(3)&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2017 17:24:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-print-mean-of-a-variable-in-the-title-of-the-report/m-p/360053#M274604</guid>
      <dc:creator>shihabur</dc:creator>
      <dc:date>2017-05-19T17:24:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to print mean of a variable in the title of the report</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-print-mean-of-a-variable-in-the-title-of-the-report/m-p/360062#M274605</link>
      <description>&lt;P&gt;PROC SQL gives you the mean as a macro variable&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql;
     select mean(price) into :meanvalue from exprev;
quit;&lt;/PRE&gt;
&lt;P&gt;Also, I don't think you want &amp;amp;type to be both in the title and the statement above which checks to see if sale_type is a certain value. Your title needs a different macro variable that has the mean value, in the above example your title would refer to &amp;amp;meanvalue&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2017 17:40:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-print-mean-of-a-variable-in-the-title-of-the-report/m-p/360062#M274605</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2017-05-19T17:40:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to print mean of a variable in the title of the report</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-print-mean-of-a-variable-in-the-title-of-the-report/m-p/360068#M274606</link>
      <description>&lt;P&gt;Note: Code really should be pasted in the code box opened using the the forum's {i} icon on the menu above. The main message window will reformat pasted text. In this case all of the rows of data as pasted have errors because columns 1-10 as in you input actually read the first two fields into the Emp_id variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This will create a macro variable named mean var you could reference in the Title&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql noprint;
   select mean(price) into :Meanvar
   from exprev;
run;&lt;/PRE&gt;
&lt;P&gt;However that will likely have more digits than makes sense so you may want&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql noprint;
   select put(mean(price),7.2) into :Meanvar
   from exprev&lt;BR /&gt;   where type=&amp;amp;type;
run;&lt;/PRE&gt;
&lt;P&gt;to round the result to 2 decimals. This step should be in your macro before the print&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your title statement would look like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;title "Mean price for Sale_Type &amp;amp;type is : &amp;amp;meanvar&amp;nbsp;";&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2017 17:50:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-print-mean-of-a-variable-in-the-title-of-the-report/m-p/360068#M274606</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-05-19T17:50:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to print mean of a variable in the title of the report</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-print-mean-of-a-variable-in-the-title-of-the-report/m-p/360069#M274607</link>
      <description>&lt;P&gt;Thanks a lot. This works.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2017 17:51:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-print-mean-of-a-variable-in-the-title-of-the-report/m-p/360069#M274607</guid>
      <dc:creator>shihabur</dc:creator>
      <dc:date>2017-05-19T17:51:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to print mean of a variable in the title of the report</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-print-mean-of-a-variable-in-the-title-of-the-report/m-p/360074#M274608</link>
      <description>&lt;P&gt;Thanks for the note. Will do that next time for sure. Your suggestion was really helpful. Thanks for that too.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2017 18:13:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-print-mean-of-a-variable-in-the-title-of-the-report/m-p/360074#M274608</guid>
      <dc:creator>shihabur</dc:creator>
      <dc:date>2017-05-19T18:13:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to print mean of a variable in the title of the report</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-print-mean-of-a-variable-in-the-title-of-the-report/m-p/360082#M274609</link>
      <description>&lt;P&gt;This is my final code&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;%macro report3(type);
proc sql noprint;
   select put(mean(price),7.2) into :Meanvar from work.exprev;
   where Sale_Type=&amp;amp;type;
run;
proc print data=work.exprev n='Number of observations for the order type: '; 
sum price;
where Sale_Type=&amp;amp;type;
Title "Mean price for Sale_Type &amp;amp;type is : &amp;amp;Meanvar ";
run;
%mend;

%report3(2)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And this is the output. You can see clearly that the mean is supposed to be 381.3/6 = 63.55 instead of 74.79. It seems to me that the PROC SQL statement is ignoring the where clause and calculating mean for the entire data set i.e. all the sales_type instead of only sales_type 2. Can you kindly tell me where I am making mistake ?&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/8966i015964E9CC7A55F5/image-size/original?v=1.0&amp;amp;px=-1" border="0" alt="Screen Shot 2017-05-19 at 2.51.40 PM.png" title="Screen Shot 2017-05-19 at 2.51.40 PM.png" /&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2017 18:56:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-print-mean-of-a-variable-in-the-title-of-the-report/m-p/360082#M274609</guid>
      <dc:creator>shihabur</dc:creator>
      <dc:date>2017-05-19T18:56:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to print mean of a variable in the title of the report</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-print-mean-of-a-variable-in-the-title-of-the-report/m-p/360085#M274610</link>
      <description>&lt;P&gt;PROC SQL is ignoring the where clause because you have a semi-colon where it does not belong.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It should be:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql noprint;
   select put(mean(price),7.2) into :Meanvar from work.exprev
   where Sale_Type=&amp;amp;type;
run;&lt;/PRE&gt;</description>
      <pubDate>Fri, 19 May 2017 19:00:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-print-mean-of-a-variable-in-the-title-of-the-report/m-p/360085#M274610</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2017-05-19T19:00:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to print mean of a variable in the title of the report</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-print-mean-of-a-variable-in-the-title-of-the-report/m-p/360088#M274611</link>
      <description>&lt;P&gt;Thanks a lot.&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2017 19:13:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-print-mean-of-a-variable-in-the-title-of-the-report/m-p/360088#M274611</guid>
      <dc:creator>shihabur</dc:creator>
      <dc:date>2017-05-19T19:13:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to print mean of a variable in the title of the report</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-print-mean-of-a-variable-in-the-title-of-the-report/m-p/481692#M274612</link>
      <description>&lt;P&gt;Hey, I am trying to do something similar i.e. create a report with the mean of a variable (price) in the title but I wanted to know how would you create a macro variable for the mean to reference in the title without the proc sql step. would it be easier to do with proc means or proc report?&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jul 2018 21:31:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-print-mean-of-a-variable-in-the-title-of-the-report/m-p/481692#M274612</guid>
      <dc:creator>mjabed600</dc:creator>
      <dc:date>2018-07-26T21:31:03Z</dc:date>
    </item>
  </channel>
</rss>

