<?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: Set a Flag for last non-missing Value in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Set-a-Flag-for-last-non-missing-Value/m-p/477422#M122947</link>
    <description>&lt;P&gt;OK, no sorting but assuming that your data is already sorted as illustrated ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming you want a separate flag for each lab code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;week2_found = 'N';&lt;/P&gt;
&lt;P&gt;recnum = 0;&lt;/P&gt;
&lt;P&gt;do until (last.lbtestcd);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; by usubjid lbtestcd notsorted;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; if visit='WEEK2' then week2_found = 'Y';&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; if week2_found = 'N' then recnum + 1;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;recnum2=0;&lt;/P&gt;
&lt;P&gt;do until (last,.lbtestcd);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; by usubjid lbtestcd notsorted;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; recnum2 + 1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; if recnum = recnum2 then flag=1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; else flag=0;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; &lt;FONT color="#ff0000"&gt;output;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;drop recnum recnum2 week2_found;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's untested code but looks about right.&lt;/P&gt;</description>
    <pubDate>Thu, 12 Jul 2018 12:51:58 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2018-07-12T12:51:58Z</dc:date>
    <item>
      <title>Set a Flag for last non-missing Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Set-a-Flag-for-last-non-missing-Value/m-p/477339#M122921</link>
      <description>&lt;P&gt;Hey folks ,here is my data and i want to flag the last non missing obs before week2 ,suggest a method other than subsetting and sorting&lt;/P&gt;
&lt;P&gt;DATA LAB;&lt;BR /&gt;INPUT USUBJID 3. VISIT $10. @15 LBTESTCD $3.&lt;BR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/131792"&gt;@19&lt;/a&gt; LBORRES 3.&lt;BR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/105043"&gt;@23&lt;/a&gt; LBDTC DDMMYY10.;&lt;BR /&gt;FORMAT LBDTC DDMMYY10.;&lt;BR /&gt;DATALINES;&lt;BR /&gt;101 SCREENING&amp;nbsp; &amp;nbsp;ALT&amp;nbsp; &amp;nbsp;24&amp;nbsp; 24-10-2015&lt;BR /&gt;101 PREDOSE&amp;nbsp; &amp;nbsp; &amp;nbsp; ALT&amp;nbsp; &amp;nbsp;52&amp;nbsp; 22-11-2015&lt;BR /&gt;101 BASELINE&amp;nbsp; &amp;nbsp; &amp;nbsp; ALT&amp;nbsp; &amp;nbsp;45&amp;nbsp; 22-12-2015&lt;BR /&gt;101 WEEK2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ALT&amp;nbsp; &amp;nbsp;50&amp;nbsp; 10-01-2016&lt;BR /&gt;101 WEEK4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ALT&amp;nbsp; &amp;nbsp;45&amp;nbsp; 11-02-2016&lt;BR /&gt;101 WEEK6&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ALT&amp;nbsp; &amp;nbsp;55&amp;nbsp; 12-02-2016&lt;BR /&gt;101 SCREENING&amp;nbsp; LDH 110&amp;nbsp; 24-10-2015&lt;BR /&gt;101 PREDOSE&amp;nbsp; &amp;nbsp; &amp;nbsp; LDH&amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp;22-11-2015&lt;BR /&gt;101 BASELINE&amp;nbsp; &amp;nbsp; &amp;nbsp; LDH&amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; 23-12-2015&lt;BR /&gt;101 WEEK2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;LDH 120 10-01-2016&lt;BR /&gt;101 WEEK4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;LDH 120 11-02-2016&lt;BR /&gt;102 SCREENING&amp;nbsp; ALT&amp;nbsp; &amp;nbsp;44&amp;nbsp; 24-10-2015&lt;BR /&gt;102 PREDOSE&amp;nbsp; &amp;nbsp; &amp;nbsp; ALT&amp;nbsp; &amp;nbsp;55&amp;nbsp; 22-11-2015&lt;BR /&gt;102 BASELINE&amp;nbsp; &amp;nbsp; &amp;nbsp; ALT&amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp;10-01-2016&lt;BR /&gt;102 WEEK2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ALT&amp;nbsp; &amp;nbsp; 52&amp;nbsp; 11-02-2016&lt;BR /&gt;102 SCREENING LDH&amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; 24-10-2015&lt;BR /&gt;102 PREDOSE&amp;nbsp; &amp;nbsp; LDH&amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp;22-11-2015&lt;BR /&gt;102 BASELINE&amp;nbsp; &amp;nbsp; LDH 120&amp;nbsp; &amp;nbsp;12-12-2015&lt;BR /&gt;102 WEEK2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;LDH 100&amp;nbsp; &amp;nbsp;11-01-2016&lt;BR /&gt;102 WEEK4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;LDH 110&amp;nbsp; &amp;nbsp;12-02-2016&lt;BR /&gt;;&lt;BR /&gt;RUN;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jul 2018 07:28:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Set-a-Flag-for-last-non-missing-Value/m-p/477339#M122921</guid>
      <dc:creator>sas_mania</dc:creator>
      <dc:date>2018-07-12T07:28:10Z</dc:date>
    </item>
    <item>
      <title>Re: base sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Set-a-Flag-for-last-non-missing-Value/m-p/477342#M122923</link>
      <description>&lt;P&gt;Unfortunately you have - again - not paid attention to the important hints displayed below the "Post" button:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-left" image-alt="grafik.png" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/21741iEBF9460742075711/image-size/large?v=v2&amp;amp;px=999" role="button" title="grafik.png" alt="grafik.png" /&gt;&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;Please fix the issues in your post, especially the title.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why do you want to solve this without subsetting and sorting?&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jul 2018 06:58:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Set-a-Flag-for-last-non-missing-Value/m-p/477342#M122923</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2018-07-12T06:58:55Z</dc:date>
    </item>
    <item>
      <title>Re: Set a Flag for last non-missing Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Set-a-Flag-for-last-non-missing-Value/m-p/477347#M122926</link>
      <description>&lt;P&gt;The goal determines the tools:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data lab;
