<?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: looping over time in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/looping-over-time/m-p/252883#M48086</link>
    <description>&lt;P&gt;Here's a sketch - untested. It most likely won't work as is, but you should be able to get the idea and modify the code as required.&lt;/P&gt;
&lt;P&gt;SAS stores dates as the number of days from January 1, 1960 and datetime as number of seconds from January 1, 1960. This means you can treat it as a number and do math with it &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
set have;
datetime=date*60*60*24+time; *Convert to datetime;
datetime_round=floor(datetime/300); *round to 5 minute intervals;&lt;BR /&gt;&lt;BR /&gt;format datetime_round datetime21.;
run;

proc means data=temp noprint nway;
class datetime_round;
var price;
output out=temp2 min(price)=min_price max(price)=max_price;
run;

data want;
set temp2;
Ratio=max/min;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 27 Feb 2016 02:44:43 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2016-02-27T02:44:43Z</dc:date>
    <item>
      <title>looping over time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/looping-over-time/m-p/252873#M48079</link>
      <description>&lt;P&gt;Dear all;&lt;/P&gt;&lt;P&gt;a beginner question&lt;/P&gt;&lt;P&gt;How can I create a loop that will select the data for five minute intervals...&lt;/P&gt;&lt;P&gt;Basically, I want the program to choose the five minutes intervals between 1530 and 22:00.&lt;/P&gt;&lt;P&gt;Said differently, I would like the program to choose 15:30-15:35, 15:35-15:40 ,so forth untl 22:00,,,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;then within that loop I would like to make calculations for the subset,...then output them,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;also for each interval, is it possible to use the proc procedures like proc mean, proc reg, etc&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sony&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 27 Feb 2016 01:27:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/looping-over-time/m-p/252873#M48079</guid>
      <dc:creator>Sonyboy</dc:creator>
      <dc:date>2016-02-27T01:27:35Z</dc:date>
    </item>
    <item>
      <title>Re: looping over time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/looping-over-time/m-p/252874#M48080</link>
      <description>&lt;P&gt;What do you mean by select data?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If if you need to run Procs in each loop you will need a macro loop.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However its its highly likely that BY processing will be more efficient.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I suggest posting more details and an example of what you're trying to achieve.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 27 Feb 2016 01:38:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/looping-over-time/m-p/252874#M48080</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-02-27T01:38:44Z</dc:date>
    </item>
    <item>
      <title>Re: looping over time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/looping-over-time/m-p/252878#M48083</link>
      <description>&lt;P&gt;Reeza,&lt;/P&gt;&lt;P&gt;I am a beginner, therefore not sure I can explain myself clearly. What I need is that the loop makes calculations for each of the 5 minutes intervals in a time horizon. For each interval, i need to calculate the ratio of max/min and see these values separetely...&lt;/P&gt;&lt;P&gt;How would a macro look like?&lt;/P&gt;&lt;P&gt;Below is a failed example of a code?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data b;&lt;BR /&gt;do i='15:30:00't to '22:00:00't by '00:05:00't;&lt;BR /&gt;do j=1 to 79;&lt;BR /&gt;data a;&lt;BR /&gt;set basefile;&lt;BR /&gt;keep date time midprice;&lt;BR /&gt;where time&amp;gt;i and time&amp;lt; (i+'00:05:00't);&lt;BR /&gt;proc means data=a noprint ;&lt;BR /&gt;var midprice;&lt;BR /&gt;by date ;&lt;BR /&gt;output out=a1;&lt;BR /&gt;run;&lt;BR /&gt;data a2;&lt;BR /&gt;set a1;&lt;BR /&gt;if _STAT_="MIN" ;&lt;BR /&gt;rename midprice=minR;&lt;BR /&gt;run;&lt;BR /&gt;data a3;&lt;BR /&gt;set a1;&lt;BR /&gt;if _STAT_="MAX" ;&lt;BR /&gt;rename midprice=maxR;&lt;BR /&gt;run;&lt;BR /&gt;end;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Sat, 27 Feb 2016 02:07:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/looping-over-time/m-p/252878#M48083</guid>
      <dc:creator>Sonyboy</dc:creator>
      <dc:date>2016-02-27T02:07:27Z</dc:date>
    </item>
    <item>
      <title>Re: looping over time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/looping-over-time/m-p/252880#M48084</link>
      <description>&lt;P&gt;You don't need a macro, round time to nearest 5 mins and run proc means with by group.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Post what your data looks like and what you want as output.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 27 Feb 2016 02:11:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/looping-over-time/m-p/252880#M48084</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-02-27T02:11:02Z</dc:date>
    </item>
    <item>
      <title>Re: looping over time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/looping-over-time/m-p/252882#M48085</link>
      <description>&lt;P&gt;Dear Reeza,&lt;/P&gt;&lt;P&gt;Pls see the input and desired output files&lt;/P&gt;&lt;P&gt;I thank you in advance for your help&lt;/P&gt;</description>
      <pubDate>Sat, 27 Feb 2016 02:37:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/looping-over-time/m-p/252882#M48085</guid>
      <dc:creator>Sonyboy</dc:creator>
      <dc:date>2016-02-27T02:37:36Z</dc:date>
    </item>
    <item>
      <title>Re: looping over time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/looping-over-time/m-p/252883#M48086</link>
      <description>&lt;P&gt;Here's a sketch - untested. It most likely won't work as is, but you should be able to get the idea and modify the code as required.&lt;/P&gt;
