<?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 fill in the gaps between sequential in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/fill-in-the-gaps-between-sequential/m-p/486355#M126535</link>
    <description>&lt;P&gt;Hi SAS expert,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a sequential data set, each id have two observations,&amp;nbsp;a sample dataset&amp;nbsp;looks like below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input id month var1;
	datalines;
	1 6  2  
	1 14 1 
	2 9  3 
	2 14 1 
	;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;First I would like to calculate the change of var1,&amp;nbsp; var1_change, defined as the last obs minus the first obs, and calculate the change per month,var1_change_per_month, defined as the change/interval, the dataset I got looks like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data cal;
	input id month var1 var1_change interval var1_change_per_month;
	datalines;
	1 6 2 . . .
	1 14 1 -1 8 -0.125
	2 9 3 . . .
	2 14 1 -2 5 -0.4
	;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Now I would like to fill in the gaps between two observations with the calculated&amp;nbsp;&lt;SPAN&gt;var1_change_per_month, the dataset i want would look like this:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	input id month var1_change_per_month;
	datalines;
	1 6 -0.125
	1 7 -0.125
	1 8 -0.125
	1 9 -0.125
	1 10 -0.125
	1 11 -0.125
	1 12 -0.125
	1 13 -0.125
	1 14 -0.125
	2 9 -0.4
	2 10 -0.4
	2 11 -0.4
	2 12 -0.4
	2 13 -0.4
	2 14 -0.4
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;Any ideas would be appreciated. Thanks!&lt;/SPAN&gt;&lt;/P&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;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 13 Aug 2018 14:25:06 GMT</pubDate>
    <dc:creator>ncy</dc:creator>
    <dc:date>2018-08-13T14:25:06Z</dc:date>
    <item>
      <title>fill in the gaps between sequential</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fill-in-the-gaps-between-sequential/m-p/486355#M126535</link>
      <description>&lt;P&gt;Hi SAS expert,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a sequential data set, each id have two observations,&amp;nbsp;a sample dataset&amp;nbsp;looks like below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input id month var1;
	datalines;
	1 6  2  
	1 14 1 
	2 9  3 
	2 14 1 
	;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;First I would like to calculate the change of var1,&amp;nbsp; var1_change, defined as the last obs minus the first obs, and calculate the change per month,var1_change_per_month, defined as the change/interval, the dataset I got looks like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data cal;
	input id month var1 var1_change interval var1_change_per_month;
	datalines;
	1 6 2 . . .
	1 14 1 -1 8 -0.125
	2 9 3 . . .
	2 14 1 -2 5 -0.4
	;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Now I would like to fill in the gaps between two observations with the calculated&amp;nbsp;&lt;SPAN&gt;var1_change_per_month, the dataset i want would look like this:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	input id month var1_change_per_month;
	datalines;
	1 6 -0.125
	1 7 -0.125
	1 8 -0.125
	1 9 -0.125
	1 10 -0.125
	1 11 -0.125
	1 12 -0.125
	1 13 -0.125
	1 14 -0.125
	2 9 -0.4
	2 10 -0.4
	2 11 -0.4
	2 12 -0.4
	2 13 -0.4
	2 14 -0.4
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;Any ideas would be appreciated. Thanks!&lt;/SPAN&gt;&lt;/P&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;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Aug 2018 14:25:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fill-in-the-gaps-between-sequential/m-p/486355#M126535</guid>
      <dc:creator>ncy</dc:creator>
      <dc:date>2018-08-13T14:25:06Z</dc:date>
    </item>
    <item>
      <title>Re: fill in the gaps between sequential</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fill-in-the-gaps-between-sequential/m-p/486360#M126537</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/214082"&gt;@ncy&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a sequential data set&lt;STRONG&gt;, each id have &lt;U&gt;two observations&lt;/U&gt;&lt;/STRONG&gt;,&amp;nbsp;a sample dataset&amp;nbsp;looks like below:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input id month var1;
	datalines;
	1 6  2  
	1 14 1 
	2 9  3 
	2 14 1 
	;
