<?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 show missing period in the time series data by Id? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-show-missing-period-in-the-time-series-data-by-Id/m-p/818284#M323001</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines expandtabs;
input ID_loan $ Period :yymmn6.;
format period yymmn6.;
datalines;
A0001	201402
A0001	201403
A0001	201408
A0002	201408
A0002	201409
A0002	201410
A0003	201406
A0003	201408
A0003	201409
A0004	201405
;

proc summary data=have nway;
class ID_loan;
var Period;
output out=temp min=min max=max;
run;
data temp1;
 set temp(drop=_:);
 do Period= min  to max;
  if month ne month(Period) then output;
  month=month(Period);
 end;
format Period yymmn6.;
drop month min max;
run;
data temp2;
 merge have(in=ina) temp1;
 by ID_loan Period;
 if not ina;
run;
data temp3;
 set temp2;
 by ID_loan;
 if first.ID_loan or intck('month',lag(Period),Period)&amp;gt;1 then group+1;
 run;
 proc summary data=temp3 nway;
 class ID_loan group;
 var Period;
 output out=want(drop=_:) min=start_date max=end_date;
 run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 15 Jun 2022 12:33:39 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2022-06-15T12:33:39Z</dc:date>
    <item>
      <title>How to show missing period in the time series data by Id?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-show-missing-period-in-the-time-series-data-by-Id/m-p/818265#M322989</link>
      <description>&lt;P&gt;Hi everyone,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can't find any materials like this, so I need your help badly.&lt;/P&gt;
