<?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 get min max and freqs of every row value grouped by ID? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-min-max-and-freqs-of-every-row-value-grouped-by-ID/m-p/828025#M327081</link>
    <description>&lt;P&gt;I would use either proc summary + merge or one data step with dow-loops:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want1;
   if 0 then set have;
   length min max earned_credits num_tests 8;
   
   min = 99999;

   do _n_ = 1 by 1 until (last.course);
      set have;
      by id course;
    
      min = min(min, grade);
      max = max(max, grade);
      num_tests = sum(num_tests, 1);
      earned_credits = sum(earned_credits, credit);
   end;
   
   do _n_ = 1 by 1 until (last.course);
      set have;
      by id course;
      output;
   end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Depending on the number of observations in your dataset, this will run faster than sql while using less memory.&lt;/P&gt;</description>
    <pubDate>Wed, 10 Aug 2022 05:47:43 GMT</pubDate>
    <dc:creator>andreas_lds</dc:creator>
    <dc:date>2022-08-10T05:47:43Z</dc:date>
    <item>
      <title>How to get min max and freqs of every row value grouped by ID?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-min-max-and-freqs-of-every-row-value-grouped-by-ID/m-p/827879#M327012</link>
      <description>&lt;P&gt;So I have this simple fictive grades of some fictive students:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id course $ grade credit; 
cards;
1 test1 6 5
1 test1 3 0
1 test1 4 0
1 test2 5 0
1 test2 7 5
1 test2 4 0

2 test1 6 5
2 test1 7 5
2 test1 8 5

3 test1 7 5
3 test1 3 0
3 test1 1 0
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which, when run in SAS, outputs this table:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-left" image-alt="have.PNG" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/74290i17777CF4D35D821C/image-size/large?v=v2&amp;amp;px=999" role="button" title="have.PNG" alt="have.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;
&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;
&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;
&lt;P&gt;&lt;BR /&gt;But I want this table as output:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-left" image-alt="want.PNG" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/74291i1B11D3F077EE339B/image-size/large?v=v2&amp;amp;px=999" role="button" title="want.PNG" alt="want.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;
&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;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Can someone help me out? Thanks in advance!&lt;/P&gt;</description>
      <pubDate>Tue, 09 Aug 2022 15:42:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-min-max-and-freqs-of-every-row-value-grouped-by-ID/m-p/827879#M327012</guid>
      <dc:creator>SAS_Question</dc:creator>
      <dc:date>2022-08-09T15:42:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to get min max and freqs of every row value grouped by ID?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-min-max-and-freqs-of-every-row-value-grouped-by-ID/m-p/827911#M327032</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/195211"&gt;@SAS_Question&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PROC SQL could be used to compute those summary statistics and it would automatically "remerge" them back to the observations from dataset HAVE, as you want. The only minor difficulty might be to ensure that the order of observations within an ID-COURSE combination in the output dataset is the same as in HAVE. If this sort order doesn't matter, you can use:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select *, min(grade) as min, max(grade) as max, sum(credit) as earned_credits, n(grade) as n_tests
from have
group by id, course;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(I have abbreviated the variable name &lt;FONT face="courier new,courier"&gt;how_many_times_made_the_test&lt;/FONT&gt; a bit.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If that sort order is an issue, create a sequential number &lt;FONT face="courier new,courier"&gt;_seqno&lt;/FONT&gt; in a preliminary DATA step, insert an ORDER BY clause into the above PROC SQL step&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;order by _seqno;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and drop the number via a dataset option: ... &lt;FONT face="courier new,courier"&gt;want(drop=_seqno)&lt;/FONT&gt; ... See&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Programming/How-can-I-keep-the-order-of-rows-the-same-after-a-left-join/m-p/782921/highlight/true#M249593" target="_blank" rel="noopener"&gt;Re: How can I keep the order of rows the same after a left join?&lt;/A&gt; for a complete code example.&lt;/P&gt;
&lt;P&gt;(Or rely on the undocumented MONOTONIC() function: &lt;FONT face="courier new,courier"&gt;order by monotonic(id);&lt;/FONT&gt;&amp;nbsp;-- not recommended).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Alternatively, use PROC SUMMARY and do the remerging in a DATA step. If dataset HAVE is sorted by ID COURSE (as in your sample data), you can use a BY statement in both steps:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=have;
by id course;
output out=stats(drop=_:) min(grade)=min max(grade)=max sum(credit)=earned_credits n(grade)=n_tests;
run;

data want;
merge have stats;
by id course;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit: Unlike PROC SQL, the SUMMARY procedure has a VAR statement to specify analysis variables. You can use this to shorten the OUTPUT statement a little:&lt;/P&gt;
&lt;PRE&gt;&lt;STRONG&gt;var grade;&lt;/STRONG&gt;
output out=stats(drop=_:) &lt;STRONG&gt;min&lt;/STRONG&gt;=min &lt;STRONG&gt;max&lt;/STRONG&gt;=max sum(credit)=earned_credits &lt;STRONG&gt;n&lt;/STRONG&gt;=n_tests;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Aug 2022 16:59:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-min-max-and-freqs-of-every-row-value-grouped-by-ID/m-p/827911#M327032</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-08-09T16:59:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to get min max and freqs of every row value grouped by ID?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-min-max-and-freqs-of-every-row-value-grouped-by-ID/m-p/828025#M327081</link>
      <description>&lt;P&gt;I would use either proc summary + merge or one data step with dow-loops:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want1;
   if 0 then set have;
   length min max earned_credits num_tests 8;
   
   min = 99999;

   do _n_ = 1 by 1 until (last.course);
      set have;
      by id course;
    
      min = min(min, grade);
      max = max(max, grade);
      num_tests = sum(num_tests, 1);
      earned_credits = sum(earned_credits, credit);
   end;
   
   do _n_ = 1 by 1 until (last.course);
      set have;
      by id course;
      output;
   end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Depending on the number of observations in your dataset, this will run faster than sql while using less memory.&lt;/P&gt;</description>
      <pubDate>Wed, 10 Aug 2022 05:47:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-min-max-and-freqs-of-every-row-value-grouped-by-ID/m-p/828025#M327081</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2022-08-10T05:47:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to get min max and freqs of every row value grouped by ID?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-min-max-and-freqs-of-every-row-value-grouped-by-ID/m-p/828144#M327126</link>
      <description>&lt;P&gt;@FreelanceReinhard and&amp;nbsp;@andreas_lds you guys both deserve my respect for your solutions and for your help!! Thank you both!&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But I have to go with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;&amp;nbsp;solution becayse that one is pure SAS-code. I like that very much. I know SQL is powerfull but I like the simplicity and power of the SAS-code solutions ! Very neat! Thank you both!! Really appriciate the help of both of you guys!!!!!&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Aug 2022 18:52:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-min-max-and-freqs-of-every-row-value-grouped-by-ID/m-p/828144#M327126</guid>
      <dc:creator>SAS_Question</dc:creator>
      <dc:date>2022-08-10T18:52:06Z</dc:date>
    </item>
  </channel>
</rss>

