<?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: Proc Sql Question on Rolling Dates with Conditions in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Proc-Sql-Question-on-Rolling-Dates-with-Conditions/m-p/72888#M21138</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;OK.Cartesian Product of DataStep is more powerful than Cartesian Product of Sql. So I used it for your a little complicated problem.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;data temp;
input account $ date : mmddyy10. test id;
format date mmddyy10.;
cards;
384000 6/30/2010 1 13.15
384000 6/15/2010 1 13.14&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
384000 6/10/2010 1 13.14&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
384000 6/1/2010&amp;nbsp; 1 13.13
386000 5/30/2010 1 12.12
386000 5/15/2010 1 12.12
386000 4/25/2010 1 12.13
;
run;

data want;
 set temp;
 found=0; matched=0;flag=0;
 do i=1 to _nobs;
&amp;nbsp; set temp(drop=test rename=(account=_account date=_date id=_id))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; nobs=_nobs point=i;
&amp;nbsp; if account=_account and _date ge date-30 and _date le date-7
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; and id=_id&amp;nbsp; then&amp;nbsp; found=1;
&amp;nbsp; if account=_account and _date ge date-30 and _date le date-7 then matched=1;
&amp;nbsp; if i=_nobs then do;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if not found and matched then flag=1;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; output;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;
 end;
 drop found matched _:;
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;Ksharp&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 30 Jun 2011 06:33:19 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2011-06-30T06:33:19Z</dc:date>
    <item>
      <title>Proc Sql Question on Rolling Dates with Conditions</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-Sql-Question-on-Rolling-Dates-with-Conditions/m-p/72875#M21125</link>
      <description>I have been thinking using Proc Sql self-join on the question below for many days. Somehow, i just cannot get the exact results. I really appreciate anyone can help me to resolve the puzzle. The general logic is "There are more than 2 test use different ID within 2 days from the same account and the total amount by these tests is above 4000 dollars. If so, the flag is 1, else the flag is 0". I want to produce the last column flag.&lt;BR /&gt;
&lt;BR /&gt;
data temp;&lt;BR /&gt;
input obs account $ date : mmddyy10. time : time9. test  id amount hit;&lt;BR /&gt;
format date mmddyy10. time time9.;&lt;BR /&gt;
datalines;&lt;BR /&gt;
&lt;BR /&gt;
1 387000 6/10/2010 12:00:00 1 13.15 2000 1&lt;BR /&gt;
2 387000 6/9/2010 12:00:00 1 13.15 3000 0&lt;BR /&gt;
3 387000 6/8/2010 12:00:00 1 13.14 2000 0&lt;BR /&gt;
4 387000 6/6/2010 11:00:00 1 13.15 2000 1&lt;BR /&gt;
5 387000 6/5/2010 10:00:00 1 13.14 2000 0&lt;BR /&gt;
6 387000 6/4/2010 9:00:00 1 13.13 1000 0&lt;BR /&gt;
7 387000 6/4/2010 8:00:00 1 13.13 500 0&lt;BR /&gt;
8 386000 5/9/2010 10:00:00 1 12.12 5000 0&lt;BR /&gt;
9 386000 5/2/2010 9:00:00 1 12.14 2000 0&lt;BR /&gt;
10 386000 5/1/2010 8:00:00 1 12.13 2000 0&lt;BR /&gt;
11 385000 3/3/2010 10:00:00 1 11.13 5000 1&lt;BR /&gt;
12 385000 3/1/2010 9:00:00 1 11.12 3000 0&lt;BR /&gt;
13 385000 3/1/2010 8:00:00 1 11.11 3000 0&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
For example:&lt;BR /&gt;
In obs 1, account 387000, within 2 days, (06/10 /10 -06/08/10), there are three test adopted different ID (13.15 and 13.14), and the total money is more than 4000 (2000+3000+2000), so the flag is 1&lt;BR /&gt;
In obs 2, account 387000, within 2 days, (06/9/10 -06/07/10), there are only two test adopted different ID, even though the money is above $4000. The flag is 0&lt;BR /&gt;
in Obs 3, account 387000, within 2 days, (06/8/10 -06/06/10), there are only two test adopted different ID, and the money is not more than $4000. The flag is 0&lt;BR /&gt;
in Obs 4, account 387000, within 2 days, (06/6/10 -06/04/10), there are four usage adopted different ID (13.15, 13.14, and13.13), and the money is more than $4000 (it is 5500). The flag is 1&lt;BR /&gt;
&lt;BR /&gt;
The flag has to satisfy the following points:&lt;BR /&gt;
&lt;BR /&gt;
1) In the same account&lt;BR /&gt;
2) within 2 days&lt;BR /&gt;
3) more than 2 test (each observation is one test)&lt;BR /&gt;
4) the sum of the amount by these test is above $4000&lt;BR /&gt;
5) used different ID.&lt;BR /&gt;
&lt;BR /&gt;
Thank you very much for your help!</description>
      <pubDate>Mon, 06 Jun 2011 05:08:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-Sql-Question-on-Rolling-Dates-with-Conditions/m-p/72875#M21125</guid>
      <dc:creator>NIUNIU</dc:creator>
      <dc:date>2011-06-06T05:08:05Z</dc:date>
    </item>
    <item>
      <title>Re: Proc Sql Question on Rolling Dates with Conditions</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-Sql-Question-on-Rolling-Dates-with-Conditions/m-p/72876#M21126</link>
      <description>If I use the following code to run, it will work. However, the run time is so long when the data set is huge with millions of rows. Any way to reduce the code running time?&lt;BR /&gt;