input
  usubjid :3.
  visit :$10.
  lbtestcd :$3.
  lborres :3.
  lbdtc :ddmmyy10.
;
format lbdtc ddmmyy10.;
datalines;
101 SCREENING   ALT   24  24-10-2015
101 PREDOSE      ALT   52  22-11-2015
101 BASELINE      ALT   45  22-12-2015
101 WEEK2           ALT   50  10-01-2016
101 WEEK4           ALT   45  11-02-2016
101 WEEK6           ALT   55  12-02-2016
101 SCREENING  LDH 110  24-10-2015
101 PREDOSE      LDH  .     22-11-2015
101 BASELINE      LDH  .      23-12-2015
101 WEEK2           LDH 120 10-01-2016
101 WEEK4           LDH 120 11-02-2016
102 SCREENING  ALT   44  24-10-2015
102 PREDOSE      ALT   55  22-11-2015
102 BASELINE      ALT   .     10-01-2016
102 WEEK2          ALT    52  11-02-2016
102 SCREENING LDH    .    24-10-2015
102 PREDOSE    LDH    .     22-11-2015
102 BASELINE    LDH 120   12-12-2015
102 WEEK2         LDH 100   11-01-2016
102 WEEK4         LDH 110   12-02-2016
;
run;

proc sort data=lab;
by usubjid descending lbdtc;
run;

data want;
set lab;
by usubjid;
retain check;
flag = 0;
if first.usubjid then check = .;
if visit = 'WEEK2' then check = 0;
else if lborres ne . and check = 0
then do;
  check = 1;
  flag = 1;
end;
drop check;
run;

