<?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 find the minimum value compared to previous observation for same subject in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-minimum-value-compared-to-previous-observation/m-p/595379#M171261</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input SUBJ	SEQ	VALUE; *	MINIM	  This is how MIN value is calculated;
cards;
1004	1	35.58	-	 - There is no obs previous and should appear blank
1004	2	32.69	35.58	 - Only one obs available prior so 35.58 is min
1004	3	26.15	32.69	 - Compared to obs 1, 2 value of 32.69 is min
1004	4	21.77	26.15	 - Compared to obs 1, 2 and 3 value of 26.15 is min
1004	5	19.47	21.77	 - Compared to 1, 2,3 and 4 value of 21.77 is min
1004	6	19.52	19.47	 - Compared to 1, 2,3,4 and 5 value of 19.47 is min
1004	7	13.71	19.47	 - Compared to 1, 2,3,4,5 and 6 for this subject value of 19.47 is min
1004	8	13.7	13.71	
1004	9	13.97	13.7	
1004	10	24.28   13.7
1005	1	22.57	-	 - There is no previous obs for this subject and should appear blank
1005	2	10.21	22.57	 - Only one row available previous so 22.57 is min
1005	3	10.35	10.21	 - Compared to 1, 2 value of 10.21 is min
1005	4	10.35	10.21	 - Compared to 1, 2 and 3 value of 10.21 is min
1005	5	10.35	10.21	 - Compared to 1, 2,3 and 4 for this subject value of 21.77 is minimum
1005	6	9.76	10.21
;

data want;
 do _n_=1 by 1 until(last.subj);
  set have;
  by subj;
  array t(99999) _temporary_;
  if _n_&amp;gt;1 then min=min(of t(*));
  t(_n_)=value;
  output;
 end;
 call missing(of t(*));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 10 Oct 2019 13:24:22 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2019-10-10T13:24:22Z</dc:date>
    <item>
      <title>How to find the minimum value compared to previous observation for same subject</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-minimum-value-compared-to-previous-observation/m-p/595373#M171257</link>
      <description>&lt;P&gt;Below is my data in column SUBJ, B and VALUE. I need MINIM column with the min(VALUE) compared to previous observations for same subject. Can Any one help?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;SUBJ	SEQ	VALUE	MINIM	  This is how MIN value is calculated
1004	1	35.58	-	 - There is no obs previous and should appear blank
1004	2	32.69	35.58	 - Only one obs available prior so 35.58 is min
1004	3	26.15	32.69	 - Compared to obs 1, 2 value of 32.69 is min
1004	4	21.77	26.15	 - Compared to obs 1, 2 and 3 value of 26.15 is min
1004	5	19.47	21.77	 - Compared to 1, 2,3 and 4 value of 21.77 is min
1004	6	19.52	19.47	 - Compared to 1, 2,3,4 and 5 value of 19.47 is min
1004	7	13.71	19.47	 - Compared to 1, 2,3,4,5 and 6 for this subject value of 19.47 is min
1004	8	13.7	13.71	
1004	9	13.97	13.7	
1004	10	24.28   13.7
1005	1	22.57	-	 - There is no previous obs for this subject and should appear blank
1005	2	10.21	22.57	 - Only one row available previous so 22.57 is min
1005	3	10.35	10.21	 - Compared to 1, 2 value of 10.21 is min
1005	4	10.35	10.21	 - Compared to 1, 2 and 3 value of 10.21 is min
1005	5	10.35	10.21	 - Compared to 1, 2,3 and 4 for this subject value of 21.77 is minimum
1005	6	9.76	10.21&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 10 Oct 2019 13:18:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-minimum-value-compared-to-previous-observation/m-p/595373#M171257</guid>
      <dc:creator>jksthomas</dc:creator>
      <dc:date>2019-10-10T13:18:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the minimum value compared to previous observation for same subject</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-minimum-value-compared-to-previous-observation/m-p/595379#M171261</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input SUBJ	SEQ	VALUE; *	MINIM	  This is how MIN value is calculated;
