<?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: Creating a variable from comparing consecutive values in Statistical Procedures</title>
    <link>https://communities.sas.com/t5/Statistical-Procedures/Creating-a-variable-from-comparing-consecutive-values/m-p/894567#M44320</link>
    <description>&lt;P&gt;Thank you for your response. It would level1=1, level2=2 etc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 15 Sep 2023 19:52:31 GMT</pubDate>
    <dc:creator>UcheOkoro</dc:creator>
    <dc:date>2023-09-15T19:52:31Z</dc:date>
    <item>
      <title>Creating a variable from comparing consecutive values</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Creating-a-variable-from-comparing-consecutive-values/m-p/894539#M44315</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;Please, I need help creating a variable from comparing consecutive values. Baseline is the baseline lab values, Month_01-Month-05 is for follow up visit lab values and PCHNG_01-PCHNG_05 is the percent change from baseline.&lt;/P&gt;
&lt;P style="margin: 0in; line-height: 15.75pt; background: white;"&gt;&lt;SPAN&gt;The first level of the variable is where baseline is =&amp;gt;400 and two consecutive follow-up visits are =&amp;gt;350;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="margin: 0in; line-height: 15.75pt; background: white; box-sizing: border-box; font-variant-ligatures: normal; font-variant-caps: normal; orphans: 2; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; word-spacing: 0px;"&gt;&lt;SPAN&gt;&amp;nbsp;the second level is where the baseline is =&amp;gt;300 but &amp;lt;400 and two consecutive follow-up visits are =&amp;gt;300 and were also within 25% of the baseline;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="margin: 0in; line-height: 15.75pt; background: white; box-sizing: border-box; font-variant-ligatures: normal; font-variant-caps: normal; orphans: 2; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; word-spacing: 0px;"&gt;&lt;SPAN&gt;the third level is where the baseline is &amp;lt;200 and two consecutive follow-ups were also within 25% of the baseline;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="margin: 0in; line-height: 15.75pt; background: white; box-sizing: border-box; font-variant-ligatures: normal; font-variant-caps: normal; orphans: 2; widows: 2; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; word-spacing: 0px;"&gt;&lt;SPAN&gt;the 4th level did not meet any of these criteria.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;Thank you!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID Baseline $ Month_01-Month_05 PCHNG_01-PCHNG_05;
datalines;
1 250 .   200 100 100 150 . -20 -60 -60 -40
2 400 300 350 380 300 300 -25 -12.5 -5 -25 -25
3 600 450 500 450 500 500 -33.3 -16.7 -33.3 -16.7 -16.7
4 160 150 140 150 140 130 -6.3 -12.5 -5.3 -12.5 -18.8&lt;BR /&gt;5 350 320 330 200 200 250 -8.6 -5.7 -42.9 -42.9 -28.6
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2023 20:44:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Creating-a-variable-from-comparing-consecutive-values/m-p/894539#M44315</guid>
      <dc:creator>UcheOkoro</dc:creator>
      <dc:date>2023-09-15T20:44:25Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a variable from comparing consecutive values</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Creating-a-variable-from-comparing-consecutive-values/m-p/894563#M44319</link>
      <description>Please show the expected output. What should the new variable show for each record in your data set?</description>
      <pubDate>Fri, 15 Sep 2023 19:28:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Creating-a-variable-from-comparing-consecutive-values/m-p/894563#M44319</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-09-15T19:28:17Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a variable from comparing consecutive values</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Creating-a-variable-from-comparing-consecutive-values/m-p/894567#M44320</link>
      <description>&lt;P&gt;Thank you for your response. It would level1=1, level2=2 etc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2023 19:52:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Creating-a-variable-from-comparing-consecutive-values/m-p/894567#M44320</guid>
      <dc:creator>UcheOkoro</dc:creator>
      <dc:date>2023-09-15T19:52:31Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a variable from comparing consecutive values</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Creating-a-variable-from-comparing-consecutive-values/m-p/894568#M44321</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/319831"&gt;@UcheOkoro&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;It would level1=1, level2=2 etc.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I still do not understand what you want. And by the way, this is not what &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt; asked for, which was to SHOW US the expected output and what the value of the new variable would be on each row of the data set.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2023 19:57:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Creating-a-variable-from-comparing-consecutive-values/m-p/894568#M44321</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-09-15T19:57:43Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a variable from comparing consecutive values</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Creating-a-variable-from-comparing-consecutive-values/m-p/894577#M44322</link>
      <description>&lt;P&gt;My apologies for the confusion. The following is a table of the output I expect to see.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="710"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="26"&gt;
