<?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 Find max/min grouped by two data fields and replace the missing data with them in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Find-max-min-grouped-by-two-data-fields-and-replace-the-missing/m-p/909654#M358775</link>
    <description>&lt;P&gt;Below is my input data&lt;/P&gt;&lt;PRE&gt;data have;
   input Col1 $ Col2 Col3;
datalines;
A 1 2
A 3 4
A 4 8
A 6 5
B 1 .
B 2 .
B 4 .
B 5 .
;&lt;/PRE&gt;&lt;P&gt;Now I would like to fill the Col3 data for B, based on the matched Col2 for A. If any Col2 data for B are missing from A, either the max or min of Col2 values from entire records for A will be used.&lt;/P&gt;&lt;P&gt;Hence, the output of B should be:&lt;/P&gt;&lt;P&gt;&amp;nbsp; B&amp;nbsp; 1&amp;nbsp; 2&lt;/P&gt;&lt;P&gt;&amp;nbsp; B 2&amp;nbsp; 8 for max or 2 for min&lt;/P&gt;&lt;P&gt;&amp;nbsp; B 4&amp;nbsp; 8&lt;/P&gt;&lt;P&gt;&amp;nbsp; B 5&amp;nbsp; 8 for max or 2 for min&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks for your help.&lt;/P&gt;</description>
    <pubDate>Mon, 25 Dec 2023 19:47:48 GMT</pubDate>
    <dc:creator>fwu811</dc:creator>
    <dc:date>2023-12-25T19:47:48Z</dc:date>
    <item>
      <title>Find max/min grouped by two data fields and replace the missing data with them</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-max-min-grouped-by-two-data-fields-and-replace-the-missing/m-p/909654#M358775</link>
      <description>&lt;P&gt;Below is my input data&lt;/P&gt;&lt;PRE&gt;data have;
   input Col1 $ Col2 Col3;
datalines;
A 1 2
A 3 4
A 4 8
A 6 5
B 1 .
B 2 .
B 4 .
B 5 .
;&lt;/PRE&gt;&lt;P&gt;Now I would like to fill the Col3 data for B, based on the matched Col2 for A. If any Col2 data for B are missing from A, either the max or min of Col2 values from entire records for A will be used.&lt;/P&gt;&lt;P&gt;Hence, the output of B should be:&lt;/P&gt;&lt;P&gt;&amp;nbsp; B&amp;nbsp; 1&amp;nbsp; 2&lt;/P&gt;&lt;P&gt;&amp;nbsp; B 2&amp;nbsp; 8 for max or 2 for min&lt;/P&gt;&lt;P&gt;&amp;nbsp; B 4&amp;nbsp; 8&lt;/P&gt;&lt;P&gt;&amp;nbsp; B 5&amp;nbsp; 8 for max or 2 for min&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks for your help.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Dec 2023 19:47:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-max-min-grouped-by-two-data-fields-and-replace-the-missing/m-p/909654#M358775</guid>
      <dc:creator>fwu811</dc:creator>
      <dc:date>2023-12-25T19:47:48Z</dc:date>
    </item>
    <item>
      <title>Re: Find max/min grouped by two data fields and replace the missing data with them</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-max-min-grouped-by-two-data-fields-and-replace-the-missing/m-p/909656#M358777</link>
      <description>&lt;P&gt;Find max/min grouped by data fields (I will call these fields FIELD1 and FIELD2)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=have nway;
    class field1 field2;
    var yourvarname;
    output out=want min= max=/autoname;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Replace missing data with them ... more explanation is needed. Missing data can be replaced by one value, but missing data cannot be replaced by "them" (which indicates to me replace the missing with both the max and the min).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the future, please give a more complete explanation of the problem, rather than just the bare minimum that you have typed in this case.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Dec 2023 19:01:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-max-min-grouped-by-two-data-fields-and-replace-the-missing/m-p/909656#M358777</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-12-25T19:01:02Z</dc:date>
    </item>
    <item>
      <title>Re: Find max/min grouped by two data fields and replace the missing data with them</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-max-min-grouped-by-two-data-fields-and-replace-the-missing/m-p/909657#M358778</link>
      <description>&lt;P&gt;Is there a question here?&lt;/P&gt;
