<?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: Calculate time difference when certain requirement met in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculate-time-difference-when-certain-requirement-met/m-p/661939#M197834</link>
    <description>&lt;P&gt;See if this gets you started.&lt;/P&gt;
&lt;PRE&gt;data have;
   input ID $  Date :mmddyy10.  FirstPositive;
   format date mmddyy10.;
datalines;
A      1/1/2020        .    
A      1/3/2020      1     
A      1/4/2020       .    
A      1/5/2020       .    
B      1/4/2020      1     
B      1/5/2020       .    
;

data want;
   set have;
   by id;
   retain count  posflag  ;
   difdate= dif(date);
   if first.id then call missing(count,posflag);
   if posflag then diff=difdate;
   if firstPositive=1 then posflag=1;
   if posflag then count+1;
   drop posflag difdate;
run;&lt;/PRE&gt;
&lt;P&gt;You may have to sort your data set by ID and Date prior to the Want data set.&lt;/P&gt;
&lt;P&gt;If you have not seen these functions before:&lt;/P&gt;
&lt;P&gt;Retain keeps variable values from iteration of the data step to the next.&lt;/P&gt;
&lt;P&gt;DIF is a function that returns the current value of a variable minus the previous value.&lt;/P&gt;
&lt;P&gt;When using BY statement SAS creates automatic variables First. and Last. that indicate whether the current is the first or last that level of a by variable.&lt;/P&gt;
&lt;P&gt;Call missing is a function that can set a number of variables to missing values.&lt;/P&gt;
&lt;P&gt;Timing of calculations is the main part of this problem with the when to set the diff value in relation to the iteration of the count.&lt;/P&gt;</description>
    <pubDate>Wed, 17 Jun 2020 22:31:28 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2020-06-17T22:31:28Z</dc:date>
    <item>
      <title>Calculate time difference when certain requirement met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-time-difference-when-certain-requirement-met/m-p/661932#M197831</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I got stuck in the process to calculate the difference between two dates under a specific requirement. My data looks like below (Diff and Count are the variables that I desire to have)&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp;Date&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; FirstPositive&amp;nbsp; Diff&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Count&lt;/P&gt;&lt;P&gt;A&amp;nbsp; &amp;nbsp; &amp;nbsp;1/1/2020&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;&lt;P&gt;A&amp;nbsp; &amp;nbsp; &amp;nbsp; 1/3/2020&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp;&lt;/P&gt;&lt;P&gt;A&amp;nbsp; &amp;nbsp; &amp;nbsp; 1/4/2020&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;A&amp;nbsp; &amp;nbsp; &amp;nbsp; 1/5/2020&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/P&gt;&lt;P&gt;B&amp;nbsp; &amp;nbsp; &amp;nbsp; 1/4/2020&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;B&amp;nbsp; &amp;nbsp; &amp;nbsp; 1/5/2020&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;I want to calculate the difference between dates for each ID, but start from the row when FirstPositive =1. I also would like to have the count variable which count the number of rows for each ID start from&amp;nbsp;the row when FirstPositive =1.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any advice would be appreciated!&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jun 2020 22:06:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-time-difference-when-certain-requirement-met/m-p/661932#M197831</guid>
      <dc:creator>huhuhu</dc:creator>
      <dc:date>2020-06-17T22:06:14Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate time difference when certain requirement met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-time-difference-when-certain-requirement-met/m-p/661938#M197833</link>
      <description>&lt;P&gt;Please try the below code&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID$ Date:mmddyy10. FirstPositive;
cards;
A 1/1/2020 .         
A 1/3/2020 1          
A 1/4/2020 .         
A 1/5/2020 .         
B 1/4/2020 1          
B 1/5/2020 .        
;
 
data want;
set have;
by id notsorted;
retain FirstPositive2;
if first.id then do;FirstPositive2=.;count=.;end;
if FirstPositive ne . then FirstPositive2=FirstPositive;
count+FirstPositive2;
if FirstPositive2 ne . then diff=Date-lag(date);
if first.id then diff=.;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 17 Jun 2020 22:29:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-time-difference-when-certain-requirement-met/m-p/661938#M197833</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2020-06-17T22:29:20Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate time difference when certain requirement met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-time-difference-when-certain-requirement-met/m-p/661939#M197834</link>
      <description>&lt;P&gt;See if this gets you started.&lt;/P&gt;
&lt;PRE&gt;data have;
   input ID $  Date :mmddyy10.  FirstPositive;
   format date mmddyy10.;
datalines;
A      1/1/2020        .    
A      1/3/2020      1     
A      1/4/2020       .    
A      1/5/2020       .    
B      1/4/2020      1     
B      1/5/2020       .    
;

