<?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: Number of quarters between two dates with gaps in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Number-of-quarters-between-two-dates-with-gaps/m-p/373792#M89427</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15329"&gt;@PhilC&lt;/a&gt;, I just added a few lines to your code and I think the code is now working well:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have1 out=have1_sorted ;
  by mgrno Firm_ID report_date;
quit;

data try (Keep=Mgrno Firm_ID beg_hold_period end_hold_period) ;
  do until (last.Firm_ID);
    set have1_sorted;
      by Mgrno Firm_ID Report_date;
      lag_Report_date=lag(Report_date);
      format beg_hold_period end_hold_period date9.;
    if first.FIRM_ID 
      then beg_hold_period=Report_date;
      else if intck("Month3", lag_Report_date,Report_date)&amp;gt;1         then do;
    	  end_hold_period=lag_Report_date;
          output;
    	  beg_hold_period=Report_date;
    	end;
  end;
  end_hold_period=Report_date;
  output;
run;


*Added the part below ;

proc sort data=try;
	by mgrno firm_id descending end_hold_period;
quit;


proc sort data=try nodupkey;
	by mgrno firm_id;
quit;  


data merged;
	merge try have1 have2;
	by mgrno firm_id;
run;


data counting;
	set merged;
	if  Event_Date &amp;gt;= Report_Date &amp;gt;= beg_hold_period;
run;

proc summary data=work.counting nway;
  var Report_Date;
  class mgrno group;
  output out=work.duration
  n=duration;
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 06 Jul 2017 22:56:01 GMT</pubDate>
    <dc:creator>Yegen</dc:creator>
    <dc:date>2017-07-06T22:56:01Z</dc:date>
    <item>
      <title>Number of quarters between two dates with gaps</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Number-of-quarters-between-two-dates-with-gaps/m-p/372636#M89127</link>
      <description>&lt;P&gt;I am trying to calculate different duration&amp;nbsp;periods that measure&amp;nbsp;how long a firm remains in a given manager's portfolio.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;The data structure looks&amp;nbsp;as follows&lt;/STRONG&gt;:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;There are distinct groups of two managers (where one of the managers may appear several times in the data in different groups).&lt;/LI&gt;&lt;LI&gt;Within the groups, each manager holds several companies (some firms may be hold by both managers, but these cases are very rare).&lt;/LI&gt;&lt;LI&gt;Whenever a manger holds a given company in his/her&amp;nbsp;portfolio, then the manger has to disclose its share rate (reporting date is given).&amp;nbsp;&lt;/LI&gt;&lt;LI&gt;&lt;STRONG&gt;NOTE&lt;/STRONG&gt;: It is possible that there may be gaps between the reporting dates for a given group-manager-firm pair. Why does this happen? Occasionally, the manager&amp;nbsp;buys shares of a given company, sell these shares, and then buys new shares after certain time has passed (i.e., this may happen with gaps). However, whenever rate &amp;gt;=0.01, the manager has to disclose the holding.&amp;nbsp;&amp;nbsp;&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&lt;STRONG&gt;What have I done?&lt;/STRONG&gt;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;I have &lt;SPAN&gt;calculated&lt;/SPAN&gt; how long (i.e.,&amp;nbsp;many quarters ) a group-manager holds a given company. (Holding_Duration1), but have ignored any gaps (biasing this variable).&lt;/LI&gt;&lt;LI&gt;I have &lt;SPAN&gt;calculated&lt;/SPAN&gt; how long (i.e.,&amp;nbsp;many quarters ) a group-manager holds a given company in which the share rate is above 5%. (Holding_Duration2),&amp;nbsp;but have ignored any gaps (biasing this variable).&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&lt;STRONG&gt;What am I missing? (I want to overcome the bias of not taking into account for the gaps between reporting dates)&lt;/STRONG&gt;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;How many quarters has it passed since the first reporting date until the event date? (Event_minus_First)&lt;/LI&gt;&lt;LI&gt;How many quartesr has it passed since the very last reporting data after which there were no reporting date gaps? (Last_Subsequent_Duration). &lt;STRONG&gt;NOTE&lt;/STRONG&gt;: The reason of calculating this variable is&amp;nbsp;to avoid the following problem. Assume in&amp;nbsp;2001Q1, a given manager purchased shares in firm ABC. Assume the manager hold his/her&amp;nbsp;shares above 5% until 2003Q2, but then sold off all shares sometime between&amp;nbsp;&lt;SPAN&gt;2003Q2 and&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;2003Q3 (so that particular firm disappears in&amp;nbsp;2003Q2).&lt;/SPAN&gt;&amp;nbsp;Then in 2005Q1, the manager again purchased shares from&amp;nbsp;firm ABC and hold his/her&amp;nbsp;shares above 5% until the event date which was in 2008Q1. In this case,&amp;nbsp;&lt;SPAN&gt;Last_Subsequent_Duration=12 because&amp;nbsp;12 quarters has passed since the most recent&amp;nbsp;reporting date&amp;nbsp;after which the manager did not sell his / her shares of company ABC.&lt;/SPAN&gt;&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Here are&amp;nbsp;my "have" as well as "want" datasets:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA have1;&lt;BR /&gt; INPUT Mgrno Firm_ID $ Report_Date Date9. Rate;&lt;BR /&gt; FORMAT Report_Date Date9.;&lt;BR /&gt; DATALINES; &lt;BR /&gt; 19500 12727AZ 31MAR1980 0.04&lt;BR /&gt; 19500 12727AZ 30JUN1980 0.05 &lt;BR /&gt; 19500 12727AZ 30SEP1980 0.08&lt;BR /&gt; 19500 12727AZ 31DEC1980 0.07&lt;BR /&gt; 19500 12727AZ 31MAR1981 0.09&lt;BR /&gt; 19500 12727AZ 30JUN1981 0.02&lt;BR /&gt; 19500 12727AZ 30SEP1981 0.08&lt;BR /&gt; 19500 12727AZ 31DEC1981 0.05&lt;BR /&gt; 19500 12727AZ 31MAR1982 0.07 &lt;BR /&gt; 19500 12727AZ 30JUN1982 0.10 &lt;BR /&gt; 39547 12727AZ 30JUN1980 0.02&lt;BR /&gt; 39547 12727AZ 30SEP1980 0.01&lt;BR /&gt; 39547 12727AZ 31DEC1980 0.06&lt;BR /&gt; 39547 12727AZ 31MAR1981 0.06&lt;BR /&gt; 99999 731238A 31MAR1982 0.11&lt;BR /&gt; 99999 731238A 30JUN1982 0.12&lt;BR /&gt; 99999 731238A 30JUN1983 0.07&lt;BR /&gt;;&lt;BR /&gt;run;