&lt;BR /&gt;
Thank you very much!&lt;BR /&gt;
&lt;BR /&gt;
PROC SQL;&lt;BR /&gt;
&lt;BR /&gt;
SELECT&lt;BR /&gt;
   a.*, 1 AS Flag&lt;BR /&gt;
FROM&lt;BR /&gt;
   TEMP a&lt;BR /&gt;
WHERE &lt;BR /&gt;
   EXISTS &lt;BR /&gt;
   (&lt;BR /&gt;
      SELECT * &lt;BR /&gt;
      FROM TEMP b &lt;BR /&gt;
      WHERE &lt;BR /&gt;
         b.Account = a.Account AND&lt;BR /&gt;
         b.date between a.date-2 and b.date  &lt;BR /&gt;
      HAVING &lt;BR /&gt;
         SUM(b.Amount) &amp;gt;= 4000 &lt;BR /&gt;
         AND COUNT(DISTINCT obs) &amp;gt; 2  &lt;BR /&gt;
   )</description>
      <pubDate>Mon, 06 Jun 2011 19:14:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-Sql-Question-on-Rolling-Dates-with-Conditions/m-p/72876#M21126</guid>
      <dc:creator>NIUNIU</dc:creator>
      <dc:date>2011-06-06T19:14:26Z</dc:date>
    </item>
    <item>
      <title>Re: Proc Sql Question on Rolling Dates with Conditions</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-Sql-Question-on-Rolling-Dates-with-Conditions/m-p/72877#M21127</link>
      <description>I used Cartesian Product of sql is more efficient than your sub-query,But it is also not suitable for large table.&lt;BR /&gt;
[pre]&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data temp;&lt;BR /&gt;
input obs account $ date : mmddyy10. time : time9. test id amount ;&lt;BR /&gt;
format date mmddyy10. time time9.;&lt;BR /&gt;
datalines;&lt;BR /&gt;
&lt;BR /&gt;
1 387000 6/10/2010 12:00:00 1 13.15 2000&lt;BR /&gt;
2 387000 6/9/2010 12:00:00 1 13.15 3000&lt;BR /&gt;
3 387000 6/8/2010 12:00:00 1 13.14 2000&lt;BR /&gt;
4 387000 6/6/2010 11:00:00 1 13.15 2000&lt;BR /&gt;
5 387000 6/5/2010 10:00:00 1 13.14 2000&lt;BR /&gt;
6 387000 6/4/2010 9:00:00 1 13.13 1000&lt;BR /&gt;
7 387000 6/4/2010 8:00:00 1 13.13 500&lt;BR /&gt;
8 386000 5/9/2010 10:00:00 1 12.12 5000&lt;BR /&gt;
9 386000 5/2/2010 9:00:00 1 12.14 2000&lt;BR /&gt;
10 386000 5/1/2010 8:00:00 1 12.13 2000&lt;BR /&gt;
11 385000 3/3/2010 10:00:00 1 11.13 5000&lt;BR /&gt;
12 385000 3/1/2010 9:00:00 1 11.12 3000&lt;BR /&gt;
;&lt;BR /&gt;
run; &lt;BR /&gt;
proc sql ;&lt;BR /&gt;
 create table want(drop=_amount count_id count_test) as&lt;BR /&gt;
  select  distinct a.*,count(distinct b.id) as count_id,count( b.test) as count_test,sum(b.amount) as _amount,&lt;BR /&gt;
      case when calculated _amount gt 4000 and calculated count_id ge 2 and &lt;BR /&gt;
                calculated count_test gt 2 then 1 else 0 end as flag&lt;BR /&gt;
   from temp as a ,temp as b&lt;BR /&gt;
    where a.account = b.account and b.date between a.date-2 and a.date&lt;BR /&gt;
     group by a.obs&lt;BR /&gt;
      ;&lt;BR /&gt;
quit;&lt;BR /&gt;
proc sort data=want;&lt;BR /&gt;
 by descending date;&lt;BR /&gt;
run;&lt;BR /&gt;
  &lt;BR /&gt;
  &lt;BR /&gt;
 [/pre]&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Tue, 07 Jun 2011 03:58:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-Sql-Question-on-Rolling-Dates-with-Conditions/m-p/72877#M21127</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-06-07T03:58:34Z</dc:date>
    </item>
    <item>
      <title>Re: Proc Sql Question on Rolling Dates with Conditions</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-Sql-Question-on-Rolling-Dates-with-Conditions/m-p/72878#M21128</link>
      <description>Hi Ksharp,&lt;BR /&gt;
Thank you very much! However do you know how to resolve the same date, but different time issue.I think if I add another condition with the date like this will work.&lt;BR /&gt;
&lt;BR /&gt;
or (b.date=a.date and b.time le a.time)&lt;BR /&gt;
&lt;BR /&gt;
use "le" because somehow the SAS forum has bugs when produce the less and equal sign.&lt;BR /&gt;
&lt;BR /&gt;
It did not recognized it.Do you have any suggest how to resolve it. &lt;BR /&gt;
Thank you.