&lt;P&gt;ID&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="52"&gt;
&lt;P&gt;Baseline&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="66"&gt;
&lt;P&gt;Month_01&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="66"&gt;
&lt;P&gt;Month_02&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="66"&gt;
&lt;P&gt;Month_03&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="60"&gt;
&lt;P&gt;Month_04&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="60"&gt;
&lt;P&gt;Month_05&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="48"&gt;
&lt;P&gt;PCHNG_01&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="60"&gt;
&lt;P&gt;PCHNG_02&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="60"&gt;
&lt;P&gt;PCHNG_03&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="46"&gt;
&lt;P&gt;PCHNG_04&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="50"&gt;
&lt;P&gt;PCHNG_05&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="50"&gt;
&lt;P&gt;New_variable&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="26"&gt;
&lt;P&gt;2&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="52"&gt;
&lt;P&gt;&lt;STRONG&gt;400&lt;/STRONG&gt;&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="66"&gt;
&lt;P&gt;300&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="66"&gt;
&lt;P&gt;&lt;STRONG&gt;350&lt;/STRONG&gt;&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="66"&gt;
&lt;P&gt;&lt;STRONG&gt;380&lt;/STRONG&gt;&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="60"&gt;
&lt;P&gt;300&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="60"&gt;
&lt;P&gt;300&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="48"&gt;
&lt;P&gt;-25&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="60"&gt;
&lt;P&gt;-12.5&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="60"&gt;
&lt;P&gt;-5&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="46"&gt;
&lt;P&gt;-25&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="50"&gt;
&lt;P&gt;-25&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="50"&gt;
&lt;P&gt;1&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="26"&gt;
&lt;P&gt;3&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="52"&gt;
&lt;P&gt;&lt;STRONG&gt;600&lt;/STRONG&gt;&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="66"&gt;
&lt;P&gt;450&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="66"&gt;
&lt;P&gt;500&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="66"&gt;
&lt;P&gt;450&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="60"&gt;
&lt;P&gt;&lt;STRONG&gt;500&lt;/STRONG&gt;&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="60"&gt;
&lt;P&gt;&lt;STRONG&gt;500&lt;/STRONG&gt;&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="48"&gt;
&lt;P&gt;-33.3&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="60"&gt;
&lt;P&gt;-16.7&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="60"&gt;
&lt;P&gt;-33.3&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="46"&gt;
&lt;P&gt;-16.7&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="50"&gt;
&lt;P&gt;-16.7&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="50"&gt;
&lt;P&gt;1&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="26"&gt;
&lt;P&gt;5&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="52"&gt;
&lt;P&gt;&lt;STRONG&gt;350&lt;/STRONG&gt;&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="66"&gt;
&lt;P&gt;&lt;STRONG&gt;320&lt;/STRONG&gt;&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="66"&gt;
&lt;P&gt;&lt;STRONG&gt;330&lt;/STRONG&gt;&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="66"&gt;
&lt;P&gt;200&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="60"&gt;
&lt;P&gt;200&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="60"&gt;
&lt;P&gt;250&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="48"&gt;
&lt;P&gt;&lt;STRONG&gt;-8.6&lt;/STRONG&gt;&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="60"&gt;
&lt;P&gt;&lt;STRONG&gt;-5.7&lt;/STRONG&gt;&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="60"&gt;
&lt;P&gt;-42.9&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="46"&gt;
&lt;P&gt;-42.9&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="50"&gt;
&lt;P&gt;-28.6&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="50"&gt;
&lt;P&gt;2&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="26"&gt;
&lt;P&gt;4&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="52"&gt;
&lt;P&gt;&lt;STRONG&gt;160&lt;/STRONG&gt;&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="66"&gt;
&lt;P&gt;150&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="66"&gt;
&lt;P&gt;140&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="66"&gt;
&lt;P&gt;150&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="60"&gt;
&lt;P&gt;140&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="60"&gt;
&lt;P&gt;130&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="48"&gt;
&lt;P&gt;-6.3&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="60"&gt;
&lt;P&gt;-12.5&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="60"&gt;
&lt;P&gt;-5.3&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="46"&gt;
&lt;P&gt;-12.5&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="50"&gt;
&lt;P&gt;-18.8&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="50"&gt;
&lt;P&gt;3&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="26"&gt;
&lt;P&gt;1&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="52"&gt;
&lt;P&gt;250&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="66"&gt;
&lt;P&gt;.&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="66"&gt;
&lt;P&gt;200&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="66"&gt;
&lt;P&gt;100&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="60"&gt;
&lt;P&gt;100&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="60"&gt;
&lt;P&gt;150&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="48"&gt;
&lt;P&gt;.&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="60"&gt;
&lt;P&gt;-20&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="60"&gt;
&lt;P&gt;-60&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="46"&gt;
&lt;P&gt;-60&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="50"&gt;
&lt;P&gt;-40&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="50"&gt;
&lt;P&gt;4&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Fri, 15 Sep 2023 20:43:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Creating-a-variable-from-comparing-consecutive-values/m-p/894577#M44322</guid>
      <dc:creator>UcheOkoro</dc:creator>
      <dc:date>2023-09-15T20:43:08Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a variable from comparing consecutive values</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Creating-a-variable-from-comparing-consecutive-values/m-p/894614#M44323</link>
      <description>&lt;P&gt;Providing sample data AND the expected result really helped to clarify things. See if below also works for your real data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID Baseline $ Month_01-Month_05 PCHNG_01-PCHNG_05 new_expected;
  datalines;
