<?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: Conditionally insert rows in a table in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-insert-rows-in-a-table/m-p/217977#M267782</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;PRE&gt;data have;
&amp;nbsp; id=1; date="01jan15"d; hour=3; minute=4; price=12; volume=16; output;
&amp;nbsp; id=1; date="01jan15"d; hour=3; minute=5; price=16; volume=20; output;
&amp;nbsp; id=1; date="01jan15"d; hour=3; minute=10; price=8; volume=4; output;
&amp;nbsp; id=1; date="01jan15"d; hour=5; minute=10; price=7; volume=25; output;
&amp;nbsp; format date date9.;
run;
data want;
 merge have have(firstobs=2 rename=(id=_id date=_date hour=_hour minute=_minute));
 output;
 if id=_id then do;
&amp;nbsp;&amp;nbsp; do i=dhms(date,hour,minute,0)+60 to dhms(_date,_hour,_minute,0)-60 by 60;
&amp;nbsp;&amp;nbsp;&amp;nbsp; date=datepart(i);hour=hour(timepart(i));minute=minute(timepart(i));price=.;volume=.;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;output;
&amp;nbsp;&amp;nbsp; end;
 end; format i datetime.;
drop _:;
run;

&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Xia Keshan&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 22 May 2015 12:36:01 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2015-05-22T12:36:01Z</dc:date>
    <item>
      <title>Conditionally insert rows in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-insert-rows-in-a-table/m-p/217969#M267774</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi everyone, &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I need some help. I have a massive dataset (tick by tick data). The columns of the table are: Dates(i.e.different dates), Hour, Minutes, Price, Volume. My problem is that some minutes are missing, so I want to insert them in the data. For instance I have data for minute 25 26 28 30 35 and I want to add rows for minute 27, 29, 31, 32,34, 33 etc for each hour and each date. The data for the price and volume&amp;nbsp; in the new rows should be empty. Could you please give any suggestion? Thanks&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 21 May 2015 12:03:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-insert-rows-in-a-table/m-p/217969#M267774</guid>
      <dc:creator>Themis_84</dc:creator>
      <dc:date>2015-05-21T12:03:34Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally insert rows in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-insert-rows-in-a-table/m-p/217970#M267775</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You have two options.&amp;nbsp; One is to go through the dataset, and do do-loops where gaps exist.&amp;nbsp; The other option is to create a template and merge that back to your data.&amp;nbsp; I will give an example of the second approach:&lt;/P&gt;&lt;P&gt;data have;&lt;/P&gt;&lt;P&gt;&amp;nbsp; id=1; date="01jan15"d; hour=3; minute=4; price=12; volume=16; output;&lt;/P&gt;&lt;P&gt;&amp;nbsp; id=1; date="01jan15"d; hour=3; minute=5; price=16; volume=20; output;&lt;/P&gt;&lt;P&gt;&amp;nbsp; id=1; date="01jan15"d; hour=3; minute=10; price=8; volume=4; output;&lt;/P&gt;&lt;P&gt;&amp;nbsp; id=1; date="01jan15"d; hour=5; minute=10; price=7; volume=25; output;&lt;/P&gt;&lt;P&gt;&amp;nbsp; format date date9.;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;/* Create a dataset expanded for each ID from minimum to maximum */&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;&amp;nbsp; create table TEMPLATE as&lt;/P&gt;&lt;P&gt;&amp;nbsp; select&amp;nbsp; ID,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; min(dhms(date,hour,minute,0)) as LOW,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; max(dhms(date,hour,minute,0)) as HIGH&lt;/P&gt;&lt;P&gt;&amp;nbsp; from&amp;nbsp;&amp;nbsp;&amp;nbsp; HAVE&lt;/P&gt;&lt;P&gt;&amp;nbsp; group by ID;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;data template (drop=low high i);&lt;/P&gt;&lt;P&gt;&amp;nbsp; set template;&lt;/P&gt;&lt;P&gt;&amp;nbsp; do i=low to high by 60;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; date=datepart(i);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; hour=hour(i);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; minute=minute(i);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; output;&lt;/P&gt;&lt;P&gt;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;/* Merge the template onto the data */&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;&amp;nbsp; create table WANT as&lt;/P&gt;&lt;P&gt;&amp;nbsp; select&amp;nbsp; COALESCE(A.ID,B.ID) as ID,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; COALESCE(A.DATE,B.DATE) as DATE format=date9.,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; COALESCE(A.HOUR,B.HOUR) as HOUR,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; COALESCE(A.MINUTE,B.MINUTE) as MINUTE,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; A.PRICE,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; A.VOLUME &lt;/P&gt;&lt;P&gt;&amp;nbsp; from&amp;nbsp;&amp;nbsp;&amp;nbsp; WORK.HAVE A&lt;/P&gt;&lt;P&gt;&amp;nbsp; full join WORK.TEMPLATE B&lt;/P&gt;&lt;P&gt;&amp;nbsp; on&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; A.ID=B.ID&lt;/P&gt;&lt;P&gt;&amp;nbsp; and&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; A.DATE=B.DATE&lt;/P&gt;&lt;P&gt;&amp;nbsp; and&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; A.HOUR=B.HOUR&lt;/P&gt;&lt;P&gt;&amp;nbsp; and&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; A.MINUTE=B.MINUTE;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 21 May 2015 12:47:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-insert-rows-in-a-table/m-p/217970#M267775</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2015-05-21T12:47:48Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally insert rows in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-insert-rows-in-a-table/m-p/217971#M267776</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you so much RW9 for the quick reply. I will try it and come back to you whether it works for my dataset. I run your example and works. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 21 May 2015 12:58:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-insert-rows-in-a-table/m-p/217971#M267776</guid>
      <dc:creator>Themis_84</dc:creator>
      <dc:date>2015-05-21T12:58:36Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally insert rows in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-insert-rows-in-a-table/m-p/217972#M267777</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 13px; background-color: #ffffff;"&gt;Just a quick question. How can I get the ID for each date?Should I do a loop? Thanks&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 21 May 2015 14:04:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-insert-rows-in-a-table/m-p/217972#M267777</guid>
      <dc:creator>Themis_84</dc:creator>
      <dc:date>2015-05-21T14:04:10Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally insert rows in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-insert-rows-in-a-table/m-p/217973#M267778</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;SPAN style="font-size: 13.3333330154419px;"&gt;Not sure I am following you?&amp;nbsp; This section will get you distinct ID's with their first and last dates, its used in the loop over, so if min date is 01jan15 and 05jan15 is max, then it will do each day.&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 21 May 2015 14:05:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-insert-rows-in-a-table/m-p/217973#M267778</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2015-05-21T14:05:30Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally insert rows in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-insert-rows-in-a-table/m-p/217974#M267779</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I mean in my set I do not have ID column. So, I want first to generate this column for each date. For example I have 01/02/2007, 01/02/2007 etc, 02/02/2007 etc. I understand I should have an ID 1 for &lt;SPAN style="font-size: 13.3333330154419px;"&gt; 01/02/2007, ID 2 for &lt;SPAN style="font-size: 13.3333330154419px;"&gt;02/02/2007 etc in order to implement the code.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 21 May 2015 14:21:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-insert-rows-in-a-table/m-p/217974#M267779</guid>
      <dc:creator>Themis_84</dc:creator>
      <dc:date>2015-05-21T14:21:04Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally insert rows in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-insert-rows-in-a-table/m-p/217975#M267780</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Oh, just drop the id bit then:&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;proc sql;&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&amp;nbsp; create table TEMPLATE as&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&amp;nbsp; select&amp;nbsp; &lt;SPAN style="line-height: 1.5em;"&gt;min(dhms(date,hour,minute,0)) as LOW,&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; max(dhms(date,hour,minute,0)) as HIGH&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&amp;nbsp; from&amp;nbsp;&amp;nbsp;&amp;nbsp; HAVE&lt;SPAN style="line-height: 1.5em;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;quit;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 21 May 2015 14:24:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-insert-rows-in-a-table/m-p/217975#M267780</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2015-05-21T14:24:23Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally insert rows in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-insert-rows-in-a-table/m-p/217976#M267781</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you so much RW9. It worked perfectly. &lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://communities.sas.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;&amp;nbsp; I did the necessary adjustments and now I have exactly what I wanted.&amp;nbsp; I am new to SAS and I have tried everything by myself, so your help saved me hours &lt;img id="smileywink" class="emoticon emoticon-smileywink" src="https://communities.sas.com/i/smilies/16x16_smiley-wink.png" alt="Smiley Wink" title="Smiley Wink" /&gt;. I have been trying to fix this problem for the past two days. :smileyplain: &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 21 May 2015 16:03:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-insert-rows-in-a-table/m-p/217976#M267781</guid>
      <dc:creator>Themis_84</dc:creator>
      <dc:date>2015-05-21T16:03:17Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally insert rows in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-insert-rows-in-a-table/m-p/217977#M267782</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;PRE&gt;data have;