&lt;P&gt;SAS stores dates as the number of days from January 1, 1960 and datetime as number of seconds from January 1, 1960. This means you can treat it as a number and do math with it &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
set have;
datetime=date*60*60*24+time; *Convert to datetime;
datetime_round=floor(datetime/300); *round to 5 minute intervals;&lt;BR /&gt;&lt;BR /&gt;format datetime_round datetime21.;
run;

proc means data=temp noprint nway;
class datetime_round;
var price;
output out=temp2 min(price)=min_price max(price)=max_price;
run;

data want;
set temp2;
Ratio=max/min;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 27 Feb 2016 02:44:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/looping-over-time/m-p/252883#M48086</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-02-27T02:44:43Z</dc:date>
    </item>
    <item>
      <title>Re: looping over time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/looping-over-time/m-p/252916#M48092</link>
      <description>&lt;P&gt;Reeza,&lt;/P&gt;&lt;P&gt;thank you for your time. I appreciate this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It worked:-)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 27 Feb 2016 10:59:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/looping-over-time/m-p/252916#M48092</guid>
      <dc:creator>Sonyboy</dc:creator>
      <dc:date>2016-02-27T10:59:13Z</dc:date>
    </item>
    <item>
      <title>Re: looping over time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/looping-over-time/m-p/252922#M48093</link>
      <description>&lt;P&gt;Tested using, using SAS functions for date-time variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   infile cards dsd dlm='09'x;
   input date:Date.	time:time.	price_bid	price_ask	midprice	spread	relative_spread;
   format date date9. time time12.;
   dt  = dhms(date,0,0,time);
   dt5 = intnx('minute5',dt,0,'B');
   format dt dt5 datetime21.;
   cards;
02Apr2012	0:00:00	45.06248623	48.9866028	47.02454451	3.924116572	0.083448263
02Apr2012	0:01:00	45.74726852	46.2597066	46.00348756	0.512438082	0.011139114
02Apr2012	0:02:00	45.74726852	46.2597066	46.00348756	0.512438082	0.011139114
02Apr2012	0:03:00	45.74726852	46.2597066	46.00348756	0.512438082	0.011139114
02Apr2012	0:04:00	45.74726852	46.2597066	46.00348756	0.512438082	0.011139114
02Apr2012	0:05:00	45.74726852	46.2597066	46.00348756	0.512438082	0.011139114
02Apr2012	0:06:00	45.74726852	46.2597066	46.00348756	0.512438082	0.011139114
02Apr2012	0:07:00	45.74726852	46.2597066	46.00348756	0.512438082	0.011139114
02Apr2012	0:08:00	45.74726852	46.2597066	46.00348756	0.512438082	0.011139114
02Apr2012	0:09:00	45.74726852	46.2597066	46.00348756	0.512438082	0.011139114
02Apr2012	0:10:00	45.74726852	46.2597066	46.00348756	0.512438082	0.011139114
02Apr2012	0:11:00	45.74726852	46.2597066	46.00348756	0.512438082	0.011139114
;;;;
   run;
proc print;
   run;
ods select none;
ods output summary=summary;
proc means stackods min max;
   class dt5;
   var price_bid--relative_spread;
   run;
ods select all;
proc print;
   run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/2084i19FF709CCCF8044F/image-size/original?v=mpbl-1&amp;amp;px=-1" border="0" alt="Capture.PNG" title="Capture.PNG" /&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 27 Feb 2016 12:08:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/looping-over-time/m-p/252922#M48093</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2016-02-27T12:08:58Z</dc:date>
    </item>
  </channel>
</rss>

