<?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: Outputting by sum with proc freq in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Outputting-by-sum-with-proc-freq/m-p/948373#M83720</link>
    <description>&lt;P&gt;So with example data we can begin to get an idea what you are trying to do.&amp;nbsp; Looks like you have grouping variables (in this case 3 of them: Make Drivetrain Type) that uniquely identify the groups.&amp;nbsp; &amp;nbsp;And you have a CLASS variable (in this case: Model) to subdivide the groups.&amp;nbsp; And an analysis variable (in this case MSRP) that you want to SUM.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One obvious savings is to use the output of the first summary (at the BY GROUP* CLASS VARIABLE level) to calculate the BY GROUP sums.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In your example that would be changing the step that makes CARS_MEANS to use CARS_FREQ as the input.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=cars_freq ;
  by make drivetrain type;
  var count ;
  output out=cars_means(drop=_:) sum=MSRP_sum_carstate;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It just doesn't attach the DOLLAR format to the variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is how you can use one PROC SUMMARY (MEANS and SUMMARY are the same procedure, they just use different defaults for the PRINT/NOPRINT option) and one data step to calculate your COUNT, PERCENT and&amp;nbsp;MSRP_sum_carstate variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=sashelp.cars chartype ;
  class Make Drivetrain Type Model ;
  types Make*Drivetrain*Type Make*Drivetrain*Type*Model;
  output out=step1 sum(MSRP)=COUNT ;
run;

data want;
  merge step1(where=(not indexc(_type_,'0')))
        step1(where=(indexc(_type_,'0')) drop=Model rename=(COUNT=MSRP_sum_carstate))
  ;
  by Make Drivetrain Type;
  PERCENT=100*(COUNT/MSRP_sum_carstate);
  drop _type_ _freq_ ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Let's test and see if they make the same values (within 6 decimal places).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc compare data=want compare=final method=absolute criterion=10E-6;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;Variables Summary

Number of Variables in Common: 7.
Number of Variables with Differing Attributes: 2.


Listing of Common Variables with Differing Attributes

Variable  Dataset     Type  Length  Format    Label

COUNT     WORK.WANT   Num        8  DOLLAR8.
          WORK.FINAL  Num        8            Frequency Count
PERCENT   WORK.WANT   Num        8
          WORK.FINAL  Num        8            Percent of Total Frequency


Observation Summary

Observation      Base  Compare

First Obs           1        1
Last  Obs         428      428

Number of Observations in Common: 428.
Total Number of Observations Read from WORK.WANT: 428.
Total Number of Observations Read from WORK.FINAL: 428.

Number of Observations with Some Compared Variables Unequal: 0.
Number of Observations with All Compared Variables Equal: 428.


Values Comparison Summary

Number of Variables Compared with All Observations Equal: 7.
Number of Variables Compared with Some Observations Unequal: 0.
Total Number of Values which Compare Unequal: 0.
Total Number of Values not EXACTLY Equal: 141.
Maximum Difference: 1.4211E-14.
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 21 Oct 2024 12:52:46 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2024-10-21T12:52:46Z</dc:date>
    <item>
      <title>Outputting by sum with proc freq</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Outputting-by-sum-with-proc-freq/m-p/948058#M83713</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the following code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;proc freq data=inputds noprint;
	by notsorted a b c;
	tables x*y*z/out=freq;
	weight count/zeros;
run;

proc means data=inputds completetypes noprint nway;
	by notsorted a b c;
	var count;
	output out=means(drop=_:) sum=count_sum_c;
run;