Data have2;
	INPUT Mgrno Firm_ID $ Event_Date Date9. Rate Group $;
	FORMAT Event_Date Date9.;
	DATALINES;
	19500 12727AZ 31MAR1982 0.07 AA
	39547 12727AZ 31MAR1981 0.06 BB
	99999 731238A 30JUN1983 0.07 BB
;
run;
&lt;BR /&gt;/*
Holding_Duration1=Number of quarters past until the event_date since the company was included in the portfolio of the manager for the first time until the event_date
Holding_Duration1=Number of quarters past until the event_date since the company was included in the portfolio of the manager for the first time until the event_date
as well as where the rate &amp;gt;= 0.05
Total=Number of quarter past from the first time the company was added into the porfolio until the event_date
Event_minus_First= Event date - First date in terms of number of quarters&lt;BR /&gt;*/

Data want;
	INPUT Mgrno Firm_ID $ Event_Date Date9. Rate Group $ Holding_Duration1 Holding_Duration2 Total Event_minus_First Last_Subsequent_Duration;
	FORMAT Event_Date Date9.;
	DATALINES;
	19500 12727AZ 31MAR1981 0.07 AA 8 6 8 8 6
	39547 12727AZ 31MAR1981 0.06 BB 4 2 4 4 6
	99999 731238A 30JUN1983 0.07 BB 3 3 3 7 1
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Here is my code for the "what I have done" part:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*First I will include all the reported holding filings of the manager for the given firm;
proc sql;
	create table first_method_V1 as 
	select *
	from have1 as a 
	right join have2 as b 
	on (a.mgrno=b.mgrno) &amp;amp; (a.firm_id=b.firm_id);
quit;


