<?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: Loop with conditional range in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Loop-with-conditional-range/m-p/437080#M282212</link>
    <description>&lt;P&gt;Yes,&amp;nbsp;a&amp;nbsp;indicator variable in the same row that&amp;nbsp;the opposite category appears.&lt;/P&gt;</description>
    <pubDate>Wed, 14 Feb 2018 13:50:32 GMT</pubDate>
    <dc:creator>fri0</dc:creator>
    <dc:date>2018-02-14T13:50:32Z</dc:date>
    <item>
      <title>Loop with conditional range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-with-conditional-range/m-p/436956#M282210</link>
      <description>&lt;P&gt;Hi, I need your help to count values of a variable but with a condition based on time ranges of another variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is my data:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="245"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="83"&gt;Date&lt;/TD&gt;
&lt;TD width="79"&gt;Time&lt;/TD&gt;
&lt;TD width="83"&gt;Type&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;01-dic&lt;/TD&gt;
&lt;TD&gt;08:00:10&lt;/TD&gt;
&lt;TD&gt;Demanda&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;01-dic&lt;/TD&gt;
&lt;TD&gt;08:00:13&lt;/TD&gt;
&lt;TD&gt;Demanda&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;01-dic&lt;/TD&gt;
&lt;TD&gt;08:00:14&lt;/TD&gt;
&lt;TD&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lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;01-dic&lt;/TD&gt;
&lt;TD&gt;08:01:29&lt;/TD&gt;
&lt;TD&gt;Oferta&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;01-dic&lt;/TD&gt;
&lt;TD&gt;08:01:35&lt;/TD&gt;
&lt;TD&gt;Oferta&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;01-dic&lt;/TD&gt;
&lt;TD&gt;08:01:39&lt;/TD&gt;
&lt;TD&gt;Demanda&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;01-dic&lt;/TD&gt;
&lt;TD&gt;08:01:39&lt;/TD&gt;
&lt;TD&gt;Oferta&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;01-dic&lt;/TD&gt;
&lt;TD&gt;08:01:39&lt;/TD&gt;
&lt;TD&gt;Oferta&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;01-dic&lt;/TD&gt;
&lt;TD&gt;08:01:42&lt;/TD&gt;
&lt;TD&gt;Demanda&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have to find in 50 seconds time ranges, if there are 5 or more "Demanda" Category and a "Oferta" just after that or also the opposite case, 5 o more "Oferta" followed by a "Demanda".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please I have no experience with loops or arrays, I tried to code a loop, but I do not how to make a reference to previous values of another variable. I would really appreciate if you could explain me the lines in your code to understand each step. Thank you very much!&lt;/P&gt;</description>
      <pubDate>Wed, 14 Feb 2018 02:51:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-with-conditional-range/m-p/436956#M282210</guid>
      <dc:creator>fri0</dc:creator>
      <dc:date>2018-02-14T02:51:38Z</dc:date>
    </item>
    <item>
      <title>Re: Loop with conditional range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-with-conditional-range/m-p/437002#M282211</link>
      <description>&lt;P&gt;So if there are 5 or more&amp;nbsp;&lt;SPAN&gt;"Demanda" category values immediately followed by a&amp;nbsp;"Oferta" in a range of 50 second in the&amp;nbsp;Time variable, what do you want to happen then? An indicator variable of 1?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Feb 2018 06:57:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-with-conditional-range/m-p/437002#M282211</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2018-02-14T06:57:32Z</dc:date>
    </item>
    <item>
      <title>Re: Loop with conditional range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-with-conditional-range/m-p/437080#M282212</link>
      <description>&lt;P&gt;Yes,&amp;nbsp;a&amp;nbsp;indicator variable in the same row that&amp;nbsp;the opposite category appears.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Feb 2018 13:50:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-with-conditional-range/m-p/437080#M282212</guid>
      <dc:creator>fri0</dc:creator>
      <dc:date>2018-02-14T13:50:32Z</dc:date>
    </item>
    <item>
      <title>Re: Loop with conditional range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-with-conditional-range/m-p/437488#M282213</link>
      <description>&lt;P&gt;You can use RETAIN.&lt;/P&gt;&lt;P&gt;By this you can shift information one row lower and then make comparisons, set counters and calculate time distances and sum then up.&lt;/P&gt;&lt;P&gt;Below i have coded an example. I have not much experience in using RETAIN, so it might be coded more efficient.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data a;&lt;BR /&gt;&lt;BR /&gt;minute = 1; transcode = 'A'; revenue = 11.64;&lt;BR /&gt;&lt;BR /&gt;output;&lt;BR /&gt;&lt;BR /&gt;minute = 4; transcode = 'A'; revenue = 8.34;&lt;BR /&gt;&lt;BR /&gt;output;&lt;BR /&gt;&lt;BR /&gt;minute = 7; transcode = 'A'; revenue = 6.09;&lt;BR /&gt;&lt;BR /&gt;output;&lt;BR /&gt;&lt;BR /&gt;minute = 10; transcode = 'B'; revenue = 5.38;&lt;BR /&gt;&lt;BR /&gt;output;&lt;BR /&gt;minute = 15; transcode = 'C'; revenue = 4.34;&lt;BR /&gt;&lt;BR /&gt;output;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data a;&lt;BR /&gt;&lt;BR /&gt;set a;&lt;BR /&gt;&lt;BR /&gt;if _n_= 1 then do; tmp1= minute; tmp2= transcode; tmp3 = revenue; end;&lt;BR /&gt;&lt;BR /&gt;retain tmp1 tmp2 tmp3;&lt;BR /&gt;&lt;BR /&gt;prae_minute= tmp1; prae_transcode= tmp2; prae_revenue_chain = tmp3;&lt;BR /&gt;&lt;BR /&gt;if prae_transcode = transcode and _n_ &amp;gt; 1 then revenue_chain= prae_revenue_chain + revenue; else revenue_chain = revenue;&lt;BR /&gt;&lt;BR /&gt;tmp1= minute; tmp2= transcode; tmp3 = revenue_chain;&lt;BR /&gt;&lt;BR /&gt;keep minute transcode revenue revenue_chain;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc print data = a; run;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Feb 2018 10:29:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-with-conditional-range/m-p/437488#M282213</guid>
      <dc:creator>atvf</dc:creator>
      <dc:date>2018-02-15T10:29:35Z</dc:date>
    </item>
    <item>
      <title>Re: Loop with conditional range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-with-conditional-range/m-p/438434#M282214</link>
      <description>&lt;P&gt;Hi, I googled about retain to build my code, but it's not perfect yet... This is my code so far:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data data_lay2;