data final;
	merge freq means;
	by notsorted a b c;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Is it possible to produce the dataset in one single step? having a notsorted means that the merge is not possible, and due to the fact that the input and freq datasets contain over 90 million rows, I would prefer not to have to sort anything unless absolutely unavoidable. Thank you for your tips in advance.&lt;/P&gt;</description>
      <pubDate>Fri, 18 Oct 2024 14:39:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Outputting-by-sum-with-proc-freq/m-p/948058#M83713</guid>
      <dc:creator>js5</dc:creator>
      <dc:date>2024-10-18T14:39:44Z</dc:date>
    </item>
    <item>
      <title>Re: Outputting by sum with proc freq</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Outputting-by-sum-with-proc-freq/m-p/948068#M83714</link>
      <description>&lt;P&gt;Without data or even a description of what you are trying to do it is going to be hard.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why not just let PROC MEANS add up the WEIGHT variable for you (especially since you want to include the zero weight observations)?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc means data=inputds completetypes noprint nway;
  class a b c;
  output out=means(drop=_:) n=nobs sum(count)=count_sum_c;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 18 Oct 2024 15:10:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Outputting-by-sum-with-proc-freq/m-p/948068#M83714</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-10-18T15:10:20Z</dc:date>
    </item>
    <item>
      <title>Re: Outputting by sum with proc freq</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Outputting-by-sum-with-proc-freq/m-p/948069#M83715</link>
      <description>&lt;P&gt;You might describe what you are wanting as output and do you actually need a data set? If the data set is not used for anything other than reporting it may be that a report procedure is the way to go.&lt;/P&gt;
&lt;P&gt;And providing data in the form of a working data step usually helps.&lt;/P&gt;
&lt;P&gt;Provide a subset and what you expect as a final result for that subset of data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am a bit concerned that you may not be getting what you want as output to begin with by using not sorted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;IF two data sets have the same number of observations you might try as without a BY then SAS does a line by line merge regardless of values. The output of the two data sets may be in the same order because of the way SAS process notsorted data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;data final;
	merge freq means;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Oct 2024 15:16:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Outputting-by-sum-with-proc-freq/m-p/948069#M83715</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-10-18T15:16:29Z</dc:date>
    </item>
    <item>
      <title>Re: Outputting by sum with proc freq</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Outputting-by-sum-with-proc-freq/m-p/948331#M83716</link>
      <description>&lt;P&gt;I do need a dataset as output. The two datasets do not have the same number of observations unfortunately. Here is a simplified example using sashelp.cars:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;proc sort data=sashelp.cars out=cars_sorted;
by Make Drivetrain Type;
run;

proc freq data=cars_sorted noprint;
	by Make Drivetrain Type;
	tables Model/out=cars_freq;
	weight MSRP/zeros;
run;

proc means data=cars_sorted completetypes noprint nway;
	by Make Drivetrain Type;
	var MSRP;
	output out=cars_means(drop=_:) sum=MSRP_sum_carstate;
run;