run;

data want;
do _n_=1 by 1 until(last.id);
set have;
by id;
if _n_=1 then do;
k=2;
set have(keep=month var1 rename=(var1=_var1 month=_m)) point=k;
var1_change_per_month=(_var1-var1)/(_m-month);
_m=month;
output;
end;
else do month=_m+1 to month;
output;
end;
end;
keep id month var1_change_per_month;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 13 Aug 2018 14:41:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fill-in-the-gaps-between-sequential/m-p/486360#M126537</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-08-13T14:41:02Z</dc:date>
    </item>
    <item>
      <title>Re: fill in the gaps between sequential</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fill-in-the-gaps-between-sequential/m-p/486370#M126541</link>
      <description>&lt;P&gt;Have you tried anything?&lt;/P&gt;
&lt;P&gt;Do you have to calculate the difference of var1 only, or are there more variables in your data? &lt;EM&gt;Assuming: only one variable.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;Are there always exactly two observation per id?&amp;nbsp;&amp;nbsp; &lt;EM&gt;Assuming: yes&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;Is the data always sorted? &lt;EM&gt;Assuming: yes&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;Do you need the dataset "cal" for anything or could we jump from "have" to "want" without any intermediate steps? &lt;EM&gt;Assuming: yes&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is a clumsy two-step solution, the second step could be integrated into the first one.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data cal;
   set have;
   by id;

   length 
      var1_change interval var1_change_per_month 8
      last_month last_var1 8
   ;
   retain last:;
   drop last:;

   if first.id then do;
      last_month = month;
      last_var1 = var1;
   end;
   else do;
      var1_change = var1 - last_var1;
      interval = month - last_month;
      var1_change_per_month = var1_change / interval;
   end;
run;

data want;
   set cal(keep=id month interval var1_change_per_month);
   by id;

   length start_month end_month 8;
   drop start_month end_month interval;

   if last.id;

   start_month = month - interval;
   end_month = month;

   do month = start_month to end_month;
      output;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 13 Aug 2018 14:54:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fill-in-the-gaps-between-sequential/m-p/486370#M126541</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2018-08-13T14:54:54Z</dc:date>
    </item>
    <item>
      <title>Re: fill in the gaps between sequential</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fill-in-the-gaps-between-sequential/m-p/486371#M126542</link>
      <description>&lt;P&gt;No matter what you have in mind, you will need 2xpass to the data. So 2XDOW seems logical, and it can deal more than 2 rows per id.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input id month var1;
	datalines;
	1 6  2  
	1 14 1 
	2 9  3 
	2 14 1 
	;
run;

data want;
	do until (last.id);
		set have;
		by id;

		if first.id then
			do;
				_from=month;
				_d1=var1;
			end;

		if last.id then
			do;
				_to=month;
				_d2=var1;
			end;
end;
		RateOfChange=(_d2-_d1)/(_to-_from);

		do until (last.id);
			set have;
			by id;

			if first.id then
				do;
					do month=_from to _to;
						output;
					end;
				end;
		end;
		keep id month RateOfChange;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 13 Aug 2018 14:57:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fill-in-the-gaps-between-sequential/m-p/486371#M126542</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2018-08-13T14:57:57Z</dc:date>
    </item>
    <item>
      <title>Re: fill in the gaps between sequential</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fill-in-the-gaps-between-sequential/m-p/486375#M126543</link>
      <description>&lt;P&gt;Well, had to take my word back. &amp;nbsp;One don't seem to need 2 pass if it is fixed number of rows per ID. So for instance 2rows/id:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input id month var1;
	datalines;
	1 6  2  
	1 14 1 
	2 9  3 
	2 14 1 
	;
run;