Message was edited by: NIUNIU</description>
      <pubDate>Tue, 07 Jun 2011 04:59:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-Sql-Question-on-Rolling-Dates-with-Conditions/m-p/72878#M21128</guid>
      <dc:creator>NIUNIU</dc:creator>
      <dc:date>2011-06-07T04:59:40Z</dc:date>
    </item>
    <item>
      <title>Re: Proc Sql Question on Rolling Dates with Conditions</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-Sql-Question-on-Rolling-Dates-with-Conditions/m-p/72879#M21129</link>
      <description>Hi.&lt;BR /&gt;
I don't know sth about your "the same date, but different time issue ".&lt;BR /&gt;
This issue stands for what ?&lt;BR /&gt;
Do you mean like this " b.date between a.date-2 and a.date"?&lt;BR /&gt;
I think your "or (b.date=a.date and b.time le a.time)" is good.&lt;BR /&gt;
 &lt;BR /&gt;
Maybe you need to post some desired output,it will clarify your question.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Tue, 07 Jun 2011 07:45:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-Sql-Question-on-Rolling-Dates-with-Conditions/m-p/72879#M21129</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-06-07T07:45:54Z</dc:date>
    </item>
    <item>
      <title>Re: Proc Sql Question on Rolling Dates with Conditions</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-Sql-Question-on-Rolling-Dates-with-Conditions/m-p/72880#M21130</link>
      <description>Hi Ksharp,&lt;BR /&gt;
&lt;BR /&gt;
For example, the same dataset, but consider obs 12 and 13. &lt;BR /&gt;
&lt;BR /&gt;
if I use the following code to produce the flag for same logic but without considering different id. Since obs 12 and 13 has the same date, so the code produce the flag=1 also for obs 13, while the flag really should be 0. The reason is because the code did not provide the time difference. &lt;BR /&gt;
&lt;BR /&gt;
Your help is high appreciated.&lt;BR /&gt;
&lt;BR /&gt;
proc sql ;&lt;BR /&gt;
 create table want (drop=_amount count_test)  as  &lt;BR /&gt;
  select  distinct a.*,&lt;BR /&gt;
/*                  count(distinct b.id) as count_id,*/&lt;BR /&gt;
                  count( b.test) as count_test,&lt;BR /&gt;
                  sum(b.amount) as _amount,&lt;BR /&gt;
&lt;BR /&gt;
      case when calculated _amount gt 4000&lt;BR /&gt;
/*                and calculated count_id ge 2 and */&lt;BR /&gt;
                and calculated count_test gt 1&lt;BR /&gt;
           then 1 else 0 end as flag&lt;BR /&gt;
&lt;BR /&gt;
   from temp as a ,temp as b&lt;BR /&gt;
    where a.account = b.account and b.date between a.date-2 and a.date&lt;BR /&gt;
/*          or (b.date=a.date and b.time le a.time)*/&lt;BR /&gt;
     group by a.obs&lt;BR /&gt;
      ;&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data temp;&lt;BR /&gt;
input obs account $ date : mmddyy10. time : time9. test id amount ;&lt;BR /&gt;
format date mmddyy10. time time9.;&lt;BR /&gt;
datalines;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
1 387000 6/10/2010 12:00:00 1 13.15 2000 1&lt;BR /&gt;
2 387000 6/9/2010 12:00:00 1 13.15 3000 1&lt;BR /&gt;
3 387000 6/8/2010 12:00:00 1 13.14 2000 0&lt;BR /&gt;
4 387000 6/6/2010 11:00:00 1 13.15 2000 1&lt;BR /&gt;
5 387000 6/5/2010 10:00:00 1 13.14 2000 0&lt;BR /&gt;
6 387000 6/4/2010 9:00:00 1 13.13 1000 0&lt;BR /&gt;
7 387000 6/4/2010 8:00:00 1 13.13 500 0&lt;BR /&gt;
8 386000 5/9/2010 10:00:00 1 12.12 5000 0&lt;BR /&gt;
9 386000 5/2/2010 9:00:00 1 12.14 2000 0&lt;BR /&gt;
10 386000 5/1/2010 8:00:00 1 12.13 2000 0&lt;BR /&gt;
11 385000 3/3/2010 10:00:00 1 11.13 5000 1&lt;BR /&gt;
12 385000 3/1/2010 9:00:00 1 11.12 3000 1&lt;BR /&gt;
13 385000 3/1/2010 8:00:00 1 11.11 3000 0&lt;BR /&gt;
;&lt;BR /&gt;
run;</description>
      <pubDate>Tue, 07 Jun 2011 09:27:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-Sql-Question-on-Rolling-Dates-with-Conditions/m-p/72880#M21130</guid>
      <dc:creator>NIUNIU</dc:creator>
      <dc:date>2011-06-07T09:27:57Z</dc:date>
    </item>
    <item>
      <title>Re: Proc Sql Question on Rolling Dates with Conditions</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-Sql-Question-on-Rolling-Dates-with-Conditions/m-p/72881#M21131</link>
      <description>Hi.&lt;BR /&gt;
That would not be a problem. Because I used &lt;BR /&gt;
[pre]&lt;BR /&gt;
from temp as a ,temp as b&lt;BR /&gt;
where a.account = b.account and b.date between a.date-2 and a.date&lt;BR /&gt;
/* or (b.date=a.date and b.time le a.time)*/&lt;BR /&gt;
&lt;B&gt;group by a.obs&lt;/B&gt;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
That obs is unique , so The problem you mentioned would not be appeared.&lt;BR /&gt;
You can test it by yourself.&lt;BR /&gt;
If you have other problems ,I will be here tomorrow.&lt;BR /&gt;
I have to leave now.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Tue, 07 Jun 2011 09:40:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-Sql-Question-on-Rolling-Dates-with-Conditions/m-p/72881#M21131</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-06-07T09:40:30Z</dc:date>
    </item>
    <item>
      <title>Re: Proc Sql Question on Rolling Dates with Conditions</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-Sql-Question-on-Rolling-Dates-with-Conditions/m-p/72882#M21132</link>
      <description>I know it sounds weird. But I tested many times, the issue still cannot resolve when two consecutive date are same, but with different time. The code won't tell the difference. &lt;BR /&gt;