set data_lay1;
by fecha hora;
retain lapse compra cod dif 0;
if hora&amp;gt;=lag(hora) then do;
	if tipo='Demanda' and clase='Ingreso' then compra+1;
	if codigo ne lag(codigo) then cod+1;
	dif=intck('second',lag(hora),hora);
	if lapse=. then lapse=0;
	lapse=lapse+dif;
	if compra&amp;gt;=5 and cod&amp;gt;=5 and tipo='Oferta' then flag=1;
end;
if lapse&amp;gt;50 then lapse=0 and compra=0 and cod=0 and dif=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 19 Feb 2018 16:48:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-with-conditional-range/m-p/438434#M282214</guid>
      <dc:creator>fri0</dc:creator>
      <dc:date>2018-02-19T16:48:41Z</dc:date>
    </item>
    <item>
      <title>Re: Loop with conditional range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-with-conditional-range/m-p/438652#M282215</link>
      <description>&lt;P&gt;This program tracks the time stamp for demand&amp;nbsp;4 demands prior to the most recent demand, and for offer&amp;nbsp;4 offers prior to the most recent offer.&amp;nbsp; You can then compare those time stamps to the current time:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data data_lay2;
  set data_lay1;
  by fecha hora;
  retain lagtime4d lagtime4o ;
  format lagtime4d lagtime4o time8.0 ;

  if tipo='Demanda' then lagtime4d=lag4(hora); else
  if tipo='Oferta'  then lagtime4o=lag4(hora);

  if (tipo='Demanda' and lagtime4o&amp;gt;=hora-50) 
  or (tipo='Oferta'  and lagtime4d&amp;gt;=hora-50);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This program forms 2 queues, one for time stamps&amp;nbsp;of offers, and one for time stamps of demands.&amp;nbsp; Each queue is 4 elements deep.&amp;nbsp; As a result, &lt;EM&gt;&lt;STRONG&gt;when the current record is an offer&lt;/STRONG&gt;&lt;/EM&gt;, then the lagtime4d is the time stamp of the 5th preceding demand.&amp;nbsp; I say the 5th preceding demand for an offer, because it is calculated as the 4th preceding demand of the most recent preceding demand.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As a result the subsetting IF statement just compares the current time stamp to either the lagtime4d or lagtime4o, depending on the current record type.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I presume HORA is a time variable, with sas time values.&lt;/P&gt;</description>
      <pubDate>Tue, 20 Feb 2018 14:47:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-with-conditional-range/m-p/438652#M282215</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-02-20T14:47:46Z</dc:date>
    </item>
  </channel>
</rss>

