<?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 compute time distance for longitudinal data in SAS in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/How-to-compute-time-distance-for-longitudinal-data-in-SAS/m-p/511162#M2124</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data origin;
	input ID Time;
	cards;
		12	3
		12	5
		12	18
		16	7
		16	25
		16	28
		16	44
	;
run;

data want;
set origin;
by id;
 dist=dif(time);
 if first.id then dist=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 07 Nov 2018 19:36:02 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2018-11-07T19:36:02Z</dc:date>
    <item>
      <title>How to compute time distance for longitudinal data in SAS</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-compute-time-distance-for-longitudinal-data-in-SAS/m-p/511157#M2122</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a longitudinal data. Each subject is repeatedly measured over time:&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;data origin;
	input ID Time;
	cards;
		12	3
		12	5
		12	18
		16	7
		16	25
		16	28
		16	44
	;
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;I would like to compute time distance. For the first measurement, time distance is set at 0. From the second time point, time distance is computed by subtracting the previous time from the current time. Time distance is set back to 0 when ID goes to the next one.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have tried a program, it works.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=origin out=origin_sort;
	by ID Time;
run;

data distance;
	set origin_sort;
	retain distance previous;
	by ID;
	if first.ID=1 then
		do;
			distance=0;
			previous=time;
		end;
	else do;
			distance=Time-previous;
			previous=previous+distance;
		end;
run;

proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I obtain the output as I want.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SAS output.png" style="width: 235px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/24743iC39F5BC3AA8733D6/image-size/large?v=v2&amp;amp;px=999" role="button" title="SAS output.png" alt="SAS output.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, I have to use two variables, and &lt;STRONG&gt;previous&lt;/STRONG&gt; is exactly the same as &lt;STRONG&gt;Time&lt;/STRONG&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do you think this is a best way to do or could you suggest some&amp;nbsp;way better?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for reading.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;P/S: I cannot copy and paste table into this question so I have to use image. I do not know how to use table here. Could you please guide me?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Nov 2018 19:32:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-compute-time-distance-for-longitudinal-data-in-SAS/m-p/511157#M2122</guid>
      <dc:creator>trungdungtran</dc:creator>
      <dc:date>2018-11-07T19:32:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to compute time distance for longitudinal data in SAS</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-compute-time-distance-for-longitudinal-data-in-SAS/m-p/511162#M2124</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data origin;
	input ID Time;
	cards;
		12	3
		12	5
		12	18
		16	7
		16	25
		16	28
		16	44
	;
run;

data want;
set origin;
by id;
 dist=dif(time);
 if first.id then dist=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 07 Nov 2018 19:36:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-compute-time-distance-for-longitudinal-data-in-SAS/m-p/511162#M2124</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-11-07T19:36:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to compute time distance for longitudinal data in SAS</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-compute-time-distance-for-longitudinal-data-in-SAS/m-p/511166#M2125</link>
      <description>&lt;P&gt;or&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set origin;
by id;
 dist=ifn(first.id,0,dif(time));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 07 Nov 2018 19:40:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-compute-time-distance-for-longitudinal-data-in-SAS/m-p/511166#M2125</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-11-07T19:40:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to compute time distance for longitudinal data in SAS</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-compute-time-distance-for-longitudinal-data-in-SAS/m-p/511188#M2129</link>
      <description>Thank you so much for letting me know a new function in SAS.</description>
      <pubDate>Wed, 07 Nov 2018 20:41:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-compute-time-distance-for-longitudinal-data-in-SAS/m-p/511188#M2129</guid>
      <dc:creator>trungdungtran</dc:creator>
      <dc:date>2018-11-07T20:41:13Z</dc:date>
    </item>
  </channel>
</rss>