&lt;BR /&gt;
If I use this logic to test:&lt;BR /&gt;
"There are more than 1 test within 2 days from the same account and the total amount by these tests is above 4000 dollars. If so, the flag is 1, else the flag is 0".&lt;BR /&gt;
&lt;BR /&gt;
proc sql ;&lt;BR /&gt;
create table want (drop=_amount count_test) as&lt;BR /&gt;
select distinct a.*,&lt;BR /&gt;
count( b.test) as count_test,&lt;BR /&gt;
sum(b.amount) as _amount,&lt;BR /&gt;
&lt;BR /&gt;
case when calculated _amount gt 4000&lt;BR /&gt;
and calculated count_test gt 1&lt;BR /&gt;
then 1 else 0 end as flag&lt;BR /&gt;
&lt;BR /&gt;
from temp as a ,temp as b&lt;BR /&gt;
where a.account = b.account and b.date between a.date-2 and a.date&lt;BR /&gt;
group by a.obs&lt;BR /&gt;
;&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
The code will give Obs 13 flag as 1, instead it should be 0. Since there is no date earlier than obs 13 in account 38500. I think the problem caused by this issue is because of the code cannot different the time.&lt;BR /&gt;
&lt;BR /&gt;
Thank you very much!</description>
      <pubDate>Tue, 07 Jun 2011 14:31:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-Sql-Question-on-Rolling-Dates-with-Conditions/m-p/72882#M21132</guid>
      <dc:creator>NIUNIU</dc:creator>
      <dc:date>2011-06-07T14:31:11Z</dc:date>
    </item>
    <item>
      <title>Re: Proc Sql Question on Rolling Dates with Conditions</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-Sql-Question-on-Rolling-Dates-with-Conditions/m-p/72883#M21133</link>
      <description>Oh.I understand what you mean.&lt;BR /&gt;
Yes.My code does not care about time.So the last obs will also be 1.&lt;BR /&gt;
If you want to follow this logic,you can use datetime format to distinct date and time exactly.&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data temp;&lt;BR /&gt;
input obs account $ date : mmddyy10. time : time9. test id amount ;&lt;BR /&gt;
format date mmddyy10. time time9.;&lt;BR /&gt;
datetime= dhms(date,0,0,time);&lt;BR /&gt;
format datetime datetime.;&lt;BR /&gt;
datalines;&lt;BR /&gt;
1 387000 6/10/2010 12:00:00 1 13.15 2000&lt;BR /&gt;
2 387000 6/9/2010 12:00:00 1 13.15 3000&lt;BR /&gt;
3 387000 6/8/2010 12:00:00 1 13.14 2000&lt;BR /&gt;
4 387000 6/6/2010 11:00:00 1 13.15 2000&lt;BR /&gt;
5 387000 6/5/2010 10:00:00 1 13.14 2000&lt;BR /&gt;
6 387000 6/4/2010 9:00:00 1 13.13 1000&lt;BR /&gt;
7 387000 6/4/2010 8:00:00 1 13.13 500&lt;BR /&gt;
8 386000 5/9/2010 10:00:00 1 12.12 5000&lt;BR /&gt;
9 386000 5/2/2010 9:00:00 1 12.14 2000&lt;BR /&gt;
10 386000 5/1/2010 8:00:00 1 12.13 2000&lt;BR /&gt;
11 385000 3/3/2010 10:00:00 1 11.13 5000&lt;BR /&gt;
12 385000 3/1/2010 9:00:00 1 11.12 3000&lt;BR /&gt;
13 385000 3/1/2010 8:00:00 1 11.11 3000&lt;BR /&gt;
;&lt;BR /&gt;
run;  &lt;BR /&gt;
proc sql ;&lt;BR /&gt;
 create table want(drop=_amount  count_test) as&lt;BR /&gt;
  select  distinct a.*,count( b.test) as count_test,sum(b.amount) as _amount,&lt;BR /&gt;
      case when calculated _amount gt 4000 and &lt;BR /&gt;
                calculated count_test gt 1 then 1 else 0 end as flag&lt;BR /&gt;
   from temp as a ,temp as b&lt;BR /&gt;
    where a.account = b.account and b.datetime between a.datetime-172800 and a.datetime&lt;BR /&gt;
    /*172800 is the number of seconds for two days.*/&lt;BR /&gt;
     group by a.obs&lt;BR /&gt;
      ;&lt;BR /&gt;
quit;&lt;BR /&gt;
proc sort data=want;&lt;BR /&gt;
 by descending date descending time;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Wed, 08 Jun 2011 02:11:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-Sql-Question-on-Rolling-Dates-with-Conditions/m-p/72883#M21133</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-06-08T02:11:47Z</dc:date>
    </item>
    <item>
      <title>Re: Proc Sql Question on Rolling Dates with Conditions</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-Sql-Question-on-Rolling-Dates-with-Conditions/m-p/72884#M21134</link>
      <description>Hi Ksharp,&lt;BR /&gt;
