<?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: how to transfer the stata code to sas code: a loop to count the no. of obs under conditions in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-transfer-the-stata-code-to-sas-code-a-loop-to-count-the/m-p/485343#M126082</link>
    <description>Hello,&lt;BR /&gt;&lt;BR /&gt;May I ask if I want to add the more complicated condition on for the count if, how should I modify the code? I tried the following but failed.&lt;BR /&gt;proc sql;&lt;BR /&gt;create table table1 as&lt;BR /&gt;select unique a.*, count(past1.date_sign) as la1_no_hand_la1&lt;BR /&gt;from lal&lt;BR /&gt;left join&lt;BR /&gt;lal past1&lt;BR /&gt;on (lal.listing_1=past1.listing_1 | lal.listing_1=past1_listing_2 | lal.listing_1=past1_listing_3 | lal.listing_1=past1_listing_4)&lt;BR /&gt;&amp;amp; ((lal.date_sign&amp;lt; past1.date_sign &amp;lt; lal.date_exp &amp;amp; past1.status=$EX) | (lal.date_sign&amp;lt; past1.date_sign &amp;lt; lal.date_sold &amp;amp; past1.status=$VE)|(lal.date_sign&amp;lt; past1.date_exp &amp;lt; lal.date_exp &amp;amp; past1.status=$EX &amp;amp; lal.status=$EX)|(lal.date_sign&amp;lt; past1.date_exp &amp;lt; lal.date_sold &amp;amp; past1.status=$EX &amp;amp; lal.status=$VE) | (lal.date_sign&amp;lt; past1.date_sold &amp;lt; lal.date_exp &amp;amp; past1.status=$VE &amp;amp; lal.status=$EX)|(lal.date_sign&amp;lt; past1.date_sold &amp;lt; lal.date_sold &amp;amp; past1.status=$VE &amp;amp; lal.status=$VE) )&lt;BR /&gt;group by 1&lt;BR /&gt;order by 1 ;&lt;BR /&gt;&lt;BR /&gt;Could you give me some suggestion?&lt;BR /&gt;&lt;BR /&gt;Thank you so much&lt;BR /&gt;&lt;BR /&gt;Freda</description>
    <pubDate>Thu, 09 Aug 2018 05:44:38 GMT</pubDate>
    <dc:creator>freda</dc:creator>
    <dc:date>2018-08-09T05:44:38Z</dc:date>
    <item>
      <title>how to transfer the stata code to sas code: a loop to count the no. of obs under conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-transfer-the-stata-code-to-sas-code-a-loop-to-count-the/m-p/483653#M125448</link>
      <description>&lt;P&gt;Good evening,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is my first time to use SAS and also my first time to ask a question in this forum. Thank you all first for your patient and help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the following data (sample):&lt;/P&gt;&lt;P&gt;obs&amp;nbsp; &amp;nbsp; id&amp;nbsp; &amp;nbsp; &amp;nbsp;listing_1&amp;nbsp; listing_2&amp;nbsp; selling_1 selling_2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;status&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;date_sign&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; la1_no_la1_5&amp;nbsp; &amp;nbsp; &amp;nbsp;la1_no_la2_5&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;C&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; D&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; sold&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2012-01-23 00:00:12&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 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; 2&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;C&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;D&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;sold&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2007-05-01&amp;nbsp; 00:01:15&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 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; 0&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;E&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; F&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;C&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; sold&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2013-03-05&amp;nbsp; 08:21:05&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 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; 0&lt;/P&gt;&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; A&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; expired&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2011-01-15 05:21:00&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 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;0&lt;/P&gt;&lt;P&gt;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;C&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;F&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;sold&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2008-05-11 00:00:00&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;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;0&lt;/P&gt;&lt;P&gt;6&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 6&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;E&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;D&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; sold&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2011-07-15 00:23:15&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 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; 0&lt;/P&gt;&lt;P&gt;7&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 7&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;C&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; A&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;sold&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2012-11-26 00:15:00&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;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;1&lt;/P&gt;&lt;P&gt;8&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 8&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;C&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;C&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; F&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;sold&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2007-12-15&amp;nbsp; 00:17:25&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 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; 0&lt;/P&gt;&lt;P&gt;9&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 9&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;F&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; C&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; sold&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2014-12-01 00:00:00&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 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;1&lt;/P&gt;&lt;P&gt;10&amp;nbsp; &amp;nbsp; &amp;nbsp; 10&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; D&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;A&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; expired&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2011-12-14 00:15:25&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 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; 0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The last two columns are the results or new variables I want to create.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have 10 observations, each observation is a transaction. For each transaction, at most two listing agents (listing_1 and listing_2) and two selling agents (selling_1 and selling_2) involved. Each id, no matter get sold or go expired, has a&amp;nbsp;date_sign, which is the date the contract signature date between the listing agents and the client. I want to create two new variables: la1_no_la1_5 and la1_no_la2_5 to record the first listing agent's past activities. la1_no_la1_5 represents the total number of transactions (both sold and expired) the first listing agent involved as a first listing agent in the past five years of the signature date.&amp;nbsp; For example, for obs1, la1_no_la1_5= 1 means the first listing agent of id=1 transaction involved in 1 transaction as a first listing&amp;nbsp;agent in the past 5 years of the signature date (&lt;SPAN&gt;2012-01-23 00:00:12&amp;nbsp;&lt;/SPAN&gt;).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The process behind I want to create a loop that for each id, the system finds its first listing agent, A in the above example, then look up all the id's (from obs1 to obs10) first listing agents and find the first listing agent = A, then compare the date_sign. if date_sign is within five years, +1 to la1_no_la1_5.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I used stata to successfully calculate the numbers, but because my full data is too large, almost 1.5 million transactions, stata takes too long to process the code.&lt;/P&gt;&lt;P&gt;Following are my stata code:&lt;/P&gt;&lt;P&gt;gen la1_no_la1_5 = .&lt;BR /&gt;local N = _N&lt;BR /&gt;forvalues i = 1(1)`N' {&lt;BR /&gt;count if listing_1[`i']==listing_1 &amp;amp; (date_sign[`i'] - date_sign)/365.25 &amp;lt;= 5 &amp;amp;(date_sign[`i'] - date_sign)/365.25 &amp;gt; 0&lt;BR /&gt;replace la1_no_la1_5 = r(N) in `i'&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anyone help me to translate the stata code to useful sas code or help me to write a new sas code to process the above calculation? I really appreciate.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you again&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Freda&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Aug 2018 02:54:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-transfer-the-stata-code-to-sas-code-a-loop-to-count-the/m-p/483653#M125448</guid>
      <dc:creator>freda</dc:creator>
      <dc:date>2018-08-03T02:54:03Z</dc:date>
    </item>
    <item>
      <title>Re: how to transfer the stata code to sas code: a loop to count the no. of obs under conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-transfer-the-stata-code-to-sas-code-a-loop-to-count-the/m-p/483665#M125453</link>
      <description>&lt;P&gt;When you say "&lt;SPAN&gt;within five years", does that mean looking back five years?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Aug 2018 04:39:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-transfer-the-stata-code-to-sas-code-a-loop-to-count-the/m-p/483665#M125453</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2018-08-03T04:39:03Z</dc:date>
    </item>
    <item>
      <title>Re: how to transfer the stata code to sas code: a loop to count the no. of obs under conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-transfer-the-stata-code-to-sas-code-a-loop-to-count-the/m-p/483666#M125454</link>
      <description>Yes, within last 5 years of the date_sign.</description>
      <pubDate>Fri, 03 Aug 2018 04:40:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-transfer-the-stata-code-to-sas-code-a-loop-to-count-the/m-p/483666#M125454</guid>
      <dc:creator>freda</dc:creator>
      <dc:date>2018-08-03T04:40:24Z</dc:date>
    </item>
    <item>
      <title>Re: how to transfer the stata code to sas code: a loop to count the no. of obs under conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-transfer-the-stata-code-to-sas-code-a-loop-to-count-the/m-p/483668#M125456</link>
      <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sql; 
select unique have.*, count(past1.date_sign) as la1_no_la1_5
from have
    left join
    have past1
    on  have.listing_1=past1.listing_1 
    and 0 &amp;lt; have.date_sign-past1.date_sign &amp;lt; 5*365.25
    group by 1  
    order by 1  ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;TABLE width="656"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="98"&gt;obs&lt;/TD&gt;
&lt;TD width="64"&gt;id&lt;/TD&gt;
&lt;TD width="64"&gt;listing_1&lt;/TD&gt;
&lt;TD width="64"&gt;listing_2&lt;/TD&gt;
&lt;TD width="64"&gt;selling_1&lt;/TD&gt;
&lt;TD width="64"&gt;selling_2&lt;/TD&gt;
&lt;TD width="64"&gt;status&lt;/TD&gt;
&lt;TD width="76"&gt;date_sign&lt;/TD&gt;
&lt;TD width="98"&gt;la1_no_la1_5&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="98"&gt;1&lt;/TD&gt;
&lt;TD width="64"&gt;1&lt;/TD&gt;
&lt;TD width="64"&gt;A&lt;/TD&gt;
&lt;TD width="64"&gt;B&lt;/TD&gt;
&lt;TD width="64"&gt;C&lt;/TD&gt;
&lt;TD width="64"&gt;D&lt;/TD&gt;
&lt;TD width="64"&gt;sold&lt;/TD&gt;
&lt;TD width="76"&gt;23-Jan-12&lt;/TD&gt;
&lt;TD width="98"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="98"&gt;2&lt;/TD&gt;
&lt;TD width="64"&gt;2&lt;/TD&gt;
&lt;TD width="64"&gt;C&lt;/TD&gt;
&lt;TD width="64"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="64"&gt;D&lt;/TD&gt;
&lt;TD width="64"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="64"&gt;sold&lt;/TD&gt;
&lt;TD width="76"&gt;1-May-07&lt;/TD&gt;
&lt;TD width="98"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="98"&gt;3&lt;/TD&gt;
&lt;TD width="64"&gt;3&lt;/TD&gt;
&lt;TD width="64"&gt;E&lt;/TD&gt;
&lt;TD width="64"&gt;F&lt;/TD&gt;
&lt;TD width="64"&gt;A&lt;/TD&gt;
&lt;TD width="64"&gt;C&lt;/TD&gt;
&lt;TD width="64"&gt;sold&lt;/TD&gt;
&lt;TD width="76"&gt;5-Mar-13&lt;/TD&gt;
&lt;TD width="98"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="98"&gt;4&lt;/TD&gt;
&lt;TD width="64"&gt;4&lt;/TD&gt;
&lt;TD width="64"&gt;B&lt;/TD&gt;
&lt;TD width="64"&gt;A&lt;/TD&gt;
&lt;TD width="64"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="64"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="64"&gt;expired&lt;/TD&gt;
&lt;TD width="76"&gt;15-Jan-11&lt;/TD&gt;
&lt;TD width="98"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="98"&gt;5&lt;/TD&gt;
&lt;TD width="64"&gt;5&lt;/TD&gt;
&lt;TD width="64"&gt;A&lt;/TD&gt;
&lt;TD width="64"&gt;C&lt;/TD&gt;
&lt;TD width="64"&gt;B&lt;/TD&gt;
&lt;TD width="64"&gt;F&lt;/TD&gt;
&lt;TD width="64"&gt;sold&lt;/TD&gt;
&lt;TD width="76"&gt;11-May-08&lt;/TD&gt;
&lt;TD width="98"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="98"&gt;6&lt;/TD&gt;
&lt;TD width="64"&gt;6&lt;/TD&gt;
&lt;TD width="64"&gt;E&lt;/TD&gt;
&lt;TD width="64"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="64"&gt;D&lt;/TD&gt;
&lt;TD width="64"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="64"&gt;sold&lt;/TD&gt;
&lt;TD width="76"&gt;15-Jul-11&lt;/TD&gt;
&lt;TD width="98"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="98"&gt;7&lt;/TD&gt;
&lt;TD width="64"&gt;7&lt;/TD&gt;
&lt;TD width="64"&gt;C&lt;/TD&gt;
&lt;TD width="64"&gt;A&lt;/TD&gt;
&lt;TD width="64"&gt;A&lt;/TD&gt;
&lt;TD width="64"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="64"&gt;sold&lt;/TD&gt;
&lt;TD width="76"&gt;26-Nov-12&lt;/TD&gt;
&lt;TD width="98"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="98"&gt;8&lt;/TD&gt;
&lt;TD width="64"&gt;8&lt;/TD&gt;
&lt;TD width="64"&gt;C&lt;/TD&gt;
&lt;TD width="64"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="64"&gt;C&lt;/TD&gt;
&lt;TD width="64"&gt;F&lt;/TD&gt;
&lt;TD width="64"&gt;sold&lt;/TD&gt;
&lt;TD width="76"&gt;15-Dec-07&lt;/TD&gt;
&lt;TD width="98"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="98"&gt;9&lt;/TD&gt;
&lt;TD width="64"&gt;9&lt;/TD&gt;
&lt;TD width="64"&gt;F&lt;/TD&gt;
&lt;TD width="64"&gt;B&lt;/TD&gt;
&lt;TD width="64"&gt;B&lt;/TD&gt;
&lt;TD width="64"&gt;C&lt;/TD&gt;
&lt;TD width="64"&gt;sold&lt;/TD&gt;
&lt;TD width="76"&gt;1-Dec-14&lt;/TD&gt;
&lt;TD width="98"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="98"&gt;10&lt;/TD&gt;
&lt;TD width="64"&gt;10&lt;/TD&gt;
&lt;TD width="64"&gt;D&lt;/TD&gt;
&lt;TD width="64"&gt;A&lt;/TD&gt;
&lt;TD width="64"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="64"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="64"&gt;expired&lt;/TD&gt;
&lt;TD width="76"&gt;14-Dec-11&lt;/TD&gt;
&lt;TD width="98"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Aug 2018 05:00:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-transfer-the-stata-code-to-sas-code-a-loop-to-count-the/m-p/483668#M125456</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-08-03T05:00:50Z</dc:date>
    </item>
    <item>
      <title>Re: how to transfer the stata code to sas code: a loop to count the no. of obs under conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-transfer-the-stata-code-to-sas-code-a-loop-to-count-the/m-p/483670#M125458</link>
      <description>Thank you so much. I copied your code, it generates the desired result in the output. However, it does not create new file by telling me the following error message:&lt;BR /&gt;NOTE: The query requires remerging summary statistics back with the original data.&lt;BR /&gt;Do you know how to solve this issue?&lt;BR /&gt;Thank you again.&lt;BR /&gt;&lt;BR /&gt;Freda&lt;BR /&gt;</description>
      <pubDate>Fri, 03 Aug 2018 05:13:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-transfer-the-stata-code-to-sas-code-a-loop-to-count-the/m-p/483670#M125458</guid>
      <dc:creator>freda</dc:creator>
      <dc:date>2018-08-03T05:13:40Z</dc:date>
    </item>
    <item>
      <title>Re: how to transfer the stata code to sas code: a loop to count the no. of obs under conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-transfer-the-stata-code-to-sas-code-a-loop-to-count-the/m-p/483672#M125460</link>
      <description>&lt;P&gt;&lt;EM&gt;It does not create new file&amp;nbsp;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Add &lt;FONT face="courier new,courier"&gt;create table&lt;/FONT&gt; at the start of the query&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Aug 2018 05:16:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-transfer-the-stata-code-to-sas-code-a-loop-to-count-the/m-p/483672#M125460</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-08-03T05:16:05Z</dc:date>
    </item>
    <item>
      <title>Re: how to transfer the stata code to sas code: a loop to count the no. of obs under conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-transfer-the-stata-code-to-sas-code-a-loop-to-count-the/m-p/483675#M125463</link>
      <description>Thank you so much. It works.</description>
      <pubDate>Fri, 03 Aug 2018 05:21:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-transfer-the-stata-code-to-sas-code-a-loop-to-count-the/m-p/483675#M125463</guid>
      <dc:creator>freda</dc:creator>
      <dc:date>2018-08-03T05:21:22Z</dc:date>
    </item>
    <item>
      <title>Re: how to transfer the stata code to sas code: a loop to count the no. of obs under conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-transfer-the-stata-code-to-sas-code-a-loop-to-count-the/m-p/485343#M126082</link>
      <description>Hello,&lt;BR /&gt;&lt;BR /&gt;May I ask if I want to add the more complicated condition on for the count if, how should I modify the code? I tried the following but failed.&lt;BR /&gt;proc sql;&lt;BR /&gt;create table table1 as&lt;BR /&gt;select unique a.*, count(past1.date_sign) as la1_no_hand_la1&lt;BR /&gt;from lal&lt;BR /&gt;left join&lt;BR /&gt;lal past1&lt;BR /&gt;on (lal.listing_1=past1.listing_1 | lal.listing_1=past1_listing_2 | lal.listing_1=past1_listing_3 | lal.listing_1=past1_listing_4)&lt;BR /&gt;&amp;amp; ((lal.date_sign&amp;lt; past1.date_sign &amp;lt; lal.date_exp &amp;amp; past1.status=$EX) | (lal.date_sign&amp;lt; past1.date_sign &amp;lt; lal.date_sold &amp;amp; past1.status=$VE)|(lal.date_sign&amp;lt; past1.date_exp &amp;lt; lal.date_exp &amp;amp; past1.status=$EX &amp;amp; lal.status=$EX)|(lal.date_sign&amp;lt; past1.date_exp &amp;lt; lal.date_sold &amp;amp; past1.status=$EX &amp;amp; lal.status=$VE) | (lal.date_sign&amp;lt; past1.date_sold &amp;lt; lal.date_exp &amp;amp; past1.status=$VE &amp;amp; lal.status=$EX)|(lal.date_sign&amp;lt; past1.date_sold &amp;lt; lal.date_sold &amp;amp; past1.status=$VE &amp;amp; lal.status=$VE) )&lt;BR /&gt;group by 1&lt;BR /&gt;order by 1 ;&lt;BR /&gt;&lt;BR /&gt;Could you give me some suggestion?&lt;BR /&gt;&lt;BR /&gt;Thank you so much&lt;BR /&gt;&lt;BR /&gt;Freda</description>
      <pubDate>Thu, 09 Aug 2018 05:44:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-transfer-the-stata-code-to-sas-code-a-loop-to-count-the/m-p/485343#M126082</guid>
      <dc:creator>freda</dc:creator>
      <dc:date>2018-08-09T05:44:38Z</dc:date>
    </item>
    <item>
      <title>Re: how to transfer the stata code to sas code: a loop to count the no. of obs under conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-transfer-the-stata-code-to-sas-code-a-loop-to-count-the/m-p/485345#M126084</link>
      <description>&lt;P&gt;We have no clue what "more complicated" conditions you want.&lt;/P&gt;
&lt;P&gt;However this syntax is invalid.&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;past1.status=$EX&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;Do you mean&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;past1.status='$EX'&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Aug 2018 06:07:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-transfer-the-stata-code-to-sas-code-a-loop-to-count-the/m-p/485345#M126084</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-08-09T06:07:27Z</dc:date>
    </item>
    <item>
      <title>Re: how to transfer the stata code to sas code: a loop to count the no. of obs under conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-transfer-the-stata-code-to-sas-code-a-loop-to-count-the/m-p/485508#M126149</link>
      <description>&lt;P&gt;Yes, I want to count the number of transactions at hand. EX stands for expired and VE stands for sold.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Freda&lt;/P&gt;</description>
      <pubDate>Thu, 09 Aug 2018 15:10:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-transfer-the-stata-code-to-sas-code-a-loop-to-count-the/m-p/485508#M126149</guid>
      <dc:creator>freda</dc:creator>
      <dc:date>2018-08-09T15:10:45Z</dc:date>
    </item>
    <item>
      <title>Re: how to transfer the stata code to sas code: a loop to count the no. of obs under conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-transfer-the-stata-code-to-sas-code-a-loop-to-count-the/m-p/485595#M126176</link>
      <description>Hello,&lt;BR /&gt;&lt;BR /&gt;I modified the code as follows:&lt;BR /&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table table1 as&lt;BR /&gt;select unique la1.*, count(?) as la1_no_hand_la1&lt;BR /&gt;from la1&lt;BR /&gt;left join&lt;BR /&gt;la1 past1&lt;BR /&gt;on (la1.listing_1=past1.listing_1 and la1.date_sign&amp;lt; past1.date_sign &amp;lt; la1.date_exp and la1.status="$EX" )&lt;BR /&gt;or (la1.listing_1=past1.listing_1 and la1.date_sign&amp;lt; past1.date_sign &amp;lt; la1.date_sold and la1.status="$VE")&lt;BR /&gt;or (la1.listing_1=past1.listing_1 and la1.date_sign&amp;lt; past1.date_exp &amp;lt; la1.date_exp and past1.status="$EX" and la1.status="$EX")&lt;BR /&gt;or (la1.listing_1=past1.listing_1 and la1.date_sign&amp;lt; past1.date_exp &amp;lt; la1.date_sold and past1.status="$EX" and la1.status="$VE")&lt;BR /&gt;or (la1.listing_1=past1.listing_1 and la1.date_sign&amp;lt; past1.date_sold &amp;lt; la1.date_exp and past1.status="$VE" and la1.status="$EX")&lt;BR /&gt;or (la1.listing_1=past1.listing_1 and la1.date_sign&amp;lt; past1.date_sold &amp;lt; la1.date_sold and past1.status="$VE" and la1.status="$VE")&lt;BR /&gt;group by 1&lt;BR /&gt;order by 1 ;&lt;BR /&gt;&lt;BR /&gt;However, I don't know what I should put instead of the ? in the count(?) because I don't understand what does past.1 stands for. I tried the original code provided by you as count(past1.date_sign), but it does not work this time.&lt;BR /&gt;The result I want to have is the listing at hand during the contract period. For example, for id=1, the listing_1 A has its own contract period between date_sign and date_exp or date_sold if the listing has been sold. I want to count all the listings the first listing agent A involved.&lt;BR /&gt;&lt;BR /&gt;Thank you&lt;BR /&gt;&lt;BR /&gt;Freda&lt;BR /&gt;</description>
      <pubDate>Thu, 09 Aug 2018 21:01:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-transfer-the-stata-code-to-sas-code-a-loop-to-count-the/m-p/485595#M126176</guid>
      <dc:creator>freda</dc:creator>
      <dc:date>2018-08-09T21:01:14Z</dc:date>
    </item>
    <item>
      <title>Re: how to transfer the stata code to sas code: a loop to count the no. of obs under conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-transfer-the-stata-code-to-sas-code-a-loop-to-count-the/m-p/485599#M126180</link>
      <description>The result supposed to be:&lt;BR /&gt;id result&lt;BR /&gt;1 1&lt;BR /&gt;2 5&lt;BR /&gt;3 0&lt;BR /&gt;4 1&lt;BR /&gt;5 0&lt;BR /&gt;6 2&lt;BR /&gt;7 0&lt;BR /&gt;8 0&lt;BR /&gt;9 0&lt;BR /&gt;10 1&lt;BR /&gt;11 1&lt;BR /&gt;12 0&lt;BR /&gt;13 2&lt;BR /&gt;14 0&lt;BR /&gt;15 0&lt;BR /&gt;16 0&lt;BR /&gt;17 0&lt;BR /&gt;18 1&lt;BR /&gt;19 0&lt;BR /&gt;20 1&lt;BR /&gt;21 0&lt;BR /&gt;22 0&lt;BR /&gt;23 0&lt;BR /&gt;24 1&lt;BR /&gt;25 1&lt;BR /&gt;26 0&lt;BR /&gt;27 0&lt;BR /&gt;28 1&lt;BR /&gt;29 1&lt;BR /&gt;30 0</description>
      <pubDate>Thu, 09 Aug 2018 21:11:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-transfer-the-stata-code-to-sas-code-a-loop-to-count-the/m-p/485599#M126180</guid>
      <dc:creator>freda</dc:creator>
      <dc:date>2018-08-09T21:11:01Z</dc:date>
    </item>
    <item>
      <title>Re: how to transfer the stata code to sas code: a loop to count the no. of obs under conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-transfer-the-stata-code-to-sas-code-a-loop-to-count-the/m-p/485600#M126181</link>
      <description>&lt;P&gt;Here is the full data for the testing&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Aug 2018 21:12:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-transfer-the-stata-code-to-sas-code-a-loop-to-count-the/m-p/485600#M126181</guid>
      <dc:creator>freda</dc:creator>
      <dc:date>2018-08-09T21:12:00Z</dc:date>
    </item>
    <item>
      <title>Re: how to transfer the stata code to sas code: a loop to count the no. of obs under conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-transfer-the-stata-code-to-sas-code-a-loop-to-count-the/m-p/485652#M126198</link>
      <description>&lt;P&gt;&lt;EM&gt;I don't understand what does past.1 stands for.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;past1&lt;/STRONG&gt; is the name of a table.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;There is only one table, but you merge it *&lt;EM&gt;with itself&lt;/EM&gt;* on ID, and &lt;STRONG&gt;past1&lt;/STRONG&gt; only matches on past dates as per the match condition.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Aug 2018 04:17:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-transfer-the-stata-code-to-sas-code-a-loop-to-count-the/m-p/485652#M126198</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-08-10T04:17:12Z</dc:date>
    </item>
  </channel>
</rss>

