<?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: Do until lag&amp;amp;i(var) gt 0 in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Do-until-lag-amp-i-var-gt-0/m-p/349715#M81181</link>
    <description>&lt;P&gt;The lag() function is a data step function that works at data step runtime.&lt;/P&gt;
&lt;P&gt;The macro %do %until, OTOH, is dealt with when the code for the data step is fetched (before compiling and &lt;U&gt;long&lt;/U&gt; before runtime).&lt;/P&gt;
&lt;P&gt;Therefore your construct makes no sense.&lt;/P&gt;
&lt;P&gt;Solve your problem by retaining period in the data step; set it to zero at every negative change and increment it otherwise.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input date :ddmmyy10. change :percent5. ;
format date ddmmyy10. change percent5.;
cards;
13/04/2017 1%
12/04/2017 2%
11/04/2017 1%
10/04/2017 -1%
;
run;

proc sort data=have;
by date;
run;

data want;
set have;
retain period;
if change &amp;lt; 0
then period = 0;
else period + 1;
run;

proc print data=want noobs;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;      date    change    period

10/04/2017    ( 1%)        0  
11/04/2017      1%         1  
12/04/2017      2%         2  
13/04/2017      1%         3  
&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 13 Apr 2017 11:13:54 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2017-04-13T11:13:54Z</dc:date>
    <item>
      <title>Do until lag&amp;i(var) gt 0</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-until-lag-amp-i-var-gt-0/m-p/349704#M81173</link>
      <description>&lt;P&gt;I'm trying to determine how many periods before the current period before the price change was negative.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm tried this code but it doesn't work:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro temp;&lt;/P&gt;&lt;P&gt;period = 0;&lt;BR /&gt;%let i = 1;&lt;BR /&gt;%do %until(lag&amp;amp;i.(price_chg_1) &amp;gt;0);&lt;/P&gt;&lt;P&gt;period = period + &amp;amp;i;&lt;/P&gt;&lt;P&gt;&amp;amp;i = &amp;amp;i+1;&lt;/P&gt;&lt;P&gt;%end;&lt;/P&gt;&lt;P&gt;%mend;&lt;/P&gt;&lt;P&gt;%temp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the error messahe I'm getting;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Required operator not found in expression: lag&amp;amp;i.(vol_chg_1) &amp;gt;0&lt;BR /&gt;ERROR: The condition in the %DO %UNTIL loop, lag&amp;amp;i.(vol_chg_1) &amp;gt;0,&lt;BR /&gt;yielded an invalid or missing value, . The macro will stop&lt;BR /&gt;executing.&lt;BR /&gt;ERROR: The macro TEMP will stop executing.&lt;BR /&gt;ERROR 180-322: Statement is not valid or it is used out of proper&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to produce the Period field below&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Date&lt;/TD&gt;&lt;TD&gt;Price change&lt;/TD&gt;&lt;TD&gt;Period&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;13/04/2017&lt;/TD&gt;&lt;TD&gt;1%&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;12/04/2017&lt;/TD&gt;&lt;TD&gt;2%&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;11/04/2017&lt;/TD&gt;&lt;TD&gt;1%&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;10/04/2017&lt;/TD&gt;&lt;TD&gt;-1%&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;BR /&gt;order.&lt;/P&gt;</description>
      <pubDate>Thu, 13 Apr 2017 10:28:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-until-lag-amp-i-var-gt-0/m-p/349704#M81173</guid>
      <dc:creator>brophymj</dc:creator>
      <dc:date>2017-04-13T10:28:03Z</dc:date>
    </item>
    <item>
      <title>Re: Do until lag&amp;i(var) gt 0</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-until-lag-amp-i-var-gt-0/m-p/349705#M81174</link>
      <description>&lt;P&gt;?? Am sure your just trying to make things more complicated for yourself:&lt;/P&gt;
&lt;PRE&gt;proc sort data=have;
  by date;
run;
data want;
  set have;
  retain flg;
  if _n_=1 then flg=0;
  if flg=1 then do;
    output;
    flg=2;
  end;
  if price_change &amp;lt; 0 then flg=1;
