<?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: Keep consecutive observations only in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Keep-consecutive-observations-only/m-p/86678#M24761</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Maybe not the most efficient, but does the job :&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;proc sql;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;create table tickers as&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;select distinct A.ticker&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;from &lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sasforum.example as A inner join&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sasforum.example as B on A.ticker=B.ticker and &lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; intnx("QTR", A.date, 0) = intnx("QTR", B.date, 1);&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;create table conseq as&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;select * &lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;from sasforum.example&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;where ticker in (select ticker from tickers)&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;order by ticker, date;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;drop table tickers;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;quit;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;PG&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 26 May 2013 20:25:39 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2013-05-26T20:25:39Z</dc:date>
    <item>
      <title>Keep consecutive observations only</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Keep-consecutive-observations-only/m-p/86671#M24754</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I would like to keep stocks (ticker) that have at least 2 consecutive observations only. However, it could be the case that they have 1 observation in one quarter but 2 consecutive observations after 2 years. Or 2 consecutive observations at the beginning but single observations then after. In that case this stock should be kept (even the single observations). So, what I would like to do is delete the stocks that have only single quarter observations.&lt;/P&gt;&lt;P&gt;Any ideas?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 25 May 2013 20:27:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Keep-consecutive-observations-only/m-p/86671#M24754</guid>
      <dc:creator>Costasg</dc:creator>
      <dc:date>2013-05-25T20:27:38Z</dc:date>
    </item>
    <item>
      <title>Re: Keep consecutive observations only</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Keep-consecutive-observations-only/m-p/86672#M24755</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Is this going the direction you want?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data stocks;&lt;BR /&gt; input ticker $;&lt;BR /&gt; cards;&lt;BR /&gt;AA&lt;BR /&gt;AA&lt;BR /&gt;AB&lt;BR /&gt;AC&lt;BR /&gt;AD&lt;BR /&gt;AD&lt;BR /&gt;AE&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;data wanted_stocks;&lt;BR /&gt; set stocks;&lt;/P&gt;&lt;P&gt; if ticker = lag(ticker) then&lt;BR /&gt;&amp;nbsp; output;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt; create table unique_stocks as&lt;BR /&gt;&amp;nbsp; select distinct ticker&lt;BR /&gt;&amp;nbsp;&amp;nbsp; from wanted_stocks;&lt;BR /&gt; create table final_result as&lt;BR /&gt;&amp;nbsp; select s.*&lt;BR /&gt;&amp;nbsp;&amp;nbsp; from unique_stocks u inner join stocks s on(u.ticker = s.ticker);&lt;BR /&gt;quit;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 25 May 2013 21:15:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Keep-consecutive-observations-only/m-p/86672#M24755</guid>
      <dc:creator>TomKari</dc:creator>
      <dc:date>2013-05-25T21:15:46Z</dc:date>
    </item>
    <item>
      <title>Re: Keep consecutive observations only</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Keep-consecutive-observations-only/m-p/86673#M24756</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Tom,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks for the answer. I think that in the first step using the LAG, it omits the first observation of each stock. For example stock 1980-4 520 AA. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 25 May 2013 21:45:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Keep-consecutive-observations-only/m-p/86673#M24756</guid>
      <dc:creator>Costasg</dc:creator>
      <dc:date>2013-05-25T21:45:05Z</dc:date>
    </item>
    <item>
      <title>Re: Keep consecutive observations only</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Keep-consecutive-observations-only/m-p/86674#M24757</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Is this what you're after?&lt;/P&gt;&lt;P&gt;&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 l.*&lt;/P&gt;&lt;P&gt;&amp;nbsp; from &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; example l,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; (&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; select&amp;nbsp; distinct ticker&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; from example&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; group by ticker,date&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; having count(*)&amp;gt;1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ) r&lt;/P&gt;&lt;P&gt;&amp;nbsp; where l.ticker=r.ticker&lt;/P&gt;&lt;P&gt;&amp;nbsp; ;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 26 May 2013 01:01:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Keep-consecutive-observations-only/m-p/86674#M24757</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2013-05-26T01:01:28Z</dc:date>
    </item>
    <item>
      <title>Re: Keep consecutive observations only</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Keep-consecutive-observations-only/m-p/86675#M24758</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I don't get the meaning of "&lt;SPAN style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;2 consecutive observations&lt;/SPAN&gt;" for this data.&amp;nbsp; Does that mean the TICKER appears for ANY manager for adjacent quarters? Or only for adjacent quarters for the same manager.&amp;nbsp; What about "TYPE"?&amp;nbsp; Once you have identified the TICKER values to keep use the list to get all the values.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here is a method using DOW loops, but it will depend on your definition and your data sort order.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want ;&lt;/P&gt;&lt;P&gt; do until (last.ticker) ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; set have ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; by ticker date;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if last.date and 1 = intck('qtr',prev_date,date) then keepit=1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if last.date then prev_date=date;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;do until (last.ticker) ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; set have ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; by ticker date;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if keepit then output;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 26 May 2013 02:18:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Keep-consecutive-observations-only/m-p/86675#M24758</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2013-05-26T02:18:28Z</dc:date>
    </item>
    <item>
      <title>Re: Keep consecutive observations only</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Keep-consecutive-observations-only/m-p/86676#M24759</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Yes, but that's only to get the candidate list of tickers that have at least two consecutive. The next two steps reduce it to a distinct list of tickers, and then pulls out the full matching set from the original dataset.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Tom&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 26 May 2013 16:03:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Keep-consecutive-observations-only/m-p/86676#M24759</guid>
      <dc:creator>TomKari</dc:creator>
      <dc:date>2013-05-26T16:03:15Z</dc:date>
    </item>
    <item>
      <title>Re: Keep consecutive observations only</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Keep-consecutive-observations-only/m-p/86677#M24760</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Tom,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;What I mean is that the TICKER should have 2 consecutive quarters by ANY manager. Keep in mind that it could be the case that in the first quarters the stock could be traded only in single quarters but later on in consecutive quarters. In that case I want to keep the single quarters as well.&lt;/P&gt;&lt;P&gt;The code doesn't work well as at the final table includes tickers with a single quarter observation. For example, ADSC, AMTD, etc.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;TomKari,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Again, somewhere it doesn't work well as at the final table includes tickers with a single quarter observation. For example, ADSC, AMTD, etc.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 26 May 2013 17:09:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Keep-consecutive-observations-only/m-p/86677#M24760</guid>
      <dc:creator>Costasg</dc:creator>
      <dc:date>2013-05-26T17:09:35Z</dc:date>
    </item>
    <item>
      <title>Re: Keep consecutive observations only</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Keep-consecutive-observations-only/m-p/86678#M24761</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Maybe not the most efficient, but does the job :&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;proc sql;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;create table tickers as&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;select distinct A.ticker&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;from &lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sasforum.example as A inner join&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sasforum.example as B on A.ticker=B.ticker and &lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; intnx("QTR", A.date, 0) = intnx("QTR", B.date, 1);&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;create table conseq as&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;select * &lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;from sasforum.example&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;where ticker in (select ticker from tickers)&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;order by ticker, date;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;drop table tickers;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;quit;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;PG&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 26 May 2013 20:25:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Keep-consecutive-observations-only/m-p/86678#M24761</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2013-05-26T20:25:39Z</dc:date>
    </item>
    <item>
      <title>Re: Keep consecutive observations only</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Keep-consecutive-observations-only/m-p/86679#M24762</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;"&gt;Identify the stocks to include. Then use that information to pull the information from the file.&amp;nbsp; That is what Pierre Gagnon posed below as two SQL steps and what I posted above as a single DATA step.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif;"&gt;Or are you trying to say that stocks that only appear in the last quarter in the input data should also be kept?&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 26 May 2013 21:45:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Keep-consecutive-observations-only/m-p/86679#M24762</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2013-05-26T21:45:14Z</dc:date>
    </item>
    <item>
      <title>Re: Keep consecutive observations only</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Keep-consecutive-observations-only/m-p/86680#M24763</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;What about the SQL I've posted earlier? It first creates a list of distinct tickers with at least 2 rows in the same quarter and then matches these tickers to the full table so getting all rows from the source table with matching tickers.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Isn't this what you've asked for?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 26 May 2013 23:05:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Keep-consecutive-observations-only/m-p/86680#M24763</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2013-05-26T23:05:54Z</dc:date>
    </item>
    <item>
      <title>Re: Keep consecutive observations only</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Keep-consecutive-observations-only/m-p/86681#M24764</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you PGStats, it works! As you said not the most efficient, but it does the job.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you Patrick, I need the ticker to have at least 2 quarters in a row, not at 2 rows in the same quarter.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Tom, your code included stocks that were traded in only 1 quarter. The stocks that I want are the ones that are traded in at least 2 quarters in a row (by any manager). &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 26 May 2013 23:31:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Keep-consecutive-observations-only/m-p/86681#M24764</guid>
      <dc:creator>Costasg</dc:creator>
      <dc:date>2013-05-26T23:31:46Z</dc:date>
    </item>
    <item>
      <title>Re: Keep consecutive observations only</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Keep-consecutive-observations-only/m-p/86682#M24765</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The data step will set the KEEPIT flag when it detects that the current qtr is right after the previous one.&amp;nbsp; There is no way it could select "singles", unless you have some other criteria I have missed that implies a different meaning for that (such requiring that the consecutive qtrs are by the same trader).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It identifies the same set of ticker symbols and same set of selected events as the SQL query.&amp;nbsp; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 27 May 2013 00:13:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Keep-consecutive-observations-only/m-p/86682#M24765</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2013-05-27T00:13:08Z</dc:date>
    </item>
    <item>
      <title>Re: Keep consecutive observations only</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Keep-consecutive-observations-only/m-p/86683#M24766</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I agree with Tom. Something must have gone wrong in your test of his code. When I try it on properly sorted data, it gives me the same number of observations as mine (398856) and the same set of tickers.&lt;/P&gt;&lt;P&gt;&amp;nbsp; &lt;/P&gt;&lt;P&gt;PG&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 27 May 2013 02:09:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Keep-consecutive-observations-only/m-p/86683#M24766</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2013-05-27T02:09:26Z</dc:date>
    </item>
  </channel>
</rss>