cards;
1004	1	35.58	-	 - There is no obs previous and should appear blank
1004	2	32.69	35.58	 - Only one obs available prior so 35.58 is min
1004	3	26.15	32.69	 - Compared to obs 1, 2 value of 32.69 is min
1004	4	21.77	26.15	 - Compared to obs 1, 2 and 3 value of 26.15 is min
1004	5	19.47	21.77	 - Compared to 1, 2,3 and 4 value of 21.77 is min
1004	6	19.52	19.47	 - Compared to 1, 2,3,4 and 5 value of 19.47 is min
1004	7	13.71	19.47	 - Compared to 1, 2,3,4,5 and 6 for this subject value of 19.47 is min
1004	8	13.7	13.71	
1004	9	13.97	13.7	
1004	10	24.28   13.7
1005	1	22.57	-	 - There is no previous obs for this subject and should appear blank
1005	2	10.21	22.57	 - Only one row available previous so 22.57 is min
1005	3	10.35	10.21	 - Compared to 1, 2 value of 10.21 is min
1005	4	10.35	10.21	 - Compared to 1, 2 and 3 value of 10.21 is min
1005	5	10.35	10.21	 - Compared to 1, 2,3 and 4 for this subject value of 21.77 is minimum
1005	6	9.76	10.21
;

data want;
 do _n_=1 by 1 until(last.subj);
  set have;
  by subj;
  array t(99999) _temporary_;
  if _n_&amp;gt;1 then min=min(of t(*));
  t(_n_)=value;
  output;
 end;
 call missing(of t(*));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 10 Oct 2019 13:24:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-minimum-value-compared-to-previous-observation/m-p/595379#M171261</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-10-10T13:24:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the minimum value compared to previous observation for same subject</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-minimum-value-compared-to-previous-observation/m-p/595385#M171264</link>
      <description>&lt;P&gt;And if you want a SQL solution&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input SUBJ	SEQ	VALUE; *	MINIM	  This is how MIN value is calculated;
cards;
1004	1	35.58	-	 - There is no obs previous and should appear blank
1004	2	32.69	35.58	 - Only one obs available prior so 35.58 is min
1004	3	26.15	32.69	 - Compared to obs 1, 2 value of 32.69 is min
1004	4	21.77	26.15	 - Compared to obs 1, 2 and 3 value of 26.15 is min
1004	5	19.47	21.77	 - Compared to 1, 2,3 and 4 value of 21.77 is min
1004	6	19.52	19.47	 - Compared to 1, 2,3,4 and 5 value of 19.47 is min
1004	7	13.71	19.47	 - Compared to 1, 2,3,4,5 and 6 for this subject value of 19.47 is min
1004	8	13.7	13.71	
1004	9	13.97	13.7	
1004	10	24.28   13.7
1005	1	22.57	-	 - There is no previous obs for this subject and should appear blank
1005	2	10.21	22.57	 - Only one row available previous so 22.57 is min
1005	3	10.35	10.21	 - Compared to 1, 2 value of 10.21 is min
1005	4	10.35	10.21	 - Compared to 1, 2 and 3 value of 10.21 is min
1005	5	10.35	10.21	 - Compared to 1, 2,3 and 4 for this subject value of 21.77 is minimum
1005	6	9.76	10.21
;

proc sql;
create table want as
select a.subj,a.seq,a.value,min(b.value) as min
from have a left join have b
on a.subj=b.subj
and b.seq&amp;lt;a.seq
group by a.subj,a.seq,a.value
order by a.subj,a.seq;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 10 Oct 2019 13:53:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-minimum-value-compared-to-previous-observation/m-p/595385#M171264</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-10-10T13:53:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the minimum value compared to previous observation for same subject</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-minimum-value-compared-to-previous-observation/m-p/595404#M171271</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/294624"&gt;@jksthomas&lt;/a&gt;&amp;nbsp;and welcome to the SAS Support Communities!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's another DATA step solution:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
do until(last.subj);
  set have;
  by subj seq;
  output;
  minim=min(minim,value);
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 10 Oct 2019 14:01:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-minimum-value-compared-to-previous-observation/m-p/595404#M171271</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-10-10T14:01:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the minimum value compared to previous observation for same subject</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-minimum-value-compared-to-previous-observation/m-p/595406#M171272</link>
      <description>&lt;P&gt;Indeed more efficient and tactfully observed(the values), makes me realise my lethargy. Need to change my coffee brand big time. Kudos!&lt;/P&gt;</description>
      <pubDate>Thu, 10 Oct 2019 14:07:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-minimum-value-compared-to-previous-observation/m-p/595406#M171272</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-10-10T14:07:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the minimum value compared to previous observation for same subject</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-minimum-value-compared-to-previous-observation/m-p/595630#M171396</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;P&gt;Indeed more efficient and tactfully observed(the values), makes me realise my lethargy. Need to change my coffee brand big time. Kudos!&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;Both of your prog worked well. Thanks a lot.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Oct 2019 04:57:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-minimum-value-compared-to-previous-observation/m-p/595630#M171396</guid>
      <dc:creator>jksthomas</dc:creator>
      <dc:date>2019-10-11T04:57:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the minimum value compared to previous observation for same subject</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-minimum-value-compared-to-previous-observation/m-p/595635#M171398</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/294624"&gt;@jksthomas&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;P&gt;Indeed more efficient and tactfully observed(the values), makes me realise my lethargy. Need to change my coffee brand big time. Kudos!&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;Both of your prog worked well. Thanks a lot.&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;One more question: Can I get one more additonal row to display which seq from the subj is taken as minim?&lt;/P&gt;</description>
      <pubDate>Fri, 11 Oct 2019 05:44:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-minimum-value-compared-to-previous-observation/m-p/595635#M171398</guid>
      <dc:creator>jksthomas</dc:creator>
      <dc:date>2019-10-11T05:44:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the minimum value compared to previous observation for same subject</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-minimum-value-compared-to-previous-observation/m-p/595655#M171406</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/294624"&gt;@jksthomas&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;One more question: Can I get one more additonal row to display which seq from the subj is taken as minim?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Sure (assuming you mean: additional &lt;EM&gt;column&lt;/EM&gt;, i.e. &lt;EM&gt;variable&lt;/EM&gt;&lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