&lt;BR /&gt;
You really rocks. The code works great for me. However, I think I did not really master how to use Cartesian Product of sql. I really appreciate if you could help on how to resolve the following issue. The difficult part for me is how to select the at least three grade C and group them together. Do you have any good document to recommend to read on Cartesian Product of sql. Thank you so much!&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
The logic is "in the same account, the id is unique id  and has at least 3 grade C within 2 days, then the flag is 1, else is 0. " For example, below data only obs 4 and 11 meets the requirement. &lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data temp;&lt;BR /&gt;
input obs account $ date : mmddyy10. time : time9. test id amount grade $ flag ;&lt;BR /&gt;
format date mmddyy10. time time9.;&lt;BR /&gt;
datetime= dhms(date,0,0,time);&lt;BR /&gt;
format datetime datetime.;&lt;BR /&gt;
datalines;&lt;BR /&gt;
1 387000 6/10/2010 12:00:00 1 13.15 2000 A 0&lt;BR /&gt;
2 387000 6/9/2010 12:00:00 1 13.15 3000 A 0&lt;BR /&gt;
3 387000 6/8/2010 12:00:00 1 13.14 2000 B 0&lt;BR /&gt;
4 387000 6/6/2010 11:00:00 1 13.15 2000 C 1&lt;BR /&gt;
5 387000 6/5/2010 10:00:00 1 13.14 2000 C 0&lt;BR /&gt;
6 387000 6/4/2010 9:00:00 1 13.13 1000 B 0&lt;BR /&gt;
7 387000 6/4/2010 8:00:00 1 13.13 500 C 0&lt;BR /&gt;
8 386000 5/9/2010 10:00:00 1 12.12 5000 C 0&lt;BR /&gt;
9 386000 5/2/2010 9:00:00 1 12.14 2000 A 0&lt;BR /&gt;
10 386000 5/1/2010 8:00:00 1 12.13 2000 B 0&lt;BR /&gt;
11 385000 3/3/2010 10:00:00 1 11.13 5000 C 1&lt;BR /&gt;
12 385000 3/1/2010 9:00:00 1 11.12 3000 C 0&lt;BR /&gt;
13 385000 3/1/2010 8:00:00 1 11.11 3000 C 0&lt;BR /&gt;
;&lt;BR /&gt;
run;</description>
      <pubDate>Wed, 08 Jun 2011 04:33:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-Sql-Question-on-Rolling-Dates-with-Conditions/m-p/72884#M21134</guid>
      <dc:creator>NIUNIU</dc:creator>
      <dc:date>2011-06-08T04:33:35Z</dc:date>
    </item>
    <item>
      <title>Re: Proc Sql Question on Rolling Dates with Conditions</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-Sql-Question-on-Rolling-Dates-with-Conditions/m-p/72885#M21135</link>
      <description>What does "the id is unique id" mean?&lt;BR /&gt;
And there is a problem. As your logic ,there will not be any obs matched your codition.&lt;BR /&gt;
For example:&lt;BR /&gt;
Obs 4 's two days range is " 6/6/2010 11:00:00 "  to "6/4/2010 11:00:00  " ,so Obs 6 and 7&lt;BR /&gt;
will not be matched with Obs 4 ,because their datetime is out of range.&lt;BR /&gt;
But you can change 172800 to meet what you want, It is up to you.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
&lt;BR /&gt;
data temp;&lt;BR /&gt;
input obs account $ date : mmddyy10. time : time9. test id amount grade $ ;&lt;BR /&gt;
format date mmddyy10. time time9.;&lt;BR /&gt;
datetime= dhms(date,0,0,time);&lt;BR /&gt;
format datetime datetime.;&lt;BR /&gt;
datalines;&lt;BR /&gt;
1 387000 6/10/2010 12:00:00 1 13.15 2000 A&lt;BR /&gt;
2 387000 6/9/2010 12:00:00 1 13.15 3000 A&lt;BR /&gt;
3 387000 6/8/2010 12:00:00 1 13.14 2000 B&lt;BR /&gt;
4 387000 6/6/2010 11:00:00 1 13.15 2000 C&lt;BR /&gt;
5 387000 6/5/2010 10:00:00 1 13.14 2000 C&lt;BR /&gt;
6 387000 6/4/2010 9:00:00 1 13.13 1000 B&lt;BR /&gt;
7 387000 6/4/2010 8:00:00 1 13.13 500 C&lt;BR /&gt;
8 386000 5/9/2010 10:00:00 1 12.12 5000 C&lt;BR /&gt;
9 386000 5/2/2010 9:00:00 1 12.14 2000 A&lt;BR /&gt;
10 386000 5/1/2010 8:00:00 1 12.13 2000 B&lt;BR /&gt;
11 385000 3/3/2010 10:00:00 1 11.13 5000 C&lt;BR /&gt;
12 385000 3/1/2010 9:00:00 1 11.12 3000 C&lt;BR /&gt;
13 385000 3/1/2010 8:00:00 1 11.11 3000 C&lt;BR /&gt;
; &lt;BR /&gt;
run; &lt;BR /&gt;
proc sql ;&lt;BR /&gt;
 create table want(drop=sum_c) as&lt;BR /&gt;
  select  distinct a.*,sum(case when b.grade eq 'C' then 1 else 0 end) as sum_c,&lt;BR /&gt;
      case when calculated sum_c ge 3 then 1 else 0 end as flag&lt;BR /&gt;
   from temp as a ,temp as b&lt;BR /&gt;
    where a.account = b.account and b.datetime between a.datetime-172800 and a.datetime&lt;BR /&gt;
    /*172800 is the number of seconds for two days.*/&lt;BR /&gt;
     group by a.obs&lt;BR /&gt;
      ;&lt;BR /&gt;