&lt;P&gt;I want to find the start date and end date by ID if there are missing periods.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A sample of data is below.&lt;/P&gt;
&lt;TABLE border="1" width="21.02194393382353%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="12.365101079191367%" height="30px"&gt;ID_loan&lt;/TD&gt;
&lt;TD width="15.194811774839136%" height="30px"&gt;Period&lt;/TD&gt;
&lt;TD width="2.1786492374727686%" height="30px"&gt;...&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="12.365101079191367%" height="30px"&gt;A0001&lt;/TD&gt;
&lt;TD width="15.194811774839136%" height="30px"&gt;201402&lt;/TD&gt;
&lt;TD width="2.1786492374727686%" height="30px"&gt;...&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="12.365101079191367%" height="30px"&gt;A0001&lt;/TD&gt;
&lt;TD width="15.194811774839136%" height="30px"&gt;201403&lt;/TD&gt;
&lt;TD width="2.1786492374727686%" height="30px"&gt;...&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="12.365101079191367%" height="30px"&gt;A0001&lt;/TD&gt;
&lt;TD width="15.194811774839136%" height="30px"&gt;201408&lt;/TD&gt;
&lt;TD width="2.1786492374727686%" height="30px"&gt;...&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="12.365101079191367%" height="30px"&gt;A0002&lt;/TD&gt;
&lt;TD width="15.194811774839136%" height="30px"&gt;201408&lt;/TD&gt;
&lt;TD width="2.1786492374727686%" height="30px"&gt;...&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="12.365101079191367%" height="30px"&gt;A0002&lt;/TD&gt;
&lt;TD width="15.194811774839136%" height="30px"&gt;201409&lt;/TD&gt;
&lt;TD width="2.1786492374727686%" height="30px"&gt;...&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="12.365101079191367%" height="30px"&gt;A0002&lt;/TD&gt;
&lt;TD width="15.194811774839136%" height="30px"&gt;201410&lt;/TD&gt;
&lt;TD width="2.1786492374727686%" height="30px"&gt;...&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="12.365101079191367%" height="30px"&gt;A0003&lt;/TD&gt;
&lt;TD width="15.194811774839136%" height="30px"&gt;201406&lt;/TD&gt;
&lt;TD width="2.1786492374727686%" height="30px"&gt;...&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="12.365101079191367%" height="30px"&gt;A0003&lt;/TD&gt;
&lt;TD width="15.194811774839136%" height="30px"&gt;201408&lt;/TD&gt;
&lt;TD width="2.1786492374727686%" height="30px"&gt;...&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="12.365101079191367%" height="30px"&gt;A0003&lt;/TD&gt;
&lt;TD width="15.194811774839136%" height="30px"&gt;201409&lt;/TD&gt;
&lt;TD width="2.1786492374727686%" height="30px"&gt;...&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="12.365101079191367%"&gt;A0004&lt;/TD&gt;
&lt;TD width="15.194811774839136%"&gt;201405&lt;/TD&gt;
&lt;TD width="2.1786492374727686%"&gt;...&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="12.365101079191367%"&gt;...&lt;/TD&gt;
&lt;TD width="15.194811774839136%"&gt;...&lt;/TD&gt;
&lt;TD width="2.1786492374727686%"&gt;...&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And I want to get the result like this.&lt;/P&gt;
&lt;TABLE border="1" width="30.628638174019606%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="4%" height="30px"&gt;ID_loan&lt;/TD&gt;
&lt;TD width="8.833333333333327%" height="30px"&gt;Start_Date&lt;/TD&gt;
&lt;TD width="17.795329570942755%" height="30px"&gt;End_Date&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="4%" height="30px"&gt;A0001&lt;/TD&gt;
&lt;TD width="8.833333333333327%" height="30px"&gt;201404&lt;/TD&gt;
&lt;TD width="17.795329570942755%" height="30px"&gt;201407&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="4%" height="30px"&gt;A0003&lt;/TD&gt;
&lt;TD width="8.833333333333327%" height="30px"&gt;201407&lt;/TD&gt;
&lt;TD width="17.795329570942755%" height="30px"&gt;201407&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="4%" height="30px"&gt;...&lt;/TD&gt;
&lt;TD width="8.833333333333327%" height="30px"&gt;...&lt;/TD&gt;
&lt;TD width="17.795329570942755%" height="30px"&gt;...&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is it possible to make this?&lt;/P&gt;
&lt;P&gt;Hopefully, I can get any help.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I really appreciate any help you can provide.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jun 2022 09:54:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-show-missing-period-in-the-time-series-data-by-Id/m-p/818265#M322989</guid>
      <dc:creator>seohyeonjeong</dc:creator>
      <dc:date>2022-06-15T09:54:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to show missing period in the time series data by Id?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-show-missing-period-in-the-time-series-data-by-Id/m-p/818269#M322990</link>
      <description>&lt;P&gt;Your sample data is not in the form of a working data step, so this program code is untested.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming the PERIOD variable is a sas date variable aligned to the beginning of each month, then:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (keep=ID_LOAN START_DATE END_DATE);
  set have;
  by id_loan;
  start_date=intnx('month',lag(period),1);
  if first.id=0 and start_date^=period then do;
    end_date=intnx('month',period,-1);
    output;
  end;
  format start_date end_date yymmn6. ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If period is really just a 6-digit number, then you could insert&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  _period_date=input(put(period,6.),yymmn6.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;just after the BY statement.&amp;nbsp; And then use &lt;EM&gt;&lt;STRONG&gt;_period_date&lt;/STRONG&gt;&lt;/EM&gt; instead of &lt;EM&gt;&lt;STRONG&gt;period&lt;/STRONG&gt;&lt;/EM&gt; in the subsequent statements.&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jun 2022 10:26:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-show-missing-period-in-the-time-series-data-by-Id/m-p/818269#M322990</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-06-15T10:26:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to show missing period in the time series data by Id?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-show-missing-period-in-the-time-series-data-by-Id/m-p/818270#M322991</link>
      <description>&lt;P&gt;Use the LAG function to retrieve the value from the preceding observation, and once you are at the second or more observation of a group, compare with the current observation and write a record if needed. Store periods as SAS dates, so you can make use of interval functions.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines dlm="09"x;
input ID_loan $ Period :yymmn6.;
format period yymmn6.;
datalines;
A0001	201402
A0001	201403
A0001	201408
A0002	201408
A0002	201409
A0002	201410
A0003	201406
A0003	201408
A0003	201409
A0004	201405
;

data want;
set have;
by id_loan;
format start_date end_date yymmn6.;
l_period = lag(period);
if not first.id_loan and intck('month',l_period,period) &amp;gt; 1
then do;
  start_date = intnx('month',l_period,1);
  end_date = intnx('month',period,-1);
  output;
end;
keep id_loan start_date end_date;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Jun 2022 10:32:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-show-missing-period-in-the-time-series-data-by-Id/m-p/818270#M322991</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-06-15T10:32:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to show missing period in the time series data by Id?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-show-missing-period-in-the-time-series-data-by-Id/m-p/818271#M322992</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I would use PROC TIMEDATA !&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.have0;
input ID_loan $ Period $;
cards;
A0001	201402
A0001	201403
A0001	201408
A0002	201408
A0002	201409
A0002	201410
A0003	201406
A0003	201408
A0003	201409
A0004	201405
;
run;

data work.have1;
 set work.have0;
 Period_num = input(put(Period,$6.)!!'01',yymmdd8.);
 format Period_num date9.;
 item1 = 1234 ;
run;

proc timedata data=have1 out=_NULL_ outarray=work.want;
   by ID_loan;
   id Period_num interval=month
                 accumulate=median
                 setmiss=missing;
   var item1;
   outarray itemshare;
   do i=1 to _length_;
      if item1[i]=. then itemshare[i] = 1;
	  else itemshare[i] = 0;
   end;
run;

PROC MEANS data=work.want MIN MAX nway;
 CLASS ID_loan;
 var Period_num;
 format Period_num date9.;
 output out=work.want_encore min= max= / autoname autolabel;
run;

proc print data=work.want_encore;
run;
/* end of program */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jun 2022 10:38:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-show-missing-period-in-the-time-series-data-by-Id/m-p/818271#M322992</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2022-06-15T10:38:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to show missing period in the time series data by Id?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-show-missing-period-in-the-time-series-data-by-Id/m-p/818284#M323001</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines expandtabs;
input ID_loan $ Period :yymmn6.;
format period yymmn6.;
datalines;
A0001	201402
A0001	201403
A0001	201408
A0002	201408
A0002	201409
A0002	201410
A0003	201406
A0003	201408
A0003	201409
A0004	201405
;

proc summary data=have nway;
class ID_loan;
var Period;
output out=temp min=min max=max;
run;
data temp1;
 set temp(drop=_:);
 do Period= min  to max;
  if month ne month(Period) then output;
  month=month(Period);
 end;
format Period yymmn6.;
drop month min max;
run;
data temp2;
 merge have(in=ina) temp1;
 by ID_loan Period;
 if not ina;
run;
data temp3;
 set temp2;
 by ID_loan;
 if first.ID_loan or intck('month',lag(Period),Period)&amp;gt;1 then group+1;
 run;
 proc summary data=temp3 nway;
 class ID_loan group;
 var Period;
 output out=want(drop=_:) min=start_date max=end_date;
 run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Jun 2022 12:33:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-show-missing-period-in-the-time-series-data-by-Id/m-p/818284#M323001</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-06-15T12:33:39Z</dc:date>
    </item>
  </channel>
</rss>