proc summary data=work.first_method_V1 nway;
  var Report_Date;
  class mgrno group;
  output out=work.Holding_Duration1
  n=Holding_Duration1;
run;

data only_5percent;
	set first_method_V1;
	if rate &amp;gt;= 0.05 &amp;amp; Event_Date &amp;gt;= Report_Date;
run;

proc summary data=work.only_5percent nway;
  var Report_Date;
  class mgrno group;
  output out=work.Holding_Duration2
  n=Holding_Duration2;
run;

data first_method_V2(drop= _TYPE_ _FREQ_);
	merge first_method_V1 Holding_Duration1 Holding_Duration2;
	by mgrno group;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Thanks in advance for any suggestions /&amp;nbsp;help you may provide.&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jul 2017 22:53:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Number-of-quarters-between-two-dates-with-gaps/m-p/372636#M89127</guid>
      <dc:creator>Yegen</dc:creator>
      <dc:date>2017-07-06T22:53:27Z</dc:date>
    </item>
    <item>
      <title>Re: Number of quarters between two dates with gaps</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Number-of-quarters-between-two-dates-with-gaps/m-p/372759#M89172</link>
      <description>&lt;P&gt;use INTCK() function.&amp;nbsp; For your case the interval will be "MONTH3"&amp;nbsp; The interval being "Month" and the multiplier being "3".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://support.sas.com/documentation/cdl/en/lefunctionsref/63354/HTML/default/viewer.htm#p0syn64amroombn14vrdzksh459w.htm" target="_self"&gt;https://support.sas.com/documentation/cdl/en/lefunctionsref/63354/HTML/default/viewer.htm#p0syn64amroombn14vrdzksh459w.htm &lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Jul 2017 15:11:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Number-of-quarters-between-two-dates-with-gaps/m-p/372759#M89172</guid>
      <dc:creator>PhilC</dc:creator>
      <dc:date>2017-07-03T15:11:20Z</dc:date>
    </item>
    <item>
      <title>Re: Number of quarters between two dates with gaps</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Number-of-quarters-between-two-dates-with-gaps/m-p/372804#M89176</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15329"&gt;@PhilC&lt;/a&gt;, thanks for the suggestion, but in the document it states the following:&lt;BR /&gt;&lt;BR /&gt;"The intervals must be listed in ascending order. &lt;STRONG&gt;There cannot be gaps between intervals&lt;/STRONG&gt;, and intervals cannot overlap."&lt;BR /&gt;&lt;BR /&gt;The major issue I am having is that there may be some gaps between the interval and what I am trying to calculate is the most recent reporting date (before the event date)&amp;nbsp;in which there are no gaps. Then, I can simply just calculate the quarters between these two dates.&lt;BR /&gt;&lt;BR /&gt;Do you have any suggestions how to overcome this issue?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Jul 2017 18:14:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Number-of-quarters-between-two-dates-with-gaps/m-p/372804#M89176</guid>
      <dc:creator>Yegen</dc:creator>
      <dc:date>2017-07-03T18:14:07Z</dc:date>
    </item>
    <item>
      <title>Re: Number of quarters between two dates with gaps</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Number-of-quarters-between-two-dates-with-gaps/m-p/372811#M89178</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15329"&gt;@PhilC&lt;/a&gt;&amp;nbsp;with the intck() function, I can calculate the intervals but still have the gaps in between issue. To overcome that issue, I thought of making use of the lag() function and checking whether there are any gaps. What I am still missing is the following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Selecting the most recent starting date of reporting after which there are no gaps. In other words, how can I select the reporting date which is&amp;nbsp;right after the last flag=1? That way, I can calculate the holding period during which there are no gaps.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any suggestions will be greatly appreciated. Here is what I have&amp;nbsp;added to the above code:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=first_method_V2;
	by mgrno report_date;
quit;

data try;
	set first_method_V2;
	Q=intck('month3', Report_date, Event_date);
run;