quit;&lt;BR /&gt;
proc sort data=want;&lt;BR /&gt;
 by descending date descending time;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Wed, 08 Jun 2011 07:02:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-Sql-Question-on-Rolling-Dates-with-Conditions/m-p/72885#M21135</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-06-08T07:02:53Z</dc:date>
    </item>
    <item>
      <title>Re: Proc Sql Question on Rolling Dates with Conditions</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-Sql-Question-on-Rolling-Dates-with-Conditions/m-p/72886#M21136</link>
      <description>Or Maybe you can start from the beginning of every day .&lt;BR /&gt;
[pre]&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data temp;&lt;BR /&gt;
input obs account $ date : mmddyy10. time : time9. test id amount grade $ ;&lt;BR /&gt;
format date mmddyy10. time time9.;&lt;BR /&gt;
datetime= &lt;B&gt;dhms(date,0,0,0);&lt;/B&gt;&lt;BR /&gt;
format datetime datetime.;&lt;BR /&gt;
datalines;&lt;BR /&gt;
1 387000 6/10/2010 12:00:00 1 13.15 2000 A&lt;BR /&gt;
2 387000 6/9/2010 12:00:00 1 13.15 3000 A&lt;BR /&gt;
3 387000 6/8/2010 12:00:00 1 13.14 2000 B&lt;BR /&gt;
4 387000 6/6/2010 11:00:00 1 13.15 2000 C&lt;BR /&gt;
5 387000 6/5/2010 10:00:00 1 13.14 2000 C&lt;BR /&gt;
6 387000 6/4/2010 9:00:00 1 13.13 1000 B&lt;BR /&gt;
7 387000 6/4/2010 8:00:00 1 13.13 500 C&lt;BR /&gt;
8 386000 5/9/2010 10:00:00 1 12.12 5000 C&lt;BR /&gt;
9 386000 5/2/2010 9:00:00 1 12.14 2000 A&lt;BR /&gt;
10 386000 5/1/2010 8:00:00 1 12.13 2000 B&lt;BR /&gt;
11 385000 3/3/2010 10:00:00 1 11.13 5000 C&lt;BR /&gt;
12 385000 3/1/2010 9:00:00 1 11.12 3000 C&lt;BR /&gt;
13 385000 3/1/2010 8:00:00 1 11.11 3000 C&lt;BR /&gt;
; &lt;BR /&gt;
run; &lt;BR /&gt;
proc sql ;&lt;BR /&gt;
 create table want(drop=sum_c) as&lt;BR /&gt;
  select  distinct a.*,sum(case when b.grade eq 'C' then 1 else 0 end) as sum_c,&lt;BR /&gt;
      case when calculated sum_c ge 3 then 1 else 0 end as flag&lt;BR /&gt;
   from temp as a ,temp as b&lt;BR /&gt;
    where a.account = b.account and b.datetime between a.datetime-172800 and a.datetime&lt;BR /&gt;
    /*172800 is the number of seconds for two days.*/&lt;BR /&gt;
     group by a.obs&lt;BR /&gt;
      ;&lt;BR /&gt;
