<?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 Choosing the Previous Quarter  using INTNX function in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Choosing-the-Previous-Quarter-using-INTNX-function/m-p/522106#M141698</link>
    <description>&lt;P&gt;hey all,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have another question that`s similar to what I had before.&lt;/P&gt;
&lt;P&gt;I have a dataset called AA, and it has these following variables:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;Data set&amp;nbsp;AA (four&amp;nbsp;columns, 1642&amp;nbsp;rows-companies), quarter represents which quarter that company filed for bankruptcy(this variable is very important),&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD&gt;Company Name&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;RAYTECH CORP&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;MACY'S INC&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;UNITED AIRLINES INC&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;TWIN STAR PRODUCTIONS INC&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;TABLE&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD&gt;CIK&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;797917&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;794367&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;319687&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;847908&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD&gt;Bankruptcy date&lt;/TD&gt;
&lt;TD&gt;Quarter&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;3/10/1989&lt;/TD&gt;
&lt;TD&gt;1989Q1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1/15/1990&lt;/TD&gt;
&lt;TD&gt;1990Q1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;12/3/1990&lt;/TD&gt;
&lt;TD&gt;1990Q4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;6/6/1991&lt;/TD&gt;
&lt;TD&gt;1991Q2&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;in the second data set, lets call it FF, I have all the 1642 companies quarterly financial data from 1985 till 2018 (75111 rows),like total assets, total liabilities, net income, R&amp;amp;D expenditure, etc.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;What I want to do is to find out each companies`&amp;nbsp;l&lt;STRONG&gt;ast quarterly&lt;/STRONG&gt;&lt;EM&gt;(but within 12 months)&amp;nbsp;f&lt;/EM&gt;iled financial data&amp;nbsp;of total assets, total liabilities, net income and R&amp;amp;D expenditure&amp;nbsp;&lt;STRONG&gt;before their bankruptcy filings&lt;/STRONG&gt;, and take&amp;nbsp;those values to create&amp;nbsp; new columns.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, in data set AA,&amp;nbsp; "&lt;SPAN&gt;RAYTECH CORP" went bankrupt&amp;nbsp;on&amp;nbsp;3/10/1989, which is in 1989Q1( first quarter), I firstly want to&amp;nbsp; merge&amp;nbsp;these two data sets by variable "CIK"(to locate the company in data set FF from data set AA). When I matched the company in dataset FF, I want&amp;nbsp;to extract the&amp;nbsp;financial values for this company in that bankruptcy filed quarter (19&lt;STRONG&gt;89&lt;/STRONG&gt;Q1) or if we don`t have data keep looking for (19&lt;STRONG&gt;88&lt;/STRONG&gt;Q4), (1988Q3), (1988Q2), (1988Q1) , and&amp;nbsp; &amp;nbsp;stop after one full circle(full 12 months, or 4 quarters). as a final results I want to have company names, CIK, and total assets, total liabilities, net income and R&amp;amp;D expenditure.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I attached this company`s financial&amp;nbsp;data, and the area colored&amp;nbsp;in yellow is the values that I want to extract (as we have the financial data on the bankruptcy filed quarter, so we&amp;nbsp; dont have to keep looking)&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I am not sure how to proceed as the interval is quarter( not a number of months), and it is more like moving the rows in the data set FF.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Thanks in advance,&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 28 Dec 2018 19:18:13 GMT</pubDate>
    <dc:creator>Ziba</dc:creator>
    <dc:date>2018-12-28T19:18:13Z</dc:date>
    <item>
      <title>Choosing the Previous Quarter  using INTNX function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Choosing-the-Previous-Quarter-using-INTNX-function/m-p/522106#M141698</link>
      <description>&lt;P&gt;hey all,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have another question that`s similar to what I had before.&lt;/P&gt;
&lt;P&gt;I have a dataset called AA, and it has these following variables:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;Data set&amp;nbsp;AA (four&amp;nbsp;columns, 1642&amp;nbsp;rows-companies), quarter represents which quarter that company filed for bankruptcy(this variable is very important),&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD&gt;Company Name&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;RAYTECH CORP&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;MACY'S INC&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;UNITED AIRLINES INC&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;TWIN STAR PRODUCTIONS INC&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;TABLE&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD&gt;CIK&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;797917&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;794367&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;319687&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;847908&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD&gt;Bankruptcy date&lt;/TD&gt;
&lt;TD&gt;Quarter&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;3/10/1989&lt;/TD&gt;
&lt;TD&gt;1989Q1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1/15/1990&lt;/TD&gt;
&lt;TD&gt;1990Q1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;12/3/1990&lt;/TD&gt;
&lt;TD&gt;1990Q4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;6/6/1991&lt;/TD&gt;
&lt;TD&gt;1991Q2&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;in the second data set, lets call it FF, I have all the 1642 companies quarterly financial data from 1985 till 2018 (75111 rows),like total assets, total liabilities, net income, R&amp;amp;D expenditure, etc.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;What I want to do is to find out each companies`&amp;nbsp;l&lt;STRONG&gt;ast quarterly&lt;/STRONG&gt;&lt;EM&gt;(but within 12 months)&amp;nbsp;f&lt;/EM&gt;iled financial data&amp;nbsp;of total assets, total liabilities, net income and R&amp;amp;D expenditure&amp;nbsp;&lt;STRONG&gt;before their bankruptcy filings&lt;/STRONG&gt;, and take&amp;nbsp;those values to create&amp;nbsp; new columns.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, in data set AA,&amp;nbsp; "&lt;SPAN&gt;RAYTECH CORP" went bankrupt&amp;nbsp;on&amp;nbsp;3/10/1989, which is in 1989Q1( first quarter), I firstly want to&amp;nbsp; merge&amp;nbsp;these two data sets by variable "CIK"(to locate the company in data set FF from data set AA). When I matched the company in dataset FF, I want&amp;nbsp;to extract the&amp;nbsp;financial values for this company in that bankruptcy filed quarter (19&lt;STRONG&gt;89&lt;/STRONG&gt;Q1) or if we don`t have data keep looking for (19&lt;STRONG&gt;88&lt;/STRONG&gt;Q4), (1988Q3), (1988Q2), (1988Q1) , and&amp;nbsp; &amp;nbsp;stop after one full circle(full 12 months, or 4 quarters). as a final results I want to have company names, CIK, and total assets, total liabilities, net income and R&amp;amp;D expenditure.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I attached this company`s financial&amp;nbsp;data, and the area colored&amp;nbsp;in yellow is the values that I want to extract (as we have the financial data on the bankruptcy filed quarter, so we&amp;nbsp; dont have to keep looking)&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I am not sure how to proceed as the interval is quarter( not a number of months), and it is more like moving the rows in the data set FF.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Thanks in advance,&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 28 Dec 2018 19:18:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Choosing-the-Previous-Quarter-using-INTNX-function/m-p/522106#M141698</guid>
      <dc:creator>Ziba</dc:creator>
      <dc:date>2018-12-28T19:18:13Z</dc:date>
    </item>
    <item>
      <title>Re: Choosing the Previous Quarter  using INTNX function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Choosing-the-Previous-Quarter-using-INTNX-function/m-p/522111#M141701</link>
      <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data HAVE;
  input CPY $ CIK $ BK_DTE : mmddyy10. BK_QTR $ ;
  cards;