&amp;nbsp; id=1; date="01jan15"d; hour=3; minute=4; price=12; volume=16; output;
&amp;nbsp; id=1; date="01jan15"d; hour=3; minute=5; price=16; volume=20; output;
&amp;nbsp; id=1; date="01jan15"d; hour=3; minute=10; price=8; volume=4; output;
&amp;nbsp; id=1; date="01jan15"d; hour=5; minute=10; price=7; volume=25; output;
&amp;nbsp; format date date9.;
run;
data want;
 merge have have(firstobs=2 rename=(id=_id date=_date hour=_hour minute=_minute));
 output;
 if id=_id then do;
&amp;nbsp;&amp;nbsp; do i=dhms(date,hour,minute,0)+60 to dhms(_date,_hour,_minute,0)-60 by 60;
&amp;nbsp;&amp;nbsp;&amp;nbsp; date=datepart(i);hour=hour(timepart(i));minute=minute(timepart(i));price=.;volume=.;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;output;
&amp;nbsp;&amp;nbsp; end;
 end; format i datetime.;
drop _:;
run;

&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Xia Keshan&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 22 May 2015 12:36:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-insert-rows-in-a-table/m-p/217977#M267782</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2015-05-22T12:36:01Z</dc:date>
    </item>
  </channel>
</rss>