data try;
	set try;
	gaps=Q-lag(Q);
	if gaps^=-1 then flag=1; else flag=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 03 Jul 2017 18:55:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Number-of-quarters-between-two-dates-with-gaps/m-p/372811#M89178</guid>
      <dc:creator>Yegen</dc:creator>
      <dc:date>2017-07-03T18:55:52Z</dc:date>
    </item>
    <item>
      <title>Re: Number of quarters between two dates with gaps</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Number-of-quarters-between-two-dates-with-gaps/m-p/373360#M89316</link>
      <description>&lt;P&gt;I see,&amp;nbsp;the concept of a&amp;nbsp;Do "Whitlock" loop might be something to introduce here...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have1 out=have1_sorted ;
  by mgrno Firm_ID report_date;
quit;

data try (Keep=Mgrno Firm_ID beg_hold_period end_hold_period) ;
  do until (last.Firm_ID);
    set have1_sorted;
      by Mgrno Firm_ID Report_date;
      lag_Report_date=lag(Report_date);
      format beg_hold_period end_hold_period date9.;
    if first.FIRM_ID 
      then beg_hold_period=Report_date;
      else if intck("Month3", lag_Report_date,Report_date)&amp;gt;1 &lt;BR /&gt;        then do;
    	  end_hold_period=lag_Report_date;
          output;
    	  beg_hold_period=Report_date;
    	end;
  end;
  end_hold_period=Report_date;
  output;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;not the complete solution but demostrates that a dataset can be created with one record per holding period, consisting of holdings held consecutive quarters.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jul 2017 17:11:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Number-of-quarters-between-two-dates-with-gaps/m-p/373360#M89316</guid>
      <dc:creator>PhilC</dc:creator>
      <dc:date>2017-07-05T17:11:01Z</dc:date>
    </item>
    <item>
      <title>Re: Number of quarters between two dates with gaps</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Number-of-quarters-between-two-dates-with-gaps/m-p/373511#M89362</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15329"&gt;@PhilC&lt;/a&gt;. I will give it a try and keep you updated if I come up with a few other changes.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jul 2017 05:33:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Number-of-quarters-between-two-dates-with-gaps/m-p/373511#M89362</guid>
      <dc:creator>Yegen</dc:creator>
      <dc:date>2017-07-06T05:33:26Z</dc:date>
    </item>
    <item>
      <title>Re: Number of quarters between two dates with gaps</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Number-of-quarters-between-two-dates-with-gaps/m-p/373792#M89427</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15329"&gt;@PhilC&lt;/a&gt;, I just added a few lines to your code and I think the code is now working well:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have1 out=have1_sorted ;
  by mgrno Firm_ID report_date;
quit;

data try (Keep=Mgrno Firm_ID beg_hold_period end_hold_period) ;
  do until (last.Firm_ID);
    set have1_sorted;
      by Mgrno Firm_ID Report_date;
      lag_Report_date=lag(Report_date);
      format beg_hold_period end_hold_period date9.;
    if first.FIRM_ID 
      then beg_hold_period=Report_date;
      else if intck("Month3", lag_Report_date,Report_date)&amp;gt;1         then do;
    	  end_hold_period=lag_Report_date;
          output;
    	  beg_hold_period=Report_date;
    	end;
  end;
  end_hold_period=Report_date;
  output;
run;


*Added the part below ;

proc sort data=try;
	by mgrno firm_id descending end_hold_period;
quit;


proc sort data=try nodupkey;
	by mgrno firm_id;
quit;  


data merged;
	merge try have1 have2;
	by mgrno firm_id;
run;


data counting;
	set merged;
	if  Event_Date &amp;gt;= Report_Date &amp;gt;= beg_hold_period;
run;

proc summary data=work.counting nway;
  var Report_Date;
  class mgrno group;
  output out=work.duration
  n=duration;
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 06 Jul 2017 22:56:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Number-of-quarters-between-two-dates-with-gaps/m-p/373792#M89427</guid>
      <dc:creator>Yegen</dc:creator>
      <dc:date>2017-07-06T22:56:01Z</dc:date>
    </item>
    <item>
      <title>Re: Number of quarters between two dates with gaps</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Number-of-quarters-between-two-dates-with-gaps/m-p/373972#M89492</link>
      <description>&lt;P&gt;glad it worked&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jul 2017 13:55:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Number-of-quarters-between-two-dates-with-gaps/m-p/373972#M89492</guid>
      <dc:creator>PhilC</dc:creator>
      <dc:date>2017-07-07T13:55:05Z</dc:date>
    </item>
  </channel>
</rss>