do until(last.subj);
  set have;
  by subj seq;
  output;
  if value&amp;lt;minim | first.subj then do;
    minim=value;
    seq_minim=seq;
  end;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In the case of tied minimum values, e.g., if in your sample data the observation with SUBJ=1005, SEQ=4 had VALUE=10.21 (instead of 10.35), the above code would store the &lt;EM&gt;first&lt;/EM&gt; SEQ with a VALUE of 10.21 in SEQ_MINIM, i.e., SEQ_MINIM=2 for the last four observations. By replacing the "&amp;lt;" sign in the IF condition with "&amp;lt;=" you could obtain the &lt;EM&gt;last&lt;/EM&gt; SEQ instead, i.e., SEQ_MINIM=4 in the last &lt;EM&gt;two&lt;/EM&gt; observations (and, of course, still SEQ_MINIM=2 in the two preceding observations).&lt;/P&gt;</description>
      <pubDate>Fri, 11 Oct 2019 07:20:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-minimum-value-compared-to-previous-observation/m-p/595655#M171406</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-10-11T07:20:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the minimum value compared to previous observation for same subject</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-minimum-value-compared-to-previous-observation/m-p/595665#M171411</link>
      <description>&lt;P&gt;hi,&lt;/P&gt;&lt;P&gt;Thanks again for you help&lt;/P&gt;&lt;P&gt;Sorry i mean to say new variable.&lt;/P&gt;&lt;P&gt;Prog works fine from 3rd row, however, for the second row SEQ_MINIM is blank. Is there a way to display seq = 1 .&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;SUBJ	VALUE	SEQ	MINIM	SEQ_MINIM
1001003	22.57	1		
1001003	10.21	2	22.57	
1001003	10.35	3	10.21	2
1001003	10.35	4	10.21	2
1001003	10.35	5	10.21	2
1001003	9.76	6	10.21	2&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;</description>
      <pubDate>Fri, 11 Oct 2019 08:14:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-minimum-value-compared-to-previous-observation/m-p/595665#M171411</guid>
      <dc:creator>jksthomas</dc:creator>
      <dc:date>2019-10-11T08:14:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the minimum value compared to previous observation for same subject</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-minimum-value-compared-to-previous-observation/m-p/595669#M171413</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/294624"&gt;@jksthomas&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Prog works fine from 3rd row, however, for the second row SEQ_MINIM is blank. Is there a way to display seq = 1 .&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This does &lt;EM&gt;not&lt;/EM&gt; happen if the input dataset (HAVE) does not already contain the derived variables (MINIM, SEQ_MINIM). So, make sure that you don't run the step, e.g., on the result of a previous run (like &lt;FONT face="courier new,courier"&gt;data have; set have;&lt;/FONT&gt; ...). If dataset HAVE for some reason contains variables MINIM or SEQ_MINIM, just drop them via a dataset option:&lt;/P&gt;
&lt;PRE&gt;set have&lt;STRONG&gt;(drop=minim seq_minim)&lt;/STRONG&gt;;&lt;/PRE&gt;</description>
      <pubDate>Fri, 11 Oct 2019 08:44:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-minimum-value-compared-to-previous-observation/m-p/595669#M171413</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-10-11T08:44:05Z</dc:date>
    </item>
  </channel>
</rss>

