<?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 SQL max value with corresponding visit in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-max-value-with-corresponding-visit/m-p/622598#M183159</link>
    <description>&lt;P&gt;Using SQL only something "ugly" as below should do the job.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have(drop=_:);
  length row_id 8;
  array var {3} 8.;
  array time_pt {3} 8.;
  format time_pt: date9.;

  do group_id=1 to 3;
    do _rows=1 to 100;
      do _i=1 to dim(var);
        var[_i]=ceil(ranuni(1)*50);
        time_pt[_i]=today()-ceil(ranuni(1)*400);
      end;
      row_id+1;
      output;
    end;
  end;

  stop;
run;

proc sql;
  create table want as
  select 
    group_id,
    max_var1,
    (select max(time_pt1) from have s where s.group_id=i.group_id and s.var1=i.max_var1) as
      max_time_pt1 format=date9.,
    max_var2,
    (select max(time_pt2) from have s where s.group_id=i.group_id and s.var2=i.max_var2) as
      max_time_pt2 format=date9.,
    max_var3,
    (select max(time_pt3) from have s where s.group_id=i.group_id and s.var3=i.max_var3) as
      max_time_pt3 format=date9.
  from
    (
      select 
        group_id,
        max(var1) as max_var1,
        max(var2) as max_var2,
        max(var3) as max_var3
      from have
      group by group_id
    ) i
  ;
quit;

/* report showing which row got selected */
proc sql;
  select 
    h.group_id,
    h.var1,
    h.time_pt1,
    w.max_time_pt1,
    h.time_pt1=w.max_time_pt1 as sel_ind
    from 
      have h inner join want w
      on h.group_id=w.group_id and h.var1=w.max_var1
    ;
quit;
      &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you're dealing with high data volumes then using a different approach will likely perform better.&lt;/P&gt;</description>
    <pubDate>Thu, 06 Feb 2020 02:20:22 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2020-02-06T02:20:22Z</dc:date>
    <item>
      <title>PROC SQL max value with corresponding visit</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-max-value-with-corresponding-visit/m-p/622584#M183152</link>
      <description>&lt;P&gt;Hello all,&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;I have calculated the max value of three variables separately. However, I need the time point associated with the max of these three. How do I calculate that?&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;For ex: if a subject has 8 values at different time points, I need the max value and it's associate time points for 3 different variables.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table peak as 
select subject, max(cd1922) as peakcd1922, max(cd19) as peakcd19, max(cd22) as peakcd22,
sum(calculated peakcd1922,calculated peakcd19,calculated peakcd22) as peakCarT
from engraft
group by subject;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 06 Feb 2020 00:12:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-max-value-with-corresponding-visit/m-p/622584#M183152</guid>
      <dc:creator>saslove</dc:creator>
      <dc:date>2020-02-06T00:12:04Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL max value with corresponding visit</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-max-value-with-corresponding-visit/m-p/622598#M183159</link>
      <description>&lt;P&gt;Using SQL only something "ugly" as below should do the job.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have(drop=_:);
  length row_id 8;
  array var {3} 8.;
  array time_pt {3} 8.;
  format time_pt: date9.;

  do group_id=1 to 3;
    do _rows=1 to 100;
      do _i=1 to dim(var);
        var[_i]=ceil(ranuni(1)*50);
        time_pt[_i]=today()-ceil(ranuni(1)*400);
      end;
      row_id+1;
      output;
    end;
  end;

  stop;
run;

proc sql;
  create table want as
  select 
    group_id,
    max_var1,
    (select max(time_pt1) from have s where s.group_id=i.group_id and s.var1=i.max_var1) as
      max_time_pt1 format=date9.,
    max_var2,
    (select max(time_pt2) from have s where s.group_id=i.group_id and s.var2=i.max_var2) as
      max_time_pt2 format=date9.,
    max_var3,
    (select max(time_pt3) from have s where s.group_id=i.group_id and s.var3=i.max_var3) as
      max_time_pt3 format=date9.
  from
    (
      select 
        group_id,
        max(var1) as max_var1,
        max(var2) as max_var2,
        max(var3) as max_var3
      from have
      group by group_id
    ) i
  ;
quit;

/* report showing which row got selected */
proc sql;
  select 
    h.group_id,
    h.var1,
    h.time_pt1,
    w.max_time_pt1,
    h.time_pt1=w.max_time_pt1 as sel_ind
    from 
      have h inner join want w
      on h.group_id=w.group_id and h.var1=w.max_var1
    ;
quit;
      &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you're dealing with high data volumes then using a different approach will likely perform better.&lt;/P&gt;</description>
      <pubDate>Thu, 06 Feb 2020 02:20:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-max-value-with-corresponding-visit/m-p/622598#M183159</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-02-06T02:20:22Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL max value with corresponding visit</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-max-value-with-corresponding-visit/m-p/622626#M183167</link>
      <description>&lt;P&gt;With a data step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have (drop=_:);
length
  subject 8
  time_pt 4
;
format time_pt yymmddd10.;
array var {3} 8.;
do subject = 1 to 3;
  do _rows = 1 to 100;
    time_pt = today()-ceil(ranuni(1)*400);
    do _i = 1 to dim(var);
      var[_i]=ceil(ranuni(1)*50);
    end;
    output;
  end;
end;
run;

data want;
set have;
by subject;
retain peak1-peak3 date1-date3;
if first.subject
then call missing(of peak:,of date:);
array peaks {3} peak:;
array dates {3} date:;
array vars {3} var:;
format date: yymmddd10.;
do _i = 1 to 3;
  if vars{_i} &amp;gt; peaks{_i}
  then do;
    peaks{_i} = vars{_i};
    dates{_i} = time_pt;
  end;
end;
if last.subject;
drop time_pt var: _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 06 Feb 2020 08:49:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-max-value-with-corresponding-visit/m-p/622626#M183167</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-02-06T08:49:54Z</dc:date>
    </item>
  </channel>
</rss>