2 400 300 350 380 300 300 -25 -12.5 -5 -25 -25 1
3 600 450 500 450 500 500 -33.3 -16.7 -33.3 -16.7 -16.7 1
5 350 320 330 200 200 250 -8.6 -5.7 -42.9 -42.9 -28.6 2
4 160 150 140 150 140 130 -6.3 -12.5 -5.3 -12.5 -18.8 3
1 250 . 200 100 100 150 . -20 -60 -60 -40 4
;

data want(drop=_:);
  set have;

  array aMth {*} month_:;
  array aPct {*} pchng_:;

  /*baseline is =&amp;gt;400 and two consecutive follow-up visits are =&amp;gt;350 */
  if baseline&amp;gt;=400 then
    do;
      do _i=1 to dim(aMth)-1;
        if aMth[_i]&amp;gt;=350 and aMth[_i+1]&amp;gt;350 then 
          do;
            new_derived=1;
            leave;
          end;
      end;
    end;

  /* baseline is =&amp;gt;300 but &amp;lt;400 and two consecutive follow-up visits are =&amp;gt;300 and were also within 25% of the baseline */
  if missing(new_derived) and baseline&amp;gt;=300 and baseline&amp;lt;400 then
    do;
      do _i=1 to dim(aMth);
        if aMth[_i]&amp;gt;=300 and aMth[_i+1]&amp;gt;300 then 
          do;
            if abs(aPct[_i])&amp;lt;=25 and abs(aPct[_i+1])&amp;lt;=25 then 
              do;
                new_derived=2;
                leave;
              end;
          end;
      end;
    end;

  /* baseline is &amp;lt;200 and two consecutive follow-ups were also within 25% of the baseline */
  if missing(new_derived) and baseline&amp;lt;200 then
    do;
      do _i=1 to dim(aPct)-1;
        if 0&amp;lt;=abs(aPct[_i])&amp;lt;=25 and 0&amp;lt;=abs(aPct[_i+1])&amp;lt;=25 then 
          do;
            new_derived=3;
            leave;
          end;
      end;
    end;

  /*4th level did not meet any of these criteria */
  if missing(new_derived) then
    do;
      new_derived=4;
    end;

run;