data final;
	merge cars_freq cars_means;
	by Make Drivetrain Type;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I am in the end interested in frequencies per by group (COUNT) as well as the denominator used to calculate them (MSRP_sum_carstate). Sorting before turned out not to be that resource-intensive after all, but using one step instead of two would still be cleaner.&lt;/P&gt;</description>
      <pubDate>Mon, 21 Oct 2024 06:38:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Outputting-by-sum-with-proc-freq/m-p/948331#M83716</guid>
      <dc:creator>js5</dc:creator>
      <dc:date>2024-10-21T06:38:37Z</dc:date>
    </item>
    <item>
      <title>Re: Outputting by sum with proc freq</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Outputting-by-sum-with-proc-freq/m-p/948332#M83717</link>
      <description>&lt;P&gt;This lacks the frequencies per a*b combination unfortunately&lt;/P&gt;</description>
      <pubDate>Mon, 21 Oct 2024 06:42:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Outputting-by-sum-with-proc-freq/m-p/948332#M83717</guid>
      <dc:creator>js5</dc:creator>
      <dc:date>2024-10-21T06:42:09Z</dc:date>
    </item>
    <item>
      <title>Re: Outputting by sum with proc freq</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Outputting-by-sum-with-proc-freq/m-p/948362#M83718</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/223838"&gt;@js5&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;This lacks the frequencies per a*b combination unfortunately&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Again, without actual data or an explanation I have no idea what that means.&lt;/P&gt;</description>
      <pubDate>Mon, 21 Oct 2024 12:12:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Outputting-by-sum-with-proc-freq/m-p/948362#M83718</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-10-21T12:12:56Z</dc:date>
    </item>
    <item>
      <title>Re: Outputting by sum with proc freq</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Outputting-by-sum-with-proc-freq/m-p/948364#M83719</link>
      <description>&lt;P&gt;Does&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Procedures/Outputting-by-sum-with-proc-freq/m-p/948331#M83716" target="_blank"&gt;https://communities.sas.com/t5/SAS-Procedures/Outputting-by-sum-with-proc-freq/m-p/948331#M83716&lt;/A&gt;&amp;nbsp;help?&lt;/P&gt;</description>
      <pubDate>Mon, 21 Oct 2024 12:16:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Outputting-by-sum-with-proc-freq/m-p/948364#M83719</guid>
      <dc:creator>js5</dc:creator>
      <dc:date>2024-10-21T12:16:48Z</dc:date>
    </item>
    <item>
      <title>Re: Outputting by sum with proc freq</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Outputting-by-sum-with-proc-freq/m-p/948373#M83720</link>
      <description>&lt;P&gt;So with example data we can begin to get an idea what you are trying to do.&amp;nbsp; Looks like you have grouping variables (in this case 3 of them: Make Drivetrain Type) that uniquely identify the groups.&amp;nbsp; &amp;nbsp;And you have a CLASS variable (in this case: Model) to subdivide the groups.&amp;nbsp; And an analysis variable (in this case MSRP) that you want to SUM.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One obvious savings is to use the output of the first summary (at the BY GROUP* CLASS VARIABLE level) to calculate the BY GROUP sums.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In your example that would be changing the step that makes CARS_MEANS to use CARS_FREQ as the input.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=cars_freq ;
  by make drivetrain type;
  var count ;
  output out=cars_means(drop=_:) sum=MSRP_sum_carstate;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It just doesn't attach the DOLLAR format to the variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is how you can use one PROC SUMMARY (MEANS and SUMMARY are the same procedure, they just use different defaults for the PRINT/NOPRINT option) and one data step to calculate your COUNT, PERCENT and&amp;nbsp;MSRP_sum_carstate variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=sashelp.cars chartype ;
  class Make Drivetrain Type Model ;
  types Make*Drivetrain*Type Make*Drivetrain*Type*Model;
  output out=step1 sum(MSRP)=COUNT ;
run;

data want;
  merge step1(where=(not indexc(_type_,'0')))
        step1(where=(indexc(_type_,'0')) drop=Model rename=(COUNT=MSRP_sum_carstate))
  ;
  by Make Drivetrain Type;
  PERCENT=100*(COUNT/MSRP_sum_carstate);
  drop _type_ _freq_ ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Let's test and see if they make the same values (within 6 decimal places).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc compare data=want compare=final method=absolute criterion=10E-6;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;Variables Summary

Number of Variables in Common: 7.
Number of Variables with Differing Attributes: 2.


Listing of Common Variables with Differing Attributes

Variable  Dataset     Type  Length  Format    Label

COUNT     WORK.WANT   Num        8  DOLLAR8.
          WORK.FINAL  Num        8            Frequency Count
PERCENT   WORK.WANT   Num        8
          WORK.FINAL  Num        8            Percent of Total Frequency


Observation Summary

Observation      Base  Compare

First Obs           1        1
Last  Obs         428      428

Number of Observations in Common: 428.
Total Number of Observations Read from WORK.WANT: 428.
Total Number of Observations Read from WORK.FINAL: 428.

Number of Observations with Some Compared Variables Unequal: 0.
Number of Observations with All Compared Variables Equal: 428.


Values Comparison Summary

Number of Variables Compared with All Observations Equal: 7.
Number of Variables Compared with Some Observations Unequal: 0.
Total Number of Values which Compare Unequal: 0.
Total Number of Values not EXACTLY Equal: 141.
Maximum Difference: 1.4211E-14.
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Oct 2024 12:52:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Outputting-by-sum-with-proc-freq/m-p/948373#M83720</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-10-21T12:52:46Z</dc:date>
    </item>
  </channel>
</rss>

