<?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: create time stop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/create-time-stop/m-p/540772#M149229</link>
    <description>&lt;P&gt;I think this is what you're asking for, but I'm not sure it's what you really want:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	length id k date 8.; 
	format date mmddyy10.;
	do id = 1 to 2;
		do k = 1 to 4;
			date = '06MAR2019'd + (k*k);
			output;
		end;
	end;
run;
data temp;
	set have;
	by id notsorted;
	format prev_date mmddyy10.;
	prev_date= lag(date);

	if first.id then call missing(prev_date);
	diff=date - prev_date;
run;

*now scoot all the diff's up a line. 
What do you want to do with the final diff per id?;
data want(rename=(temp_id = id temp_k=k temp_date=date));
	set temp;
	length temp_id temp_k temp_date 8.;
	format temp_date mmddyy10.;
	retain temp_id temp_k temp_date;

	if not missing(diff) then output;

	temp_id = id;
	temp_k= k;
	temp_date = date;
	keep diff temp_id temp_k temp_date;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Creates:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 398px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/27699iA378873A8A8332A9/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;So here, diff is the difference between (k+1) - k. Is this what you need? I would think through your problem before copying this logic.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 06 Mar 2019 13:57:38 GMT</pubDate>
    <dc:creator>noling</dc:creator>
    <dc:date>2019-03-06T13:57:38Z</dc:date>
    <item>
      <title>create time stop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-time-stop/m-p/540637#M149176</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a longitudinal dataset with many ED (emergency department) visits per individual (id). Each visit has an index date (begdate) and the variable&amp;nbsp;year. I am trying to model a cox for recurrent&amp;nbsp;events creating time0 (start) and time2 (stop).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The main variables&amp;nbsp;are:&lt;/P&gt;&lt;P&gt;id: individuals&lt;/P&gt;&lt;P&gt;k: observations&amp;nbsp;or visits&lt;/P&gt;&lt;P&gt;begdate&amp;nbsp;: date of the observations - visit the ED&lt;/P&gt;&lt;P&gt;year : calendar year&amp;nbsp;&lt;/P&gt;&lt;P&gt;freq_user_y : 4 visits or more to the ED a year&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I created the difference between the observations and the lag time but it turns out the observations k=1 are being excluded&amp;nbsp;because they do not have a time2. And the lag time has been created just for the observations k &amp;gt;=&amp;nbsp;2 .&lt;/P&gt;&lt;P&gt;I wonder how I can create a time 2 for the first observation then the difference between time 1 and 2 (t2-t1) be kept in this line (k=1), not in the second visit (k=2) and so on, time difference t3-t2 in line k=2, not in k=3...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Look forward to hearing from you.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Tomas&lt;/P&gt;</description>
      <pubDate>Wed, 06 Mar 2019 03:39:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-time-stop/m-p/540637#M149176</guid>
      <dc:creator>tomas-andriotti</dc:creator>
      <dc:date>2019-03-06T03:39:52Z</dc:date>
    </item>
    <item>
      <title>Re: create time stop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-time-stop/m-p/540772#M149229</link>
      <description>&lt;P&gt;I think this is what you're asking for, but I'm not sure it's what you really want:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	length id k date 8.; 
	format date mmddyy10.;
	do id = 1 to 2;
		do k = 1 to 4;
			date = '06MAR2019'd + (k*k);
			output;
		end;
	end;
run;
data temp;
	set have;
	by id notsorted;
	format prev_date mmddyy10.;
	prev_date= lag(date);

	if first.id then call missing(prev_date);
	diff=date - prev_date;
run;

*now scoot all the diff's up a line. 
What do you want to do with the final diff per id?;
data want(rename=(temp_id = id temp_k=k temp_date=date));
	set temp;
	length temp_id temp_k temp_date 8.;
	format temp_date mmddyy10.;
	retain temp_id temp_k temp_date;

	if not missing(diff) then output;

	temp_id = id;
	temp_k= k;
	temp_date = date;
	keep diff temp_id temp_k temp_date;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Creates:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 398px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/27699iA378873A8A8332A9/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;So here, diff is the difference between (k+1) - k. Is this what you need? I would think through your problem before copying this logic.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Mar 2019 13:57:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-time-stop/m-p/540772#M149229</guid>
      <dc:creator>noling</dc:creator>
      <dc:date>2019-03-06T13:57:38Z</dc:date>
    </item>
    <item>
      <title>Re: create time stop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-time-stop/m-p/540791#M149231</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I ran the command and it created another table with the difference in line 1 for all individuals even&amp;nbsp;the ones&amp;nbsp;with no second&amp;nbsp;observation because there are individuals with no k2. So, I do have a dataset have and what&amp;nbsp;I need is to scoot up the diff only for those individuals with a recurrent event ( k&amp;gt;2) those with no second event should not have a difference because they were not observed or followed up to a second time.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Look forward to hearing from you.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Tomas&lt;/P&gt;</description>
      <pubDate>Wed, 06 Mar 2019 14:46:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-time-stop/m-p/540791#M149231</guid>
      <dc:creator>tomas-andriotti</dc:creator>
      <dc:date>2019-03-06T14:46:21Z</dc:date>
    </item>
    <item>
      <title>Re: create time stop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-time-stop/m-p/540798#M149232</link>
      <description>&lt;P&gt;Ok, so only scooting up records that have more than 1 ID records. I changed my sample data and added a "BY" statement and second output condition for single ID records:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	length id k date 8.; 
	format date mmddyy10.;
	id=1;
		do k = 1 to 4;
			date = '06MAR2019'd + (k*k);
			output;
		end;
	k=1;date = '06MAR2019'd;
	id=2; output;
	id=3; output;

run;
data temp;
	set have;
	by id notsorted;
	format prev_date mmddyy10.;
	prev_date= lag(date);

	if first.id then call missing(prev_date);
	diff=date - prev_date;
run;

*now scoot ONLY the diff's up a line if their id is in more than one row.
Output the single ID records; 
data want(rename=(temp_id = id temp_k=k temp_date=date));
	set temp;
	by id;
	length temp_id temp_k temp_date 8.;
	format temp_date mmddyy10.;
	retain temp_id temp_k temp_date;

	if not missing(diff) then output;

	temp_id = id;
	temp_k= k;
	temp_date = date;
	keep diff temp_id temp_k temp_date;
	
	*output again for single records with only 1 ID row;
	if first.id and last.id then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Yields:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/27703iA1C9416750FD1E9C/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Mar 2019 15:03:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-time-stop/m-p/540798#M149232</guid>
      <dc:creator>noling</dc:creator>
      <dc:date>2019-03-06T15:03:01Z</dc:date>
    </item>
    <item>
      <title>Re: create time stop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-time-stop/m-p/540803#M149234</link>
      <description>Hey thanks !&lt;BR /&gt;I believe it worked. I just have to merge table want with the dataset I have.&lt;BR /&gt;Best,&lt;BR /&gt;Tomas</description>
      <pubDate>Wed, 06 Mar 2019 15:24:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-time-stop/m-p/540803#M149234</guid>
      <dc:creator>tomas-andriotti</dc:creator>
      <dc:date>2019-03-06T15:24:23Z</dc:date>
    </item>
  </channel>
</rss>

