<?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: Is there a way to use FIRST and LAST options within an IF THEN statement? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Is-there-a-way-to-use-FIRST-and-LAST-options-within-an-IF-THEN/m-p/772873#M245436</link>
    <description>&lt;P&gt;Remember that the data step is row-by-row always, it (normally) can't seek about the dataset randomly.&amp;nbsp; (There is a way to do that, but it's nearly always the wrong approach - it's very slow and loses a bunch of functionality.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What `first.var` does is it tells you whether the row you're on is *different from the previous row*.&amp;nbsp; That's it!&amp;nbsp; And what `last.var` does is it tells you if the row you're on is *different from the next row*.&amp;nbsp; (Yes, SAS does sort of peek at the next row for this, it's the exception to the rule.)&amp;nbsp; Mentally, in english, it tells you if you're on the "first" row of a sequence of that ID.&amp;nbsp; It just returns 1 or 0 - true or false.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Unfortunately, it only works with the `by` variables - so we need to make your data work for that.&amp;nbsp; We do need to subset the data, but we don't need to do it in a separate step - we can do it on the fly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1.&amp;nbsp; Let's subset your dataset while we do this, using&amp;nbsp;&lt;STRONG&gt;where&lt;/STRONG&gt; to only include the Phase1 folks.&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&amp;nbsp; where&amp;nbsp;findw(VISIT, 'PHASE#1') &amp;gt; 0;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;2.&amp;nbsp; Let's&amp;nbsp;&lt;STRONG&gt;retain&lt;/STRONG&gt; the start variables, because we'll need them to hang around until the last row.&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt; retain trtsdt trtstm;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;3.&amp;nbsp; Let's save aside the trtsdt and trtstm when we are on a first.id row.&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;  if first.id then do;
    trtsdt = datepart(stdtc);
    trtstm = timepart(stdtc);
 end;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;4.&amp;nbsp; Let's then save the trtedt/trtetm when we're on a last.id row, and output that row.&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;  if last.id then do;
    trtedt = datepart(stdtc);
    trtetm = timepart(stdtc);
    output;
  end;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;All together we have:&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
&amp;nbsp; set have;
&amp;nbsp; where&amp;nbsp;findw(VISIT, 'PHASE#1') &amp;gt; 0;
  by id;
  retain trtsdt trtstm;
  if first.id then do;
    trtsdt = datepart(stdtc);
    trtstm = timepart(stdtc);
  end;
  if last.id then do;
    trtedt = datepart(stdtc);
    trtetm = timepart(stdtc);
    output;
  end;&lt;BR /&gt;  keep id trtsdt trtstm trtedt trtetm;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 07 Oct 2021 20:09:48 GMT</pubDate>
    <dc:creator>snoopy369</dc:creator>
    <dc:date>2021-10-07T20:09:48Z</dc:date>
    <item>
      <title>Is there a way to use FIRST and LAST options within an IF THEN statement?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-a-way-to-use-FIRST-and-LAST-options-within-an-IF-THEN/m-p/772864#M245428</link>
      <description>&lt;P&gt;I want to take the first and last date of a subject that meets a specific criteria. So where VISIT contains "PHASE #1", I want to store the first start date and time as TRTSDT and TRTSTM, respectively and the last end date and time as TRTEDT and TRTETM, respectively. Then I want to repeat the process for the remaining phases. Is there a way to do this without having to subset?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I essentially want something along the lines of:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
 set have;
 by ID;
	if findw(VISIT, 'PHASE#1') &amp;gt; 0 then do; 
		if 	TRTSDT = datepart(first.STDTC); TRTSTM = timepart(first.STDTC); 
			TRTEDT = datepart(last.ENDTC); TRTETM = timepart(last.ENDTC);
		end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I have also tried using 'if first.ID then TRTSDT = datepart(first.STDTC)...' but it seemed to only look at first ID and last ID within the whole dataset (e.g. PHASE #1 DAY1 and PHASE#3 DAY2 for subject 1)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="mariko5797_0-1633634631630.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/64491i33609DC9EA4E64D4/image-size/medium?v=v2&amp;amp;px=400" role="button" title="mariko5797_0-1633634631630.png" alt="mariko5797_0-1633634631630.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="mariko5797_1-1633634652259.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/64492i1560469B61E22AFB/image-size/medium?v=v2&amp;amp;px=400" role="button" title="mariko5797_1-1633634652259.png" alt="mariko5797_1-1633634652259.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Oct 2021 19:40:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-a-way-to-use-FIRST-and-LAST-options-within-an-IF-THEN/m-p/772864#M245428</guid>
      <dc:creator>mariko5797</dc:creator>
      <dc:date>2021-10-07T19:40:01Z</dc:date>
    </item>
    <item>
      <title>Re: Is there a way to use FIRST and LAST options within an IF THEN statement?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-a-way-to-use-FIRST-and-LAST-options-within-an-IF-THEN/m-p/772870#M245433</link>
      <description>&lt;P&gt;How about this:&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;if first.stdtc then do;
    TRTSDT = datepart(STDTC); 
    trtstm = timepart(STDTC); 
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;with an optional&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;retain trtsdt trstm;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;depending on what you are trying to do, which isn't 100% clear to me.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Oct 2021 19:53:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-a-way-to-use-FIRST-and-LAST-options-within-an-IF-THEN/m-p/772870#M245433</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-10-07T19:53:25Z</dc:date>
    </item>
    <item>
      <title>Re: Is there a way to use FIRST and LAST options within an IF THEN statement?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-a-way-to-use-FIRST-and-LAST-options-within-an-IF-THEN/m-p/772872#M245435</link>
      <description>&lt;P&gt;Taking the DATEPART() or TIMEPART() of a binary variable is just silly.&amp;nbsp; The DATEPART is always going to by zero, which is '01JAN1960'd.&amp;nbsp; And the TIMEPART() is either going to be midnight or one second after midnight.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to find the min/max datetime value per PHASE then you need to have a variable that indicates the PHASE.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data phase;
  set have;
  phase = scan(visit,2,' ');
run;

data want;
 set phase ;
 by ID phase;
 if first.phase then start_dt=stddtc;
 retain start_dt;
 if last.phase;
 end_dt=enddtc;
 keep id phase start_dt end_dt;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 07 Oct 2021 20:03:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-a-way-to-use-FIRST-and-LAST-options-within-an-IF-THEN/m-p/772872#M245435</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-10-07T20:03:26Z</dc:date>
    </item>
    <item>
      <title>Re: Is there a way to use FIRST and LAST options within an IF THEN statement?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-a-way-to-use-FIRST-and-LAST-options-within-an-IF-THEN/m-p/772873#M245436</link>
      <description>&lt;P&gt;Remember that the data step is row-by-row always, it (normally) can't seek about the dataset randomly.&amp;nbsp; (There is a way to do that, but it's nearly always the wrong approach - it's very slow and loses a bunch of functionality.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What `first.var` does is it tells you whether the row you're on is *different from the previous row*.&amp;nbsp; That's it!&amp;nbsp; And what `last.var` does is it tells you if the row you're on is *different from the next row*.&amp;nbsp; (Yes, SAS does sort of peek at the next row for this, it's the exception to the rule.)&amp;nbsp; Mentally, in english, it tells you if you're on the "first" row of a sequence of that ID.&amp;nbsp; It just returns 1 or 0 - true or false.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Unfortunately, it only works with the `by` variables - so we need to make your data work for that.&amp;nbsp; We do need to subset the data, but we don't need to do it in a separate step - we can do it on the fly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1.&amp;nbsp; Let's subset your dataset while we do this, using&amp;nbsp;&lt;STRONG&gt;where&lt;/STRONG&gt; to only include the Phase1 folks.&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&amp;nbsp; where&amp;nbsp;findw(VISIT, 'PHASE#1') &amp;gt; 0;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;2.&amp;nbsp; Let's&amp;nbsp;&lt;STRONG&gt;retain&lt;/STRONG&gt; the start variables, because we'll need them to hang around until the last row.&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt; retain trtsdt trtstm;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;3.&amp;nbsp; Let's save aside the trtsdt and trtstm when we are on a first.id row.&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;  if first.id then do;
    trtsdt = datepart(stdtc);
    trtstm = timepart(stdtc);
 end;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;4.&amp;nbsp; Let's then save the trtedt/trtetm when we're on a last.id row, and output that row.&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;  if last.id then do;
    trtedt = datepart(stdtc);
    trtetm = timepart(stdtc);
    output;
  end;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;All together we have:&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
&amp;nbsp; set have;
&amp;nbsp; where&amp;nbsp;findw(VISIT, 'PHASE#1') &amp;gt; 0;
  by id;
  retain trtsdt trtstm;
  if first.id then do;
    trtsdt = datepart(stdtc);
    trtstm = timepart(stdtc);
  end;
  if last.id then do;
    trtedt = datepart(stdtc);
    trtetm = timepart(stdtc);
    output;
  end;&lt;BR /&gt;  keep id trtsdt trtstm trtedt trtetm;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Oct 2021 20:09:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-a-way-to-use-FIRST-and-LAST-options-within-an-IF-THEN/m-p/772873#M245436</guid>
      <dc:creator>snoopy369</dc:creator>
      <dc:date>2021-10-07T20:09:48Z</dc:date>
    </item>
    <item>
      <title>Re: Is there a way to use FIRST and LAST options within an IF THEN statement?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-a-way-to-use-FIRST-and-LAST-options-within-an-IF-THEN/m-p/772887#M245439</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=have nway;
    class id;
    var stdtc endtc;
    output out=_minmax_(drop=_:) min(stdtc)=min_stdtc max(etdtc)=max_etdtc;
run;
data want;
    merge have _minmax_;
    by id;
    trtsdt=datepart(min_stdtc);
    trtstm=timepart(min_stdtc);
    trtedt=datepart(max_etdtc);
    trtetm=timepart(max_etdtc);
    drop min_stdtc max_etdtc;
run;
    &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 07 Oct 2021 21:31:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-a-way-to-use-FIRST-and-LAST-options-within-an-IF-THEN/m-p/772887#M245439</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-10-07T21:31:58Z</dc:date>
    </item>
  </channel>
</rss>