UNITED 319687 12/3/1990 1990Q4
MACY 794367 1/15/1990 1990Q1
RAYTECH 797917 3/10/1989 1989Q1
run;

data FF;
  CIK='319687'; QTR='1989Q4'; ASSET=1; output;
  CIK='319687'; QTR='1988Q3'; ASSET=1; output;
  CIK='794367'; QTR='1989Q3'; ASSET=1; output;
  CIK='794367'; QTR='1988Q2'; ASSET=1; output;
  CIK='797917'; QTR='1989Q1'; ASSET=1; output;
  CIK='797917'; QTR='1988Q4'; ASSET=1; output;
run;

data WANT;
  merge HAVE FF;
  by CIK;
  DIST= intck('quarter',input(BK_QTR,yyq6.),input(QTR,yyq6.));
  if first.CIK ;          /* The first match is always the best, i.e. the closest date */
  if DIST &amp;lt; -3 then call missing(QTR, ASSET); /* If date too far back then nullify the match*/ 
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="center"&gt;
&lt;TABLE class="table" summary="Procedure Print: Data Set WORK.WANT" frame="box" rules="all" cellspacing="0" cellpadding="5"&gt;&lt;COLGROUP&gt; &lt;COL /&gt; &lt;COL /&gt; &lt;COL /&gt; &lt;COL /&gt; &lt;COL /&gt; &lt;COL /&gt;&lt;/COLGROUP&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="l header" scope="col"&gt;CPY&lt;/TH&gt;
&lt;TH class="l header" scope="col"&gt;CIK&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;BK_DTE&lt;/TH&gt;
&lt;TH class="l header" scope="col"&gt;BK_QTR&lt;/TH&gt;
&lt;TH class="l header" scope="col"&gt;QTR&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;ASSET&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;UNITED&lt;/TD&gt;
&lt;TD class="l data"&gt;319687&lt;/TD&gt;
&lt;TD class="r data"&gt;11294&lt;/TD&gt;
&lt;TD class="l data"&gt;1990Q4&lt;/TD&gt;
&lt;TD class="l data"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="r data"&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;MACY&lt;/TD&gt;
&lt;TD class="l data"&gt;794367&lt;/TD&gt;
&lt;TD class="r data"&gt;10972&lt;/TD&gt;
&lt;TD class="l data"&gt;1990Q1&lt;/TD&gt;
&lt;TD class="l data"&gt;1989Q3&lt;/TD&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;RAYTECH&lt;/TD&gt;
&lt;TD class="l data"&gt;797917&lt;/TD&gt;
&lt;TD class="r data"&gt;10661&lt;/TD&gt;
&lt;TD class="l data"&gt;1989Q1&lt;/TD&gt;
&lt;TD class="l data"&gt;1989Q1&lt;/TD&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Dec 2018 04:03:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Choosing-the-Previous-Quarter-using-INTNX-function/m-p/522111#M141701</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-12-18T04:03:22Z</dc:date>
    </item>
    <item>
      <title>Re: Choosing the Previous Quarter  using INTNX function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Choosing-the-Previous-Quarter-using-INTNX-function/m-p/522113#M141703</link>
      <description>&lt;P&gt;You want the most recent quarterly&amp;nbsp;FF quarterly&amp;nbsp;record prior to or equal to the quarter of bankruptcy in dataset AA - as long as the FF record is no more than 4 quarters preceding the bankruptcy.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I see you have a date variable in AA.&amp;nbsp; Do you also have a date variables (not a quarter identifier) in FF?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Dec 2018 04:35:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Choosing-the-Previous-Quarter-using-INTNX-function/m-p/522113#M141703</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-12-18T04:35:11Z</dc:date>
    </item>
    <item>
      <title>Re: Choosing the Previous Quarter  using INTNX function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Choosing-the-Previous-Quarter-using-INTNX-function/m-p/522217#M141745</link>
      <description>&lt;P&gt;Hey Kris,&lt;/P&gt;&lt;P&gt;Thanks for your efforts, but as a final result,&amp;nbsp;I want to get each company`s financial data(i attached a document, you can have a alook), not the quarter.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Dec 2018 14:03:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Choosing-the-Previous-Quarter-using-INTNX-function/m-p/522217#M141745</guid>
      <dc:creator>Ziba</dc:creator>
      <dc:date>2018-12-18T14:03:42Z</dc:date>
    </item>
    <item>
      <title>Re: Choosing the Previous Quarter  using INTNX function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Choosing-the-Previous-Quarter-using-INTNX-function/m-p/522225#M141749</link>
      <description>&lt;P&gt;In dataset AA I only have bankruptcy date as a date variable, the quarter identifier that I have in AA is a numeric value(that I filled manually). I believe in FF, that quarter identifier is numeric as well. BUT in FF, I also have a date variable, which indicates the quarterly end month, like 19890331(Q1),19890630(Q2),199890930,(Q3)19891230 (Q4). I was wondering if it is possible first to determine the quarterly end month for each company by looking at bankruptcy dates, and then proceed with the same procedure.&amp;nbsp;&lt;/P&gt;&lt;P&gt;thank you&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Dec 2018 14:15:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Choosing-the-Previous-Quarter-using-INTNX-function/m-p/522225#M141749</guid>
      <dc:creator>Ziba</dc:creator>
      <dc:date>2018-12-18T14:15:30Z</dc:date>
    </item>
    <item>
      <title>Re: Choosing the Previous Quarter  using INTNX function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Choosing-the-Previous-Quarter-using-INTNX-function/m-p/522334#M141786</link>
      <description>&lt;P&gt;Do not provide information in MS Office documents.&lt;/P&gt;
&lt;P&gt;Many people including myself cannot/will not download them due to the risk these documents present.&lt;/P&gt;
&lt;P&gt;The format for presenting the input data and the desired solution is well defined. See:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank" rel="noopener noreferrer"&gt;How to convert datasets to data steps&lt;/A&gt;&lt;BR /&gt;&lt;A href="https://gist.githubusercontent.com/statgeek/bcc55940dd825a13b9c8ca40a904cba9/raw/865d2cf18f5150b8e887218dde0fc3951d0ff15b/data2datastep.sas" target="_blank" rel="nofollow noopener noreferrer noopener noreferrer"&gt;The macro for direct download&lt;/A&gt;&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/help/faqpage/faq-category-id/posting#posting" target="_blank" rel="noopener noreferrer"&gt;How to post code&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Please stick to it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;My solution provides the company's data, reduced to variable ASSET in my example.&lt;/P&gt;</description>
      <pubDate>Tue, 18 Dec 2018 20:43:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Choosing-the-Previous-Quarter-using-INTNX-function/m-p/522334#M141786</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-12-18T20:43:29Z</dc:date>
    </item>
    <item>
      <title>Re: Choosing the Previous Quarter  using INTNX function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Choosing-the-Previous-Quarter-using-INTNX-function/m-p/522404#M141817</link>
      <description>&lt;P&gt;Let's say you have&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The bankruptcy data set AA, with one record per CIK, sorted by CIK, and with bankruptcy date BKRDATE&lt;/LI&gt;
&lt;LI&gt;The quarterly data set FF, sorted by CIK/DATADATE, with never more than one record per quarter.&amp;nbsp; DATADATE is always the last date of the quarter.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;You want to produce one record per bankrupt CIK, with the same-quarter FF data, or most recent FF data prior to BKRDATE.&amp;nbsp; One of the issues is that BKRDATE may be 15feb2011, but the DATADATE for that quarter is 31mar2011.&amp;nbsp; So, depending on whether there is a quarterly FF observation for the AA bankruptcy quarter, vs a prior FF quarter, you'll&amp;nbsp;can end up with an FF&amp;nbsp;record with DATADATE either preceding (by up to 4 quarters) or after&amp;nbsp;BKRDATE (same quarter).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below is a synthetic sample for CIK's 1,2,3&amp;nbsp;and 4, all with BKRDATE=15feb2011 in data set AA.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But in FF&amp;nbsp; (with * next to the proper selection)&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;CIK 1 ends on the same quarter (datadate=31mar2011)&lt;/LI&gt;
&lt;LI&gt;CIK 2 ends in the prior quarter (31dec2010),&lt;/LI&gt;
&lt;LI&gt;CIK 3 ends 3 quarters prior (30jun2010),&lt;/LI&gt;
&lt;LI&gt;CIK 4 ends too early (31dec2009)&lt;/LI&gt;
&lt;LI&gt;CIK 5 has no bankruptcy record in AA&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data aa;
  input cik bkrdate :date9.;
  format bkrdate yymmddn8.;
datalines;
1 15feb2011
2 15feb2011
3 15feb2011
4 15feb2011
run;

data ff;
  input cik datadate :date9.; format datadate yymmddn8.;
datalines;
 1 31dec2009
 1 31mar2010
 1 30jun2010
 1 30sep2010
 1 31dec2010
 1 31mar2011  *
 2 31dec2009
 2 31mar2010
 2 30jun2010
 2 30sep2010
 2 31dec2010  *
 3 31dec2009
 3 31mar2010
 3 30jun2010  *
 4 31dec2009  None
 5 31dec2009  None
 5 31mar2010
 5 30jun2010
 5 30sep2010
 5 31dec2010
 5 31mar2011
 5 30jun2011
run;

data want (drop=_:);
  do until (last.cik);
    set aa (keep=cik in=inaa) ff (keep=cik);  /*Get CIK's only */
    by cik;
    if inaa then set aa;                      /* Get BKRDATE*/
    else do;
      merge ff
            ff (firstobs=2 keep=datadate rename=(datadate=_nxtdate));
      if last.cik then _nxtdate='31dec9999'd;
      if bkrdate=. then continue;                                 /*No need to test dates*/
      if not (0&amp;lt;=intck('qtr',datadate,bkrdate)&amp;lt;=4) then continue; /*Get another FF*/
      if 0&amp;lt;=intck('qtr',_nxtdate,bkrdate)&amp;lt;=4 then continue;       /*Get another FF*/
      output;
    end;
  end;
  bkrdate=.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For each CIK:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The program reads the AA record prior to all the FF records.&lt;/LI&gt;
&lt;LI&gt;If the record-in-hand is a CIK from AA, then read the BKRDATE from AA.&amp;nbsp; This will be automatically retained through all the FF records until then next AA record is encountered (for the next CIK).&lt;/LI&gt;
&lt;LI&gt;If not an AA record (i.e. record-in-hand is a CIK from FF) then
&lt;OL&gt;
&lt;LI&gt;read the full FF record and also the next FF datadate (renamed to _nxtdate).&lt;/LI&gt;
&lt;LI&gt;if the record-in-hand is last for this CIK, then _nxtdate is for the following CIK and is set to 31dec9999.&lt;/LI&gt;
&lt;LI&gt;If the BKRDATE is missing (i.e. no AA record for this CIK) then "continue" the do group - i.e. go to top of loop and read more FF data.&lt;/LI&gt;
&lt;LI&gt;if FF datadate for record in hand is NOT between zero quarters and 4 quarters prior to bkrdate then "continue"&amp;nbsp;&amp;nbsp;(go to top of loop and get another record).&lt;/LI&gt;
&lt;LI&gt;If _nxtdate following&amp;nbsp;IS between zero and 4 quarters prior to bkrdate then "continue" - go to top of loop and get that record, so that DATADATE will pass the test/&lt;/LI&gt;
&lt;LI&gt;The only record that can pass #4 and #5 above is the best possible FF record, so it is OUTPUT.&amp;nbsp; Note that&amp;nbsp;while there can be multiple records with satisfactory&amp;nbsp;DATADATEs, the&amp;nbsp;test on _NXTDATE will&amp;nbsp;force another record to be read if _NXTDATE also qualifies, so you won't output multiple records per CIK, because eventually _NXTDATE will no longer qualify.&lt;/LI&gt;
&lt;/OL&gt;
&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;Note if there is an AA record, but no FF records, the is no output, just as if&amp;nbsp;there were FF records, but none qualified.&amp;nbsp; Same if there are FF records, but no AA record - no output.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Dec 2018 01:50:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Choosing-the-Previous-Quarter-using-INTNX-function/m-p/522404#M141817</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-12-19T01:50:56Z</dc:date>
    </item>
    <item>
      <title>Re: Choosing the Previous Quarter  using INTNX function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Choosing-the-Previous-Quarter-using-INTNX-function/m-p/523787#M142357</link>
      <description>&lt;P&gt;Thank you so much for your help, the code that you provided worked perfectly. I really appreciated&amp;nbsp;your&amp;nbsp;help.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 28 Dec 2018 18:30:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Choosing-the-Previous-Quarter-using-INTNX-function/m-p/523787#M142357</guid>
      <dc:creator>Ziba</dc:creator>
      <dc:date>2018-12-28T18:30:27Z</dc:date>
    </item>
  </channel>
</rss>