data want;
   set have;
   by id;
   retain count  posflag  ;
   difdate= dif(date);
   if first.id then call missing(count,posflag);
   if posflag then diff=difdate;
   if firstPositive=1 then posflag=1;
   if posflag then count+1;
   drop posflag difdate;
run;&lt;/PRE&gt;
&lt;P&gt;You may have to sort your data set by ID and Date prior to the Want data set.&lt;/P&gt;
&lt;P&gt;If you have not seen these functions before:&lt;/P&gt;
&lt;P&gt;Retain keeps variable values from iteration of the data step to the next.&lt;/P&gt;
&lt;P&gt;DIF is a function that returns the current value of a variable minus the previous value.&lt;/P&gt;
&lt;P&gt;When using BY statement SAS creates automatic variables First. and Last. that indicate whether the current is the first or last that level of a by variable.&lt;/P&gt;
&lt;P&gt;Call missing is a function that can set a number of variables to missing values.&lt;/P&gt;
&lt;P&gt;Timing of calculations is the main part of this problem with the when to set the diff value in relation to the iteration of the count.&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jun 2020 22:31:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-time-difference-when-certain-requirement-met/m-p/661939#M197834</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-06-17T22:31:28Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate time difference when certain requirement met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-time-difference-when-certain-requirement-met/m-p/661946#M197840</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sample(Drop = diff count);
informat ID $3. Date mmddyy10. FirstPositive Diff Count 3.;
input ID $3. Date FirstPositive Diff Count;
format date mmddyy10.;
datalines;
A 1/1/2020 . . .
A 1/3/2020 1 . 1 
A 1/4/2020 . 1 2
A 1/5/2020 . 1 3
B 1/4/2020 1 . 1
B 1/5/2020 . 1 2
;
proc sort; by ID Date; run;

data sample(drop=prv_date);
set sample;
by id;
retain count Diff prv_date 0;
if first.id then do;
count=.; Diff=.;
prv_date = .;
end;

if FirstPositive = 1 then do;
count=1;
prv_date = Date;
end;
else if count &amp;gt; 0 then do;
count = count+1;
Diff = Date-prv_date;
prv_date=date;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 17 Jun 2020 22:59:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-time-difference-when-certain-requirement-met/m-p/661946#M197840</guid>
      <dc:creator>smantha</dc:creator>
      <dc:date>2020-06-17T22:59:39Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate time difference when certain requirement met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-time-difference-when-certain-requirement-met/m-p/661952#M197844</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/79834"&gt;@huhuhu&lt;/a&gt;&amp;nbsp; Your case presents a nice scenario for yet another "dorfmanisms" aka automatic variables usage-&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   input ID $  Date :mmddyy10.  FirstPositive;
   format date mmddyy10.;
datalines;
A      1/1/2020        .    
A      1/3/2020      1     
A      1/4/2020       .    
A      1/5/2020       .    
B      1/4/2020      1     
B      1/5/2020       .    
;

data want;
 do until(last.id);
  set have;
  by id;
  diff=dif(date);
  if _n_ then diff=.;
  if FirstPositive then _n_=0;
  if _n_=0  then  count=sum(count,1);
  output;
 end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 17 Jun 2020 23:38:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-time-difference-when-certain-requirement-met/m-p/661952#M197844</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-06-17T23:38:23Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate time difference when certain requirement met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-time-difference-when-certain-requirement-met/m-p/661954#M197845</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID  $ Date : mmddyy10.  FirstPositive;
format date mmddyy10.;
cards;
A 1/1/2020 .
A 1/3/2020 1
A 1/4/2020 .
A 1/5/2020 .
B 1/4/2020 1
B 1/5/2020 .
;
run;



data want;
	Set have;
	by ID FirstPositive notsorted;

	retain FirstPositive_;

	if FirstPositive=1 then FirstPositive_=FirstPositive;

	if first.id then count=.;
		if FirstPositive_=1  then count+ (1 * FirstPositive_);	else count =.;

	dif=ifn(first.ID=0 and count &amp;gt; FirstPositive_ ,dif(date),.);

	drop FirstPositive_ ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 17 Jun 2020 23:55:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-time-difference-when-certain-requirement-met/m-p/661954#M197845</guid>
      <dc:creator>r_behata</dc:creator>
      <dc:date>2020-06-17T23:55:01Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate time difference when certain requirement met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-time-difference-when-certain-requirement-met/m-p/661961#M197850</link>
      <description>Thank you so much. That's exactly what I'm looking for. I also appreciate your detailed explanation of functions used here. so helpful!</description>
      <pubDate>Thu, 18 Jun 2020 00:09:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-time-difference-when-certain-requirement-met/m-p/661961#M197850</guid>
      <dc:creator>huhuhu</dc:creator>
      <dc:date>2020-06-18T00:09:01Z</dc:date>
    </item>
  </channel>
</rss>