proc print data=want;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1694854366812.png" style="width: 1105px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/88093i4A12E9EBC424D991/image-dimensions/1105x161?v=v2" width="1105" height="161" role="button" title="Patrick_0-1694854366812.png" alt="Patrick_0-1694854366812.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As a variation to above code here a version that uses a GOTO. I consider this one of the rather rare cases where using GOTO is still legitimate because it avoids execution of logical steps once a case has been identified.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop=_:);
  set have;

  array aMth {*} month_:;
  array aPct {*} pchng_:;

  /*baseline is =&amp;gt;400 and two consecutive follow-up visits are =&amp;gt;350 */
  if baseline&amp;gt;=400 then
    do;
      do _i=1 to dim(aMth)-1;
        if aMth[_i]&amp;gt;=350 and aMth[_i+1]&amp;gt;350 then 
          do;
            new_derived=1;
            /* case identified, do not check further */
            goto done;
          end;
      end;
    end;

  /* baseline is =&amp;gt;300 but &amp;lt;400 and two consecutive follow-up visits are =&amp;gt;300 and were also within 25% of the baseline */
  if baseline&amp;gt;=300 and baseline&amp;lt;400 then
    do;
      do _i=1 to dim(aMth);
        if aMth[_i]&amp;gt;=300 and aMth[_i+1]&amp;gt;300 then 
          do;
            if abs(aPct[_i])&amp;lt;=25 and abs(aPct[_i+1])&amp;lt;=25 then 
              do;
                new_derived=2;
                /* case identified, do not check further */
                goto done;
              end;
          end;
      end;
    end;

  /* baseline is &amp;lt;200 and two consecutive follow-ups were also within 25% of the baseline */
  if baseline&amp;lt;200 then
    do;
      do _i=1 to dim(aPct)-1;
        if 0&amp;lt;=abs(aPct[_i])&amp;lt;=25 and 0&amp;lt;=abs(aPct[_i+1])&amp;lt;=25 then 
          do;
            new_derived=3;
            /* case identified, do not check further */
            goto done;
          end;
      end;
    end;

  /*4th level did not meet any of these criteria */
  if missing(new_derived) then
    do;
      new_derived=4;
    end;

  /* target of goto statement */
  done:;

run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 16 Sep 2023 09:20:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Creating-a-variable-from-comparing-consecutive-values/m-p/894614#M44323</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-09-16T09:20:09Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a variable from comparing consecutive values</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Creating-a-variable-from-comparing-consecutive-values/m-p/894619#M44324</link>
      <description>&lt;P&gt;This need to compare successive values in arrays is a good situation to use the UNTIL condition.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit: Let me restate that:&amp;nbsp; the need to stop a loop once a condition has been met is ready-made for the UNTIL clause.&amp;nbsp; And if the until condition is related to the new variable in this case, you can avoid a good deal of extra code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID $ Baseline  Month_01-Month_05 PCHNG_01-PCHNG_05 new_expected;
  datalines;
2 400 300 350 380 300 300 -25    -12.5   -5   -25   -25    1
3 600 450 500 450 500 500 -33.3  -16.7  -33.3 -16.7 -16.7  1
5 350 320 330 200 200 250  -8.6   -5.7  -42.9 -42.9 -28.6  2
4 160 150 140 150 140 130  -6.3  -12.5   -5.3 -12.5 -18.8  3
1 250 .   200 100 100 150    .   -20    -60   -60   -40    4
;

data want;
  set have;
  array f {*} month_01-month_05 ;
  array p {*} pchng_01-pchng_05 ;

  new=4;
  if baseline &amp;gt;= 400    then do i=2 to dim(f) until (new=1);
    if f{i}&amp;gt;=350 and f{i-1}&amp;gt;=350 then new=1;
  end;
  else if baseline&amp;gt;=300 then do i=2 to dim(f) until (new=2);
    if n(f{i},f{i-1})=2 and  min(f{i},f{i-1})&amp;gt;=300 and   max(abs(p{i}),abs(p{i-1})) &amp;lt;=25 then new=2;
  end;
  else if baseline&amp;lt;200 then do i=2 to dim(f) until (new=3);
    if n(p{i},p{i-1})=2 and max(abs(p{i}),abs(p{i-1}))&amp;lt;=25 then new=3;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 16 Sep 2023 12:38:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Creating-a-variable-from-comparing-consecutive-values/m-p/894619#M44324</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-09-16T12:38:25Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a variable from comparing consecutive values</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Creating-a-variable-from-comparing-consecutive-values/m-p/894652#M44325</link>
      <description>&lt;P&gt;Thank you so much, Patrick for your help!&lt;/P&gt;</description>
      <pubDate>Sun, 17 Sep 2023 03:29:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Creating-a-variable-from-comparing-consecutive-values/m-p/894652#M44325</guid>
      <dc:creator>UcheOkoro</dc:creator>
      <dc:date>2023-09-17T03:29:45Z</dc:date>
    </item>
  </channel>
</rss>