run;&lt;/PRE&gt;
&lt;P&gt;I.e. set flg to 1 when neg is found, then on the next obs its one so output, then set to not 1 so you only get one row output. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, please&amp;nbsp;&lt;STRONG&gt;post test data in the form of a datastep!&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Apr 2017 10:33:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-until-lag-amp-i-var-gt-0/m-p/349705#M81174</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-04-13T10:33:54Z</dc:date>
    </item>
    <item>
      <title>Re: Do until lag&amp;i(var) gt 0</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-until-lag-amp-i-var-gt-0/m-p/349710#M81178</link>
      <description>&lt;P&gt;Hi RW9&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm not trying to make things complicated. I'm just doing the best I can.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried the code below&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;input date price_change;&lt;BR /&gt;datalines;&lt;BR /&gt;3 1&lt;BR /&gt;2 1&lt;BR /&gt;1 1&lt;BR /&gt;0 -1&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;proc sort data=have;&lt;BR /&gt;by date;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;retain flg;&lt;BR /&gt;if _n_=1 then flg=0;&lt;BR /&gt;if flg=1 then do;&lt;BR /&gt;output;&lt;BR /&gt;flg=2;&lt;BR /&gt;end;&lt;BR /&gt;if price_change &amp;lt; 0 then flg=1;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;... but it's not working. Heres a larger dataset that might make it a bit clearer (i'm just using number for the date for simplicity):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;data have;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;input date price_change;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;datalines;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;20 1&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;19 1&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;18 1&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;17 -1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;16 1&lt;/P&gt;&lt;P&gt;15 1&lt;/P&gt;&lt;P&gt;14 -1&lt;/P&gt;&lt;P&gt;13 -1&lt;/P&gt;&lt;P&gt;12 1&lt;/P&gt;&lt;P&gt;11 &amp;nbsp;1&lt;/P&gt;&lt;P&gt;10 1&lt;/P&gt;&lt;P&gt;9 1&lt;/P&gt;&lt;P&gt;8 1&lt;/P&gt;&lt;P&gt;7 1&lt;/P&gt;&lt;P&gt;6 1&lt;/P&gt;&lt;P&gt;5 1&lt;/P&gt;&lt;P&gt;4 -1&lt;/P&gt;&lt;P&gt;3 -1&lt;/P&gt;&lt;P&gt;2 1&lt;/P&gt;&lt;P&gt;1 1;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want the final dataset to look like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Note: period field is the number of days since the last negative price change. I will also have a field for positive price change&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;data have;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;input date price_change period;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;datalines;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;20 1 3&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;19 1 2&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;18 1 1&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;17 -1 0&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;16 1 2&lt;/P&gt;&lt;P&gt;15 1 1&lt;/P&gt;&lt;P&gt;14 -1 0&lt;/P&gt;&lt;P&gt;13 -1 0&lt;/P&gt;&lt;P&gt;12 1 8&lt;/P&gt;&lt;P&gt;11 &amp;nbsp;1 7&lt;/P&gt;&lt;P&gt;10 1 6&lt;/P&gt;&lt;P&gt;9 1 5&lt;/P&gt;&lt;P&gt;8 1 4&lt;/P&gt;&lt;P&gt;7 1 3&lt;/P&gt;&lt;P&gt;6 1 2&lt;/P&gt;&lt;P&gt;5 1 1&lt;/P&gt;&lt;P&gt;4 -1 0&lt;/P&gt;&lt;P&gt;3 -1 0&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Apr 2017 11:03:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-until-lag-amp-i-var-gt-0/m-p/349710#M81178</guid>
      <dc:creator>brophymj</dc:creator>
      <dc:date>2017-04-13T11:03:26Z</dc:date>
    </item>
    <item>
      <title>Re: Do until lag&amp;i(var) gt 0</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-until-lag-amp-i-var-gt-0/m-p/349715#M81181</link>
      <description>&lt;P&gt;The lag() function is a data step function that works at data step runtime.&lt;/P&gt;
&lt;P&gt;The macro %do %until, OTOH, is dealt with when the code for the data step is fetched (before compiling and &lt;U&gt;long&lt;/U&gt; before runtime).&lt;/P&gt;
&lt;P&gt;Therefore your construct makes no sense.&lt;/P&gt;
&lt;P&gt;Solve your problem by retaining period in the data step; set it to zero at every negative change and increment it otherwise.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input date :ddmmyy10. change :percent5. ;
format date ddmmyy10. change percent5.;
cards;
13/04/2017 1%
12/04/2017 2%
11/04/2017 1%
10/04/2017 -1%
;
run;

proc sort data=have;
by date;
run;

data want;
set have;
retain period;
if change &amp;lt; 0
then period = 0;
else period + 1;
run;

proc print data=want noobs;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;      date    change    period

10/04/2017    ( 1%)        0  
11/04/2017      1%         1  
12/04/2017      2%         2  
13/04/2017      1%         3  
&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Apr 2017 11:13:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-until-lag-amp-i-var-gt-0/m-p/349715#M81181</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-04-13T11:13:54Z</dc:date>
    </item>
    <item>
      <title>Re: Do until lag&amp;i(var) gt 0</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-until-lag-amp-i-var-gt-0/m-p/349723#M81185</link>
      <description>&lt;P&gt;Thanks KurtBrenser&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If my dataset had another column called signal and I want to calculate the number of days since the last price change from the signal how would I alter the code below to accomodate that. Here is the revised dataset:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When signal is B, calculate the number of periods since the last price decrease.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;data have;&lt;BR /&gt;input signal $1. date :ddmmyy10. change :percent5. period;&lt;BR /&gt;format signal $1. date ddmmyy10. change percent5. period;&lt;BR /&gt;cards;&lt;BR /&gt;B 13/04/2017 1% 3&lt;BR /&gt;# 12/04/2017 2% .&lt;BR /&gt;# 11/04/2017 1% .&lt;BR /&gt;# 10/04/2017 -1% .&lt;BR /&gt;# 09/04/2017 -1% .&lt;BR /&gt;# 08/04/2017 1% .&lt;BR /&gt;# 07/04/2017 2% .&lt;BR /&gt;# 06/04/2017 3% .&lt;BR /&gt;B 05/04/2017 1% 2&lt;BR /&gt;# 04/04/2017 2% .&lt;BR /&gt;# 03/04/2017 -1% .&lt;BR /&gt;# 02/04/2017 -1% .&lt;BR /&gt;# 01/04/2017 2% .&lt;BR /&gt;# 31/03/2017 1% .&lt;BR /&gt;# 30/03/2017 2% .&lt;BR /&gt;# 29/03/2017 3% .&lt;BR /&gt;# 28/03/2017 4% .&lt;BR /&gt;B 27/03/2017 1% 1&lt;BR /&gt;# 26/03/2017 -1% .&lt;BR /&gt;# 25/03/2017 -1% .&lt;BR /&gt;# 24/03/2017 -2% .&lt;BR /&gt;# 23/03/2017 1% .&lt;BR /&gt;# 22/03/2017 1% .&lt;BR /&gt;# 21/03/2017 -1% .&lt;BR /&gt;# 20/03/2017 -1% .&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;I then want to soemone pull the price information from the last period where there was a price decrease so my final dataset would look like.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;input signal $1. date :ddmmyy10. change :percent5. period change_last_neg :percent5.;&lt;BR /&gt;format signal $1. date ddmmyy10. change percent5. period change_last_neg percent5.; &lt;BR /&gt;cards;&lt;BR /&gt;B 13/04/2017 1% 3 -1%&lt;BR /&gt;# 12/04/2017 2% . &lt;BR /&gt;# 11/04/2017 1% . &lt;BR /&gt;# 10/04/2017 -1% . &lt;BR /&gt;# 09/04/2017 -1% . &lt;BR /&gt;# 08/04/2017 1% . &lt;BR /&gt;# 07/04/2017 2% . &lt;BR /&gt;# 06/04/2017 3% . &lt;BR /&gt;B 05/04/2017 1% 2 -1%&lt;BR /&gt;# 04/04/2017 2% . &lt;BR /&gt;# 03/04/2017 -1% . &lt;BR /&gt;# 02/04/2017 -1% . &lt;BR /&gt;# 01/04/2017 2% . &lt;BR /&gt;# 31/03/2017 1% . &lt;BR /&gt;# 30/03/2017 2% . &lt;BR /&gt;# 29/03/2017 3% . &lt;BR /&gt;# 28/03/2017 4% . &lt;BR /&gt;B 27/03/2017 1% 1 -1%&lt;BR /&gt;# 26/03/2017 -1% . &lt;BR /&gt;# 25/03/2017 -1% . &lt;BR /&gt;# 24/03/2017 -2% . &lt;BR /&gt;# 23/03/2017 1% . &lt;BR /&gt;# 22/03/2017 1% . &lt;BR /&gt;# 21/03/2017 -1% . &lt;BR /&gt;# 20/03/2017 -1% . &lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Apr 2017 12:11:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-until-lag-amp-i-var-gt-0/m-p/349723#M81185</guid>
      <dc:creator>brophymj</dc:creator>
      <dc:date>2017-04-13T12:11:23Z</dc:date>
    </item>
    <item>
      <title>Re: Do until lag&amp;i(var) gt 0</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-until-lag-amp-i-var-gt-0/m-p/349725#M81187</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31948"&gt;@brophymj&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Thanks KurtBrenser&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If my dataset had another column called signal and I want to calculate the number of days since the last price change from the signal how would I alter the code below to accomodate that. Here is the revised dataset:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When signal is B, calculate the number of periods since the last price decrease.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Now that should not be so hard, just a slight extension:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
retain _period;
if change &amp;lt; 0
then _period = 0;
else _period + 1;
if signal = 'B' then period = _period;
drop _period;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 13 Apr 2017 12:13:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-until-lag-amp-i-var-gt-0/m-p/349725#M81187</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-04-13T12:13:43Z</dc:date>
    </item>
    <item>
      <title>Re: Do until lag&amp;i(var) gt 0</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-until-lag-amp-i-var-gt-0/m-p/349726#M81188</link>
      <description>&lt;P&gt;Thanks but it doesn't produce the dataset that's Im looking for. When I run this code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;input signal $1. date :ddmmyy10. change :percent5.;&lt;BR /&gt;format signal $1. date ddmmyy10. change percent5. ; &lt;BR /&gt;cards;&lt;BR /&gt;B 13/04/2017 1%&lt;BR /&gt;# 12/04/2017 2%&lt;BR /&gt;# 11/04/2017 1%&lt;BR /&gt;# 10/04/2017 -1&lt;BR /&gt;# 09/04/2017 -1%&lt;BR /&gt;# 08/04/2017 1%&lt;BR /&gt;# 07/04/2017 2%&lt;BR /&gt;# 06/04/2017 3%&lt;BR /&gt;B 05/04/2017 1%&lt;BR /&gt;# 04/04/2017 2%&lt;BR /&gt;# 03/04/2017 -1%&lt;BR /&gt;# 02/04/2017 -1%&lt;BR /&gt;# 01/04/2017 2% &lt;BR /&gt;# 31/03/2017 1%&lt;BR /&gt;# 30/03/2017 2%&lt;BR /&gt;# 29/03/2017 3%&lt;BR /&gt;# 28/03/2017 4%&lt;BR /&gt;B 27/03/2017 1%&lt;BR /&gt;# 26/03/2017 -1%&lt;BR /&gt;# 25/03/2017 -1%&lt;BR /&gt;# 24/03/2017 -2%&lt;BR /&gt;# 23/03/2017 1%&lt;BR /&gt;# 22/03/2017 1%&lt;BR /&gt;# 21/03/2017 -1%&lt;BR /&gt;# 20/03/2017 -1%&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;retain period;&lt;BR /&gt;if change &amp;lt; 0&lt;BR /&gt;then period = 0;&lt;BR /&gt;else period + 1;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;set want;&lt;BR /&gt;retain _period;&lt;BR /&gt;if change &amp;lt; 0&lt;BR /&gt;then _period = 0;&lt;BR /&gt;else _period + 1;&lt;BR /&gt;if signal = 'B' then period = _period;&lt;BR /&gt;drop _period;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't get the data set below&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;data have;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;input signal $1. date :ddmmyy10. change &lt;/SPAN&gt;&lt;IMG class="emoticon emoticon-smileytongue" src="https://communities.sas.com/i/smilies/16x16_smiley-tongue.png" border="0" alt="Smiley Tongue" title="Smiley Tongue" /&gt;&lt;SPAN&gt;ercent5. period change_last_neg &lt;/SPAN&gt;&lt;IMG class="emoticon emoticon-smileytongue" src="https://communities.sas.com/i/smilies/16x16_smiley-tongue.png" border="0" alt="Smiley Tongue" title="Smiley Tongue" /&gt;&lt;SPAN&gt;ercent5.;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;format signal $1. date ddmmyy10. change percent5. period change_last_neg percent5.; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;cards;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;B 13/04/2017 1% 3 -1%&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# 12/04/2017 2% . &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# 11/04/2017 1% . &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# 10/04/2017 -1% . &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# 09/04/2017 -1% . &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# 08/04/2017 1% . &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# 07/04/2017 2% . &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# 06/04/2017 3% . &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;B 05/04/2017 1% 2 -1%&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# 04/04/2017 2% . &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# 03/04/2017 -1% . &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# 02/04/2017 -1% . &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# 01/04/2017 2% . &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# 31/03/2017 1% . &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# 30/03/2017 2% . &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# 29/03/2017 3% . &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# 28/03/2017 4% . &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;B 27/03/2017 1% 1 -1%&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# 26/03/2017 -1% . &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# 25/03/2017 -1% . &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# 24/03/2017 -2% . &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# 23/03/2017 1% . &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# 22/03/2017 1% . &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# 21/03/2017 -1% . &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# 20/03/2017 -1% . &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Also do you know how to pull the price change from the last period with a negative price change i.e.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;data have;&lt;BR /&gt;input signal $1. date :ddmmyy10. change &lt;IMG class="emoticon emoticon-smileytongue" src="https://communities.sas.com/i/smilies/16x16_smiley-tongue.png" border="0" alt="Smiley Tongue" title="Smiley Tongue" /&gt;ercent5. period change_last_neg &lt;IMG class="emoticon emoticon-smileytongue" src="https://communities.sas.com/i/smilies/16x16_smiley-tongue.png" border="0" alt="Smiley Tongue" title="Smiley Tongue" /&gt;ercent5.;&lt;BR /&gt;format signal $1. date ddmmyy10. change percent5. period change_last_neg percent5.; &lt;BR /&gt;cards;&lt;BR /&gt;B 13/04/2017 1% 3 -1%&lt;BR /&gt;# 12/04/2017 2% . &lt;BR /&gt;# 11/04/2017 1% . &lt;BR /&gt;# 10/04/2017 -1% . &lt;BR /&gt;# 09/04/2017 -1% . &lt;BR /&gt;# 08/04/2017 1% . &lt;BR /&gt;# 07/04/2017 2% . &lt;BR /&gt;# 06/04/2017 3% . &lt;BR /&gt;B 05/04/2017 1% 2 -1%&lt;BR /&gt;# 04/04/2017 2% . &lt;BR /&gt;# 03/04/2017 -1% . &lt;BR /&gt;# 02/04/2017 -1% . &lt;BR /&gt;# 01/04/2017 2% . &lt;BR /&gt;# 31/03/2017 1% . &lt;BR /&gt;# 30/03/2017 2% . &lt;BR /&gt;# 29/03/2017 3% . &lt;BR /&gt;# 28/03/2017 4% . &lt;BR /&gt;B 27/03/2017 1% 1 -1%&lt;BR /&gt;# 26/03/2017 -1% . &lt;BR /&gt;# 25/03/2017 -1% . &lt;BR /&gt;# 24/03/2017 -2% . &lt;BR /&gt;# 23/03/2017 1% . &lt;BR /&gt;# 22/03/2017 1% . &lt;BR /&gt;# 21/03/2017 -1% . &lt;BR /&gt;# 20/03/2017 -1% . &lt;BR /&gt;;&lt;BR /&gt;run;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Apr 2017 12:26:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-until-lag-amp-i-var-gt-0/m-p/349726#M81188</guid>
      <dc:creator>brophymj</dc:creator>
      <dc:date>2017-04-13T12:26:04Z</dc:date>
    </item>
    <item>
      <title>Re: Do until lag&amp;i(var) gt 0</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-until-lag-amp-i-var-gt-0/m-p/349744#M81199</link>
      <description>&lt;P&gt;Read my initial answer again. You missed the sort, which is crucial for the logic. Also note that only one of the data steps is needed, either the simple version or the extended version from my second answer.&lt;/P&gt;
&lt;P&gt;If you need the original most-recent-first order, add a proc sort at the end that reverses the sort order.&lt;/P&gt;</description>
      <pubDate>Thu, 13 Apr 2017 12:54:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-until-lag-amp-i-var-gt-0/m-p/349744#M81199</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-04-13T12:54:56Z</dc:date>
    </item>
    <item>
      <title>Re: Do until lag&amp;i(var) gt 0</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-until-lag-amp-i-var-gt-0/m-p/349749#M81204</link>
      <description>&lt;P&gt;Thanks Kurt. That works now. If I want to retain the price change data from the last period with a price change, how would I go about doing this. Can i use the lag function with period to get this? So I know the last negative change was 3 periods ago, what was that change?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Apr 2017 13:04:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-until-lag-amp-i-var-gt-0/m-p/349749#M81204</guid>
      <dc:creator>brophymj</dc:creator>
      <dc:date>2017-04-13T13:04:30Z</dc:date>
    </item>
    <item>
      <title>Re: Do until lag&amp;i(var) gt 0</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-until-lag-amp-i-var-gt-0/m-p/349750#M81205</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31948"&gt;@brophymj&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Thanks Kurt. That works now. If I want to retain the price change data from the last period with a price change, how would I go about doing this. Can i use the lag function with period to get this? So I know the last negative change was 3 periods ago, what was that change?&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Use another RETAINed variable for this.&lt;/P&gt;</description>
      <pubDate>Thu, 13 Apr 2017 13:06:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-until-lag-amp-i-var-gt-0/m-p/349750#M81205</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-04-13T13:06:48Z</dc:date>
    </item>
  </channel>
</rss>