quit;&lt;BR /&gt;
proc sort data=want;&lt;BR /&gt;
 by descending date descending time;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Wed, 08 Jun 2011 07:09:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-Sql-Question-on-Rolling-Dates-with-Conditions/m-p/72886#M21136</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-06-08T07:09:48Z</dc:date>
    </item>
    <item>
      <title>Re: Proc Sql Question on Rolling Dates with Conditions</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-Sql-Question-on-Rolling-Dates-with-Conditions/m-p/72887#M21137</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Ksharp,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you very much for all the help. However, these days I am still struggling with how to code the unique id part. See if I can explain more clearly.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The general logic is "The id is first time appears within -30 days and -7 days from the same account. If so, the flag is 1, else the flag is 0". I want to produce the last column flag.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;TABLE border="0" cellpadding="0" cellspacing="0" style="margin-left: 3.9pt;"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD style="padding: 0pt 5.4pt;" valign="top" width="67"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;obs&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD style="padding: 0pt 5.4pt;" valign="top" width="67"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;account&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD style="padding: 0pt 5.4pt;" valign="top" width="67"&gt;&lt;/TD&gt;&lt;TD style="padding: 0pt 5.4pt;" valign="top" width="86"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;date&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD style="padding: 0pt 5.4pt;" valign="top" width="67"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;test&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD style="padding: 0pt 5.4pt;" valign="top" width="60"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;id&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD style="padding: 0pt 5.4pt;" valign="top" width="67"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;flag&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 0pt 5.4pt;" valign="top" width="67"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;1&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD style="padding: 0pt 5.4pt;" valign="top" width="67"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;384000&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD style="padding: 0pt 5.4pt;" valign="top" width="67"&gt;&lt;/TD&gt;&lt;TD style="padding: 0pt 5.4pt;" valign="top" width="86"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;6/30/2010&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD style="padding: 0pt 5.4pt;" valign="top" width="67"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;1&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD style="padding: 0pt 5.4pt;" valign="top" width="60"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;13.15&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD style="padding: 0pt 5.4pt;" valign="top" width="67"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;1&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 0pt 5.4pt;" valign="top" width="67"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;2&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD style="padding: 0pt 5.4pt;" valign="top" width="67"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;384000&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD style="padding: 0pt 5.4pt;" valign="top" width="67"&gt;&lt;/TD&gt;&lt;TD style="padding: 0pt 5.4pt;" valign="top" width="86"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;6/15/2010&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD style="padding: 0pt 5.4pt;" valign="top" width="67"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;1&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD style="padding: 0pt 5.4pt;" valign="top" width="60"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;13.14&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD style="padding: 0pt 5.4pt;" valign="top" width="67"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;1&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 0pt 5.4pt;" valign="top" width="67"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;3&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD style="padding: 0pt 5.4pt;" valign="top" width="67"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;384000&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD style="padding: 0pt 5.4pt;" valign="top" width="67"&gt;&lt;/TD&gt;&lt;TD style="padding: 0pt 5.4pt;" valign="top" width="86"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;6/10/2010&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD style="padding: 0pt 5.4pt;" valign="top" width="67"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;1&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD style="padding: 0pt 5.4pt;" valign="top" width="60"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;13.14&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD style="padding: 0pt 5.4pt;" valign="top" width="67"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;1&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 0pt 5.4pt;" valign="top" width="67"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;4&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD style="padding: 0pt 5.4pt;" valign="top" width="67"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;384000&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD style="padding: 0pt 5.4pt;" valign="top" width="67"&gt;&lt;/TD&gt;&lt;TD style="padding: 0pt 5.4pt;" valign="top" width="86"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;6/1/2010&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD style="padding: 0pt 5.4pt;" valign="top" width="67"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;1&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD style="padding: 0pt 5.4pt;" valign="top" width="60"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;13.13&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD style="padding: 0pt 5.4pt;" valign="top" width="67"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;0&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="background-color: yellow; padding: 0pt 5.4pt;" valign="top" width="67"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;5&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD style="background-color: yellow; padding: 0pt 5.4pt;" valign="top" width="67"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;386000&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD style="background-color: yellow; padding: 0pt 5.4pt;" valign="top" width="67"&gt;&lt;/TD&gt;&lt;TD style="background-color: yellow; padding: 0pt 5.4pt;" valign="top" width="86"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;5/30/2010&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD style="background-color: yellow; padding: 0pt 5.4pt;" valign="top" width="67"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;1&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD style="background-color: yellow; padding: 0pt 5.4pt;" valign="top" width="60"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;12.12&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD style="background-color: yellow; padding: 0pt 5.4pt;" valign="top" width="67"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;0&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="background-color: yellow; padding: 0pt 5.4pt;" valign="top" width="67"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;6&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD style="background-color: yellow; padding: 0pt 5.4pt;" valign="top" width="67"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;386000&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD style="background-color: yellow; padding: 0pt 5.4pt;" valign="top" width="67"&gt;&lt;/TD&gt;&lt;TD style="background-color: yellow; padding: 0pt 5.4pt;" valign="top" width="86"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;5/15/2010&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD style="background-color: yellow; padding: 0pt 5.4pt;" valign="top" width="67"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;1&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD style="background-color: yellow; padding: 0pt 5.4pt;" valign="top" width="60"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;12.12&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD style="background-color: yellow; padding: 0pt 5.4pt;" valign="top" width="67"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;1&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="background-color: yellow; padding: 0pt 5.4pt;" valign="top" width="67"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;7&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD style="background-color: yellow; padding: 0pt 5.4pt;" valign="top" width="67"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;386000&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD style="background-color: yellow; padding: 0pt 5.4pt;" valign="top" width="67"&gt;&lt;/TD&gt;&lt;TD style="background-color: yellow; padding: 0pt 5.4pt;" valign="top" width="86"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;4/25/2010&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD style="background-color: yellow; padding: 0pt 5.4pt;" valign="top" width="67"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;1&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD style="background-color: yellow; padding: 0pt 5.4pt;" valign="top" width="60"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;12.13&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD style="background-color: yellow; padding: 0pt 5.4pt;" valign="top" width="67"&gt;&lt;P align="center"&gt;&lt;SPAN style="color: black; font-size: 10pt;"&gt;0&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For example, for obs 1, the id is 13.15, between 30 and 7 days, there is no same id is 13.15. So the flag is 1.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;however, for obs 2, the id is 13.14, even though there is another 13.14 appears in obs 3, but the flag is still 1, since between 06/15 and 06/10, there is only 5 days difference, not more than 7 days difference. We will ignore the first 7 days observation. Therefore, this obs 3 will not count. And again, after we ignore the first 7 days, between -7 and -30 days, there is no same 13.14 id, so the flag will be 1.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Obs 4 will be 0, since this is no history to compare.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Obs 5 will be 0, since there is another id 12.12 in obs 6, and the date is between 7 days and 30 days difference.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am struggling how to code to ignore the first 7 days, and only count between 7 days and 30 days range.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I really appreciate if you could help me how to resolve this puzzle. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you very much!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 30 Jun 2011 04:04:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-Sql-Question-on-Rolling-Dates-with-Conditions/m-p/72887#M21137</guid>
      <dc:creator>NIUNIU</dc:creator>
      <dc:date>2011-06-30T04:04:57Z</dc:date>
    </item>
    <item>
      <title>Re: Proc Sql Question on Rolling Dates with Conditions</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-Sql-Question-on-Rolling-Dates-with-Conditions/m-p/72888#M21138</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;OK.Cartesian Product of DataStep is more powerful than Cartesian Product of Sql. So I used it for your a little complicated problem.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;data temp;
