<?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 REPORT - subset WHERE or IF THEN within a compute block to calculate statistics in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/PROC-REPORT-subset-WHERE-or-IF-THEN-within-a-compute-block-to/m-p/838051#M331391</link>
    <description>&lt;P&gt;Thank you Kurt and Ksharp, this works.&lt;/P&gt;</description>
    <pubDate>Wed, 12 Oct 2022 13:47:47 GMT</pubDate>
    <dc:creator>DougHold</dc:creator>
    <dc:date>2022-10-12T13:47:47Z</dc:date>
    <item>
      <title>PROC REPORT - subset WHERE or IF THEN within a compute block to calculate statistics</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-REPORT-subset-WHERE-or-IF-THEN-within-a-compute-block-to/m-p/837976#M331344</link>
      <description>&lt;P&gt;I am using the SAShelp cars data. Here I am creating a report of the mean MSRP subset according to Origin and DriveTrain BY whether the cars have horsepower greater than 200 or less than equal to 200.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The following gives me almost what I want, but I would like it to appear together in one table rather than two separate tables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA HP_Cars;
	SET sashelp.cars;
	IF Horsepower &amp;gt; 200 THEN HP200 = 1;
	                    ELSE HP200 = 0;
RUN;

PROC SORT DATA=Hp_cars;
	BY HP200;
RUN;

ODS PDF;
PROC REPORT DATA = HP_Cars    OUT=Cars_report;
	Title "Cars Report mean MSRP";
	    BY HP200; 
	COLUMN 
        Origin
        DriveTrain
		MSRP = mean_MSRP
		;    
	DEFINE Origin        / group;
	DEFINE DriveTrain    / group;
	DEFINE mean_MSRP     / analysis mean "mean_MSRP";

	BREAK AFTER Origin / summarize style=[background=graydd];
		COMPUTE AFTER Origin;
			DriveTrain='Any';
		ENDCOMP;

	RBREAK AFTER / summarize style=[background=lightblue];
		COMPUTE AFTER;
			Origin='Any';
			DriveTrain='Any';
		ENDCOMP;
RUN;
ODS PDF CLOSE;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Alternatively to the above, is it possible to subset (i.e. using WHERE or IF THEN) within a compute block to calculate the means? The following doesn't actually work, but for the purpose of learning, I would like to know if it's even possible to do something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ODS PDF;
PROC REPORT DATA = HP_Cars    OUT=Cars_report;
	Title "Cars Report mean MSRP";
    COLUMN 
        Origin
        DriveTrain
		MSRP = mean_MSRP
		HP200
        mean_MSRP_HP200plus
		mean_MSRP_HP200less
		;    
	DEFINE Origin                / group;
	DEFINE DriveTrain            / group;
	DEFINE mean_MSRP             / analysis mean noprint;
	DEFINE HP200                 / computed noprint;
	DEFINE mean_MSRP_HP200plus   / computed "mean_MSRP HP&amp;gt;200";
    DEFINE mean_MSRP_HP200less   / computed "mean_MSRP HP&amp;lt;=200";

    COMPUTE mean_MSRP_HP200plus;
	     IF HP200=1 THEN mean_MSRP_HP200plus = MEAN(MSRP);     *calculate mean MSRP for HP &amp;gt; 200; 
    ENDCOMP;

	COMPUTE mean_MSRP_HP200less;
	     IF HP200=0 THEN mean_MSRP_HP200less = MEAN(MSRP);     *calculate mean MSRP for HP &amp;lt;= 200; 
    ENDCOMP;

	BREAK AFTER Origin / summarize style=[background=graydd];
		COMPUTE AFTER Origin;
			DriveTrain='Any';
		ENDCOMP;

	RBREAK AFTER / summarize style=[background=lightblue];
		COMPUTE AFTER;
			Origin='Any';
			DriveTrain='Any';
		ENDCOMP;