proc sort data=want;
by usubjid lbdtc;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 12 Jul 2018 07:28:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Set-a-Flag-for-last-non-missing-Value/m-p/477347#M122926</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-07-12T07:28:36Z</dc:date>
    </item>
    <item>
      <title>Re: Set a Flag for last non-missing Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Set-a-Flag-for-last-non-missing-Value/m-p/477358#M122928</link>
      <description>&lt;P&gt;PS I edited the inadequate subject line. Please be more creative in the future.Using "base sas" in the &lt;EM&gt;Base SAS&lt;/EM&gt; Programming community is, ahem, not very bright.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jul 2018 07:30:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Set-a-Flag-for-last-non-missing-Value/m-p/477358#M122928</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-07-12T07:30:15Z</dc:date>
    </item>
    <item>
      <title>Re: Set a Flag for last non-missing Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Set-a-Flag-for-last-non-missing-Value/m-p/477422#M122947</link>
      <description>&lt;P&gt;OK, no sorting but assuming that your data is already sorted as illustrated ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming you want a separate flag for each lab code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;week2_found = 'N';&lt;/P&gt;
&lt;P&gt;recnum = 0;&lt;/P&gt;
&lt;P&gt;do until (last.lbtestcd);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; by usubjid lbtestcd notsorted;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; if visit='WEEK2' then week2_found = 'Y';&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; if week2_found = 'N' then recnum + 1;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;recnum2=0;&lt;/P&gt;
&lt;P&gt;do until (last,.lbtestcd);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; by usubjid lbtestcd notsorted;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; recnum2 + 1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; if recnum = recnum2 then flag=1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; else flag=0;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; &lt;FONT color="#ff0000"&gt;output;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;drop recnum recnum2 week2_found;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's untested code but looks about right.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jul 2018 12:51:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Set-a-Flag-for-last-non-missing-Value/m-p/477422#M122947</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-07-12T12:51:58Z</dc:date>
    </item>
    <item>
      <title>Re: Set a Flag for last non-missing Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Set-a-Flag-for-last-non-missing-Value/m-p/477463#M122962</link>
      <description>&lt;P&gt;How about this one ?&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;PRE&gt;&lt;CODE class=" language-sas"&gt;data lab;
input
  usubjid :3.
  visit :$10.
  lbtestcd :$3.
  lborres :3.
  lbdtc :ddmmyy10.
;
format lbdtc ddmmyy10.;
datalines;
101 SCREENING   ALT   24  24-10-2015
101 PREDOSE      ALT   52  22-11-2015
101 BASELINE      ALT   45  22-12-2015
101 WEEK2           ALT   50  10-01-2016
101 WEEK4           ALT   45  11-02-2016
101 WEEK6           ALT   55  12-02-2016
101 SCREENING  LDH 110  24-10-2015
101 PREDOSE      LDH  .     22-11-2015
101 BASELINE      LDH  .      23-12-2015
101 WEEK2           LDH 120 10-01-2016
101 WEEK4           LDH 120 11-02-2016
102 SCREENING  ALT   44  24-10-2015
102 PREDOSE      ALT   55  22-11-2015
102 BASELINE      ALT   .     10-01-2016
102 WEEK2          ALT    52  11-02-2016
102 SCREENING LDH    .    24-10-2015
102 PREDOSE    LDH    .     22-11-2015
102 BASELINE    LDH 120   12-12-2015
102 WEEK2         LDH 100   11-01-2016
102 WEEK4         LDH 110   12-02-2016
;
run;

data want;
 do i=1 by 1 until(last.lbtestcd);
  set lab;
  by usubjid lbtestcd notsorted;
  if visit not =: 'WEEK' and not missing(lborres) then idx=i;
 end; 

 do i=1 by 1 until(last.lbtestcd);
  set lab;
  by usubjid lbtestcd notsorted;
  if idx=i then flag=1;else flag=0;
  output;
 end;
 drop i idx;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jul 2018 13:55:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Set-a-Flag-for-last-non-missing-Value/m-p/477463#M122962</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-07-12T13:55:04Z</dc:date>
    </item>
  </channel>
</rss>

