<?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: calculate the change from minimum of the previous visit values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/calculate-the-change-from-minimum-of-the-previous-visit-values/m-p/494356#M130251</link>
    <description>&lt;P&gt;Thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt; for your response and the code gave the expected result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I agree with you to have a numeric variable for sorting purpose.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards dlm='09'x;
input Subjid : Visit$	Value;
visitnum=input(compress(visit,,'kd'),best.);
cards;
6	Visit1	75
6	Visit2	82
6	Visit3	14
6	Visit4	73
6	Visit5	67
6	Visit6	61
6	Visit7	38
;

proc sort data=have out=have2(keep=subjid visit visitnum value);
by subjid visitnum ;
run;

data want;
do until(last.subjid);
  set have2;
  by subjid visitnum;
  if nmiss(value,base)=0 then pchg=((value-base)/base)*100;
  else pchg=.;
  output;
  if value&amp;gt;.z then base=min(base, value);
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 11 Sep 2018 02:00:05 GMT</pubDate>
    <dc:creator>Jagadishkatam</dc:creator>
    <dc:date>2018-09-11T02:00:05Z</dc:date>
    <item>
      <title>calculate the change from minimum of the previous visit values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-the-change-from-minimum-of-the-previous-visit-values/m-p/494023#M130107</link>
      <description>&lt;P&gt;Hi experts,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is with regard to a programming query to calculate the change from minimum of the previous visit values.&lt;/P&gt;
&lt;P&gt;The calculation would check each visit against all prior visits for smallest value and calculate a percentage of change. An example would be something like visit 3 checks visit 1 and 2 and get the minimum of visit 1 and visit 2 to derive the change. I tried the below code in datastep and i am getting the expected output.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Would like to check if there are any better approaches in datastep or proc sql&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Subjid Visit	Value
6	Visit1	75
6	Visit2	82
6	Visit3	14
6	Visit4	73
6	Visit5	67
6	Visit6	61
6	Visit7	38


proc sort data=have out=have2(keep=subjid visit value);
by subjid visit ;
run;

data want;
length new new2 $100;
set have2;
by subjid visit;
retain new;
if first.subjid then new='';
if value ne . then new=catx(',',new,value);
new2=lag(new);
if first.subjid then new2='';
array cnt(15) x1-x15;
do i=1 to 15;
cnt(i)=input(scan(new2,i,','),best.);
end;
base=min(of x1-x15);
if nmiss(value,base)=0 then pchg=((value-base)/base)*100;
drop new new2 x: i;
run;


Subjid Visit Value BASE PCHG
6	Visit1	75	.	.
6	Visit2	82	75	7
6	Visit3	14	75	-61
6	Visit4	73	14	59
6	Visit5	67	73	-6
6	Visit6	61	67	-6
6	Visit7	38	61	-23



&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Sep 2018 07:25:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-the-change-from-minimum-of-the-previous-visit-values/m-p/494023#M130107</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2018-09-10T07:25:57Z</dc:date>
    </item>
    <item>
      <title>Re: calculate the change from minimum of the previous visit values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-the-change-from-minimum-of-the-previous-visit-values/m-p/494042#M130113</link>
      <description>&lt;P&gt;Hi Jag,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try this (using your sorted dataset HAVE2):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
do until(last.subjid);
  set have2;
  by subjid visit;
  if nmiss(value,base)=0 then pchg=((value-base)/base)*100;
  else pchg=.;
  output;
  if value&amp;gt;.z then base=min(base, value);
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Actually, I would prefer a &lt;EM&gt;numeric&lt;/EM&gt;&amp;nbsp;variable containing the visit number so as to avoid incorrect sort orders like 'visit10'&amp;lt;'visit2'.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit: Assuming that all non-missing VALUEs are positive.&lt;/P&gt;
&lt;P&gt;Edit 2: Inserted "&lt;FONT face="courier new,courier"&gt;if value&amp;gt;.z then&lt;/FONT&gt; " just to avoid a note in the log about "...&amp;nbsp;performing an operation on missing values" in cases where the first VALUE in a BY group is missing.&lt;/P&gt;</description>
      <pubDate>Mon, 10 Sep 2018 09:39:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-the-change-from-minimum-of-the-previous-visit-values/m-p/494042#M130113</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2018-09-10T09:39:44Z</dc:date>
    </item>
    <item>
      <title>Re: calculate the change from minimum of the previous visit values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-the-change-from-minimum-of-the-previous-visit-values/m-p/494119#M130156</link>
      <description>&lt;P&gt;Why Visit 5 6 7 is not 14?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards expandtabs truncover;
input Subjid Visit $ Value;
cards;
6	Visit1	75
6	Visit2	82
6	Visit3	14
6	Visit4	73
6	Visit5	67
6	Visit6	61
6	Visit7	38
;
run;
data want;
  set have;
  by subjid;
array x{999} _temporary_;
if first.subjid then do;n=0; call missing(of x{*});	end;
base=min(of x{*});
n+1;x{n}=value;
drop n;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 10 Sep 2018 13:30:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-the-change-from-minimum-of-the-previous-visit-values/m-p/494119#M130156</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-09-10T13:30:10Z</dc:date>
    </item>
    <item>
      <title>Re: calculate the change from minimum of the previous visit values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-the-change-from-minimum-of-the-previous-visit-values/m-p/494356#M130251</link>
      <description>&lt;P&gt;Thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt; for your response and the code gave the expected result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I agree with you to have a numeric variable for sorting purpose.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards dlm='09'x;
input Subjid : Visit$	Value;
visitnum=input(compress(visit,,'kd'),best.);
cards;
6	Visit1	75
6	Visit2	82
6	Visit3	14
6	Visit4	73
6	Visit5	67
6	Visit6	61
6	Visit7	38
;

proc sort data=have out=have2(keep=subjid visit visitnum value);
by subjid visitnum ;
run;

data want;
do until(last.subjid);
  set have2;
  by subjid visitnum;
  if nmiss(value,base)=0 then pchg=((value-base)/base)*100;
  else pchg=.;
  output;
  if value&amp;gt;.z then base=min(base, value);
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 11 Sep 2018 02:00:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-the-change-from-minimum-of-the-previous-visit-values/m-p/494356#M130251</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2018-09-11T02:00:05Z</dc:date>
    </item>
    <item>
      <title>Re: calculate the change from minimum of the previous visit values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-the-change-from-minimum-of-the-previous-visit-values/m-p/494358#M130252</link>
      <description>&lt;P&gt;Thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;, My mistake it should be 14 on visit 5,6,7&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You solution also worked.&lt;/P&gt;</description>
      <pubDate>Tue, 11 Sep 2018 02:01:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-the-change-from-minimum-of-the-previous-visit-values/m-p/494358#M130252</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2018-09-11T02:01:45Z</dc:date>
    </item>
  </channel>
</rss>