input account $ date : mmddyy10. test id;
format date mmddyy10.;
cards;
384000 6/30/2010 1 13.15
384000 6/15/2010 1 13.14&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
384000 6/10/2010 1 13.14&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
384000 6/1/2010&amp;nbsp; 1 13.13
386000 5/30/2010 1 12.12
386000 5/15/2010 1 12.12
386000 4/25/2010 1 12.13
;
run;

data want;
 set temp;
 found=0; matched=0;flag=0;
 do i=1 to _nobs;
&amp;nbsp; set temp(drop=test rename=(account=_account date=_date id=_id))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; nobs=_nobs point=i;
&amp;nbsp; if account=_account and _date ge date-30 and _date le date-7
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; and id=_id&amp;nbsp; then&amp;nbsp; found=1;
&amp;nbsp; if account=_account and _date ge date-30 and _date le date-7 then matched=1;
&amp;nbsp; if i=_nobs then do;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if not found and matched then flag=1;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; output;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;
 end;
 drop found matched _:;
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;Ksharp&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 30 Jun 2011 06:33:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-Sql-Question-on-Rolling-Dates-with-Conditions/m-p/72888#M21138</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-06-30T06:33:19Z</dc:date>
    </item>
    <item>
      <title>Re: Proc Sql Question on Rolling Dates with Conditions</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-Sql-Question-on-Rolling-Dates-with-Conditions/m-p/72889#M21139</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Ksharp,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Sincerely appreciate all your help. This is really something. Definitely learned a lot from you.&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;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you very much!!! &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;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 30 Jun 2011 07:19:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-Sql-Question-on-Rolling-Dates-with-Conditions/m-p/72889#M21139</guid>
      <dc:creator>NIUNIU</dc:creator>
      <dc:date>2011-06-30T07:19:27Z</dc:date>
    </item>
    <item>
      <title>Re: Proc Sql Question on Rolling Dates with Conditions</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-Sql-Question-on-Rolling-Dates-with-Conditions/m-p/72890#M21140</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi NiuNiu&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Yes.&lt;/P&gt;&lt;P&gt;I admit that Cartesian Product of sql is a little faster than data step.&lt;/P&gt;&lt;P&gt;But data step 's Cartesian Product is more powerful, which can do more than sql.&lt;/P&gt;&lt;P&gt;Especially for some complex situation, I prefer to use data step.&lt;/P&gt;&lt;P&gt;SQL is designed based on Cartesian Product, so maybe it is optimized by some skill.&lt;/P&gt;&lt;P&gt;And data step need to open dataset each time of iteration, so it is maybe the cause to waste more time than sql.&lt;/P&gt;&lt;P&gt;So for some simple problem, I recommend to use sql, But for some complicated problem which sql can not accomplish. The only choice is data step &lt;img id="smileysad" class="emoticon emoticon-smileysad" src="https://communities.sas.com/i/smilies/16x16_smiley-sad.png" alt="Smiley Sad" title="Smiley Sad" /&gt; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;Ksharp&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 04 Jul 2011 06:31:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-Sql-Question-on-Rolling-Dates-with-Conditions/m-p/72890#M21140</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-07-04T06:31:17Z</dc:date>
    </item>
    <item>
      <title>Re: Proc Sql Question on Rolling Dates with Conditions</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-Sql-Question-on-Rolling-Dates-with-Conditions/m-p/72891#M21141</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi. I recode it by using proc sql.&lt;/P&gt;&lt;P&gt;But not know how fast will it be .&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;data temp;
input account $ date : mmddyy10. test id;
format date mmddyy10.;
cards;
384000 6/30/2010 1 13.15
384000 6/15/2010 1 13.14&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
384000 6/10/2010 1 13.14&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
384000 6/1/2010&amp;nbsp; 1 13.13
386000 5/30/2010 1 12.12
386000 5/15/2010 1 12.12
386000 4/25/2010 1 12.13
;
run;
proc sql;
 create table op as
&amp;nbsp; select a.*,b.date as _date ,b.id as _id ,case
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; when a.id=b.id then 1
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; when b.id is missing then 1
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else 0
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end as count
&amp;nbsp;&amp;nbsp; from temp as a left join temp as b
&amp;nbsp;&amp;nbsp;&amp;nbsp; on a.account=b.account and b.date between a.date-30 and a.date-7;

 create table flag as
&amp;nbsp; select account,date,not(sum(count)) as flag
&amp;nbsp;&amp;nbsp; from op
&amp;nbsp;&amp;nbsp;&amp;nbsp; group by account,date;


 create table want as
&amp;nbsp; select a.*,flag
&amp;nbsp;&amp;nbsp; from temp as a left join flag as b
&amp;nbsp;&amp;nbsp;&amp;nbsp; on a.account=b.account and a.date=b.date
&amp;nbsp;&amp;nbsp;&amp;nbsp; ;
quit;

&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Ksharp&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 05 Jul 2011 02:37:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-Sql-Question-on-Rolling-Dates-with-Conditions/m-p/72891#M21141</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-07-05T02:37:15Z</dc:date>
    </item>
  </channel>
</rss>