RUN;
ODS PDF CLOSE;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;What I want is this:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="DougHold_1-1665542138811.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/76101i469FB5F6AAF0B380/image-size/medium?v=v2&amp;amp;px=400" role="button" title="DougHold_1-1665542138811.png" alt="DougHold_1-1665542138811.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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Oct 2022 02:39:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-REPORT-subset-WHERE-or-IF-THEN-within-a-compute-block-to/m-p/837976#M331344</guid>
      <dc:creator>DougHold</dc:creator>
      <dc:date>2022-10-12T02:39:37Z</dc:date>
    </item>
    <item>
      <title>Re: PROC REPORT - subset WHERE or IF THEN within a compute block to calculate statistics</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-REPORT-subset-WHERE-or-IF-THEN-within-a-compute-block-to/m-p/837984#M331348</link>
      <description>&lt;P&gt;Apply a format to HP200 which displays the texts, and use HP200 as an ACROSS variable:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data HP_Cars;
set sashelp.cars;
hp200 = Horsepower &amp;gt; 200;
run;

proc format;
value hp200_ /* underline is needed because a format name must not end with a digit */
  0 = "mean_MSRP HP&amp;lt;=200"
  1 = "mean_MSRP HP&amp;gt;200"
;
run;

proc report data=hp_cars;
column origin drivetrain msrp,hp200;
define origin / group;
define drivetrain /group;
define msrp / "" analysis mean;
define hp200 / "" across format=hp200_.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 12 Oct 2022 06:50:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-REPORT-subset-WHERE-or-IF-THEN-within-a-compute-block-to/m-p/837984#M331348</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-10-12T06:50:52Z</dc:date>
    </item>
    <item>
      <title>Re: PROC REPORT - subset WHERE or IF THEN within a compute block to calculate statistics</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-REPORT-subset-WHERE-or-IF-THEN-within-a-compute-block-to/m-p/838014#M331370</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*
As Kurt point out ,Adding a format and a ACROSS variable
*/

proc format;
value hp200_ /* underline is needed because a format name must not end with a digit */
  0 = "mean_MSRP HP&amp;lt;=200"
  1 = "mean_MSRP HP&amp;gt;200"
;
run;

DATA HP_Cars;
 SET sashelp.cars;
 IF Horsepower &amp;gt; 200 THEN HP200 = 1;
                     ELSE HP200 = 0;
RUN;

PROC SORT DATA=Hp_cars;
 BY HP200;
RUN;


PROC REPORT DATA = HP_Cars   nowd OUT=Cars_report;
 Title "Cars Report mean MSRP";
 COLUMN 
        Origin
        DriveTrain
  MSRP,HP200 
  ;    
    define HP200 /across format=hp200_. "";
 DEFINE Origin        / group;
 DEFINE DriveTrain    / group;
 DEFINE MSRP     / analysis mean "";

 BREAK AFTER Origin / summarize style=[background=graydd];
  COMPUTE AFTER Origin;
   DriveTrain='Any';
  ENDCOMP;

 RBREAK AFTER / summarize style=[background=lightblue];
  COMPUTE AFTER ;
   Origin='Any';
   DriveTrain='Any';
  ENDCOMP;

RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_0-1665574660143.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/76106iB35395AAC9A7F885/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_0-1665574660143.png" alt="Ksharp_0-1665574660143.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Oct 2022 11:37:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-REPORT-subset-WHERE-or-IF-THEN-within-a-compute-block-to/m-p/838014#M331370</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-10-12T11:37:19Z</dc:date>
    </item>
    <item>
      <title>Re: PROC REPORT - subset WHERE or IF THEN within a compute block to calculate statistics</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-REPORT-subset-WHERE-or-IF-THEN-within-a-compute-block-to/m-p/838051#M331391</link>
      <description>&lt;P&gt;Thank you Kurt and Ksharp, this works.&lt;/P&gt;</description>
      <pubDate>Wed, 12 Oct 2022 13:47:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-REPORT-subset-WHERE-or-IF-THEN-within-a-compute-block-to/m-p/838051#M331391</guid>
      <dc:creator>DougHold</dc:creator>
      <dc:date>2022-10-12T13:47:47Z</dc:date>
    </item>
  </channel>
</rss>