data want;
  set have;
   by id;
    _dm=dif(month);
	_dv=dif(var1);
	if last.id then do;
	   RateOfChange=_dv/_dm;
	   do month=month-_dm to month;
	     output;
	   end;
	   end;
	   keep id month RateOfChange;
	run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 13 Aug 2018 15:08:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fill-in-the-gaps-between-sequential/m-p/486375#M126543</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2018-08-13T15:08:31Z</dc:date>
    </item>
    <item>
      <title>Re: fill in the gaps between sequential</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fill-in-the-gaps-between-sequential/m-p/486381#M126544</link>
      <description>&lt;P&gt;If it were up to me ....&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;by id;&lt;/P&gt;
&lt;P&gt;interval = dif(month);&lt;/P&gt;
&lt;P&gt;var1_change = dif(var1);&lt;/P&gt;
&lt;P&gt;if last.id;&lt;/P&gt;
&lt;P&gt;var1_change_per_month = var1_change / interval;&lt;/P&gt;
&lt;P&gt;do month = month - interval to month;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; output;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;keep id month var1_change_per_month;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With apologies to&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4877"&gt;@Haikuo&lt;/a&gt;&amp;nbsp;since I belatedly noticed how similar this is to your solution!&lt;/P&gt;</description>
      <pubDate>Mon, 13 Aug 2018 15:19:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fill-in-the-gaps-between-sequential/m-p/486381#M126544</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-08-13T15:19:33Z</dc:date>
    </item>
    <item>
      <title>Re: fill in the gaps between sequential</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fill-in-the-gaps-between-sequential/m-p/486423#M126561</link>
      <description>&lt;P&gt;If you do a "look ahead" by using a self-merge with firstobs=2 you can accommodate more than one record-per-id in a single data step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input id month var1;
	datalines;
	1 6  2  
	1 14 1 
	2 9  3 
	2 14 1 
	;
run;

data want (drop=_: var1);
  set have;
  by id;
  set have (firstobs=2 keep=month var1 rename=(month=_nxt_month var1=_nxt_var1));

  var1_change_per_month=(_nxt_var1-var1)/(_nxt_month-month);
  if not last.id then do month=month to _nxt_month;
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now I wrote "merge" even though the program doesn't use a MERGE statement.&amp;nbsp; Instead it uses two SET's, one starting out at the second observation.&amp;nbsp;&amp;nbsp; Ordinarily this technique would be a problem because when the first SET reads the last obs, the second SET attempts to read beyond the last obs, causing the data step to immediately stop.&amp;nbsp;&amp;nbsp; For this task, however, that's just fine.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The reason I chose to use two SETs instead of a MERGE is to take advantage of the BY statement, which of course allows use of the "if not last.id" test.&amp;nbsp; A merge with BY would not preserve the offset initially provided by firstobs=2.&lt;/P&gt;</description>
      <pubDate>Mon, 13 Aug 2018 17:36:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fill-in-the-gaps-between-sequential/m-p/486423#M126561</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-08-13T17:36:00Z</dc:date>
    </item>
    <item>
      <title>Re: fill in the gaps between sequential</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fill-in-the-gaps-between-sequential/m-p/486432#M126565</link>
      <description>&lt;P&gt;Thanks andreas_ids. the solution works very well.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To clarify your assumptions.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yes, there are exactly two observations per id, and i have more than var1 to calculate. But I can apply this technique to other variables. The 'cal' is a dataset I already got, I put this intermediate dataset here to help formulate my question about getting dataset 'want'.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Aug 2018 18:42:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fill-in-the-gaps-between-sequential/m-p/486432#M126565</guid>
      <dc:creator>ncy</dc:creator>
      <dc:date>2018-08-13T18:42:04Z</dc:date>
    </item>
    <item>
      <title>Re: fill in the gaps between sequential</title>
      <link>https://communities.sas.com/t5/SAS-Programming/fill-in-the-gaps-between-sequential/m-p/486441#M126569</link>
      <description>&lt;P&gt;Thanks mkeintz for the solution and explanation. A good technique!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Aug 2018 19:14:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/fill-in-the-gaps-between-sequential/m-p/486441#M126569</guid>
      <dc:creator>ncy</dc:creator>
      <dc:date>2018-08-13T19:14:40Z</dc:date>
    </item>
  </channel>
</rss>