&lt;P&gt;What is your input data?&lt;/P&gt;
&lt;P&gt;What output do you want for that particular input?&lt;BR /&gt;What missing data do you want to replace? What are the rules for determining what to replace it with?&lt;/P&gt;</description>
      <pubDate>Mon, 25 Dec 2023 19:02:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-max-min-grouped-by-two-data-fields-and-replace-the-missing/m-p/909657#M358778</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-12-25T19:02:36Z</dc:date>
    </item>
    <item>
      <title>Re: Find max/min grouped by two data fields and replace the missing data with them</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-max-min-grouped-by-two-data-fields-and-replace-the-missing/m-p/909658#M358779</link>
      <description>Many thanks for your quick reply.&lt;BR /&gt;&lt;BR /&gt;I just added an example to my question.&lt;BR /&gt;&lt;BR /&gt;Thank you again.</description>
      <pubDate>Mon, 25 Dec 2023 19:50:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-max-min-grouped-by-two-data-fields-and-replace-the-missing/m-p/909658#M358779</guid>
      <dc:creator>fwu811</dc:creator>
      <dc:date>2023-12-25T19:50:15Z</dc:date>
    </item>
    <item>
      <title>Re: Find max/min grouped by two data fields and replace the missing data with them</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-max-min-grouped-by-two-data-fields-and-replace-the-missing/m-p/909660#M358781</link>
      <description>&lt;P&gt;Thank you for providing a working DATA step with sample data.&amp;nbsp; You haven't provided a rule for determining when to replace missing values of col3 with the max value vs the min, so this code always assigns the max.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:);
  set have;
  if _n_=1 then do;
    declare hash h ();
      h.definekey('col2');
      h.definedata('col3');
      h.definedone();
  end;
  if col1='A' then do;
    h.add();
    _min=min(_min,col3);
    _max=max(_max,col3);
    retain _min _max;
  end;
  else if h.find() ^=0 then col3=_max;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It assumes that all the COL='A' observations are at the beginning of the data set, and that all other obs need to use the data from 'A' to determine col3.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Dec 2023 20:40:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-max-min-grouped-by-two-data-fields-and-replace-the-missing/m-p/909660#M358781</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-12-25T20:40:44Z</dc:date>
    </item>
    <item>
      <title>Re: Find max/min grouped by two data fields and replace the missing data with them</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-max-min-grouped-by-two-data-fields-and-replace-the-missing/m-p/909665#M358783</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   input Col1 $ Col2 Col3;
datalines;
A 1 2
A 3 4
A 4 8
A 6 5
B 1 .
B 2 .
B 4 .
B 5 .
;
proc sql;
create table want as
select Col1,Col2,coalesce(
(select Col3 from have where col1='A' and col2=a.col2),
(select max(Col3) from have  where col1='A')
) as Col3
 from have as a;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 26 Dec 2023 01:32:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-max-min-grouped-by-two-data-fields-and-replace-the-missing/m-p/909665#M358783</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-12-26T01:32:25Z</dc:date>
    </item>
    <item>
      <title>Re: Find max/min grouped by two data fields and replace the missing data with them</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-max-min-grouped-by-two-data-fields-and-replace-the-missing/m-p/909701#M358797</link>
      <description>&lt;P&gt;Thanks both, which all work for me.&lt;/P&gt;</description>
      <pubDate>Tue, 26 Dec 2023 19:24:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-max-min-grouped-by-two-data-fields-and-replace-the-missing/m-p/909701#M358797</guid>
      <dc:creator>fwu811</dc:creator>
      <dc:date>2023-12-26T19:24:22Z</dc:date>
    </item>
    <item>
      <title>Re: Find max/min grouped by two data fields and replace the missing data with them</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-max-min-grouped-by-two-data-fields-and-replace-the-missing/m-p/909702#M358798</link>
      <description>Yes, and the code works.</description>
      <pubDate>Tue, 26 Dec 2023 19:25:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-max-min-grouped-by-two-data-fields-and-replace-the-missing/m-p/909702#M358798</guid>
      <dc:creator>fwu811</dc:creator>
      <dc:date>2023-12-26T19:25:02Z</dc:date>
    </item>
  </channel>
</rss>

