<?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: flag if more than one sample on same date in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/flag-if-more-than-one-sample-on-same-date/m-p/733183#M228481</link>
    <description>Thanks, ChrisNZ.&lt;BR /&gt;1. I will use the appropriate icon to paste my code next time, thank you for pointing this out.&lt;BR /&gt;2. The flag is 0 for first record of ID=1 and for the last record of ID=2 because there are no plasma samples taken on those dates. And as I've clearly specified, the flag will be 0 for blood samples collected on any given day if there's no plasma sample collected on same day.&lt;BR /&gt;3. It's not a mistake, see #2 above.</description>
    <pubDate>Tue, 13 Apr 2021 02:32:28 GMT</pubDate>
    <dc:creator>Merdock</dc:creator>
    <dc:date>2021-04-13T02:32:28Z</dc:date>
    <item>
      <title>flag if more than one sample on same date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/flag-if-more-than-one-sample-on-same-date/m-p/731503#M227855</link>
      <description>&lt;P&gt;Hello!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset with&amp;nbsp; sample types (blood and plasma) taken from patients on various dates.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I want is: if a patient only had blood sample type taken on any given date, with no plasma sample, then I want to set up a flag, multi_samp=0. On the other hand, if on any given date, patient had both blood and plasma samples taken, then multi_samp=1. If patient only had plasma sample on any given date then multi_samp=1, regardless of whether there was a blood sample taken on that date.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;input patid $ sn st $ date: mmddyy.;&lt;BR /&gt;format date mmddyy.;&lt;BR /&gt;datalines;&lt;BR /&gt;001 1002130 blood 07/01/2019&lt;BR /&gt;001 1002131 blood 08/25/2020&lt;BR /&gt;001 1002132 blood 08/25/2020&lt;BR /&gt;001 1002133 plasma 08/25/2020&lt;BR /&gt;002 2500252 plasma 12/02/2020&lt;BR /&gt;002 2500253 plasma 04/05/2019&lt;BR /&gt;002 2500254 blood 04/05/2019&lt;BR /&gt;002 2500255 blood 04/05/2019&lt;BR /&gt;002 2500256 blood 09/14/2019&lt;BR /&gt;003 1224535 blood 01/05/2021&lt;BR /&gt;003 1224536 blood 01/05/2021&lt;BR /&gt;003 1224537 plasma 01/05/2021&lt;BR /&gt;004 6602191 blood 09/08/2020&lt;BR /&gt;004 6602192 blood 09/08/2020&lt;BR /&gt;004 6602193 blood 11/02/2020&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;input patid $ sn st $ date: mmddyy. multi_samp;&lt;BR /&gt;format date mmddyy.;&lt;BR /&gt;datalines;&lt;BR /&gt;001 1002130 blood 07/01/2019 0&lt;BR /&gt;001 1002131 blood 08/25/2020 1&lt;BR /&gt;001 1002132 blood 08/25/2020 1&lt;BR /&gt;001 1002133 plasma 08/25/2020 1&lt;BR /&gt;002 2500252 plasma 12/02/2020 1&lt;BR /&gt;002 2500253 plasma 04/05/2019 1&lt;BR /&gt;002 2500254 blood 04/05/2019 1&lt;BR /&gt;002 2500255 blood 04/05/2019 1&lt;BR /&gt;002 2500256 blood 09/14/2019 0&lt;BR /&gt;003 1224535 blood 01/05/2021 1&lt;BR /&gt;003 1224536 blood 01/05/2021 1&lt;BR /&gt;003 1224537 plasma 01/05/2021 1&lt;BR /&gt;004 6602191 blood 09/08/2020 0&lt;BR /&gt;004 6602192 blood 09/08/2020 0&lt;BR /&gt;004 6602193 blood 11/02/2020 0&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I've done so far is below but this doesn't quite give me what I need and I've been stuck here for the past day:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sort data=have out=have2;&lt;BR /&gt;by patid st date; run;&lt;BR /&gt;data final;&lt;BR /&gt;set have2;&lt;BR /&gt;by patid st date;&lt;BR /&gt;if st ne "blood" then multi_samp=1;&lt;/P&gt;&lt;P&gt;if st="blood" then do;&lt;BR /&gt;if first.date and last.date then multi_samp=0;&lt;BR /&gt;else multi_samp=1;&lt;BR /&gt;end;&lt;/P&gt;&lt;P&gt;run;&lt;BR /&gt;proc print data=final; run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If anybody has any tips or suggestions, I'd greatly appreciate it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Apr 2021 03:52:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/flag-if-more-than-one-sample-on-same-date/m-p/731503#M227855</guid>
      <dc:creator>Merdock</dc:creator>
      <dc:date>2021-04-06T03:52:56Z</dc:date>
    </item>
    <item>
      <title>Re: flag if more than one sample on same date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/flag-if-more-than-one-sample-on-same-date/m-p/731505#M227857</link>
      <description>&lt;P&gt;Your data appears to already be grouped by patid/date, even though it's not in ascending or descending order, so you don't have to sort the data.&amp;nbsp; &amp;nbsp;But you do need to read each patid/date group twice - once to find any "plasma" records, and once to output the data with the muti_samp dummy variable:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input patid $ sn st $ date: mmddyy.;
format date mmddyy.;
datalines;
001 1002130 blood 07/01/2019
001 1002131 blood 08/25/2020
001 1002132 blood 08/25/2020
001 1002133 plasma 08/25/2020
002 2500252 plasma 12/02/2020
002 2500253 plasma 04/05/2019
002 2500254 blood 04/05/2019
002 2500255 blood 04/05/2019
002 2500256 blood 09/14/2019
003 1224535 blood 01/05/2021
003 1224536 blood 01/05/2021
003 1224537 plasma 01/05/2021
004 6602191 blood 09/08/2020
004 6602192 blood 09/08/2020
004 6602193 blood 11/02/2020
run;
proc sort;
  by patid date;
run;
data want;
  do until (last.date);
    set have ;
    by patid date ;
    if st='plasma' then multi_samp=1;
  end;
  if multi_samp^=1 then multi_samp=0;
  do until (last.date);
    set have ;
    by patid date ;
    output;
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BTW, &lt;EM&gt;&lt;STRONG&gt;multi_samp&lt;/STRONG&gt;&lt;/EM&gt; is a bit misleading term, since there is nothing "multi" in having only a plasma sample.&lt;/P&gt;</description>
      <pubDate>Tue, 06 Apr 2021 04:36:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/flag-if-more-than-one-sample-on-same-date/m-p/731505#M227857</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-04-06T04:36:58Z</dc:date>
    </item>
    <item>
      <title>Re: flag if more than one sample on same date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/flag-if-more-than-one-sample-on-same-date/m-p/731508#M227859</link>
      <description>&lt;P&gt;1. Paste your code using the appropriate icon&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. Why is your flag equal to zero for the first record of ID=1, and also for the last record of ID=2 ?&lt;/P&gt;
&lt;P&gt;3. Assuming that's a mistake (please proofread your questions carefully before submitting, that's the least you can do), here's another way:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;&amp;nbsp;
&amp;nbsp; create table WANT as&amp;nbsp;
  select *, ( sum(ST ne 'BLOOD') &amp;gt; 0 ) as FLAG
  from HAVE
  group by PATID
  order by PATID;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Apr 2021 04:58:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/flag-if-more-than-one-sample-on-same-date/m-p/731508#M227859</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-04-06T04:58:29Z</dc:date>
    </item>
    <item>
      <title>Re: flag if more than one sample on same date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/flag-if-more-than-one-sample-on-same-date/m-p/731515#M227864</link>
      <description>&lt;P&gt;Three methods: SQL with remerge, double DO loop, or data step merge with itself:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input patid $ sn st $ date: mmddyy.;
format date mmddyy.;
datalines;
001 1002130 blood 07/01/2019
001 1002131 blood 08/25/2020
001 1002132 blood 08/25/2020
001 1002133 plasma 08/25/2020
002 2500252 plasma 12/02/2020
002 2500253 plasma 04/05/2019
002 2500254 blood 04/05/2019
002 2500255 blood 04/05/2019
002 2500256 blood 09/14/2019
003 1224535 blood 01/05/2021
003 1224536 blood 01/05/2021
003 1224537 plasma 01/05/2021
004 6602191 blood 09/08/2020
004 6602192 blood 09/08/2020
004 6602193 blood 11/02/2020
;

proc sql;
create table want1 as
  select
    a.*,
    sum(a.st = "plasma") &amp;gt; 0 as flag
  from have a
  group by patid, date
;
quit;

proc sort data=have;
by patid date;
run;

data want2;
do until (last.date);
  set have;
  by patid date;
  if st = "plasma" then flag = 1;
end;
flag = coalesce(flag,0);
do until (last.date);
  set have;
  by patid date;
  output;
end;
run;
  
proc sort data=want2;
by patid st date;
run;

data want3;
merge
  have
  have (
    in=f
    keep=patid date st
    rename=(st=_st)
    where=(_st = "plasma")
  )
;
by patid date;
flag = f;
drop _st;
run;

proc sort data=want3;
by patid st date;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 06 Apr 2021 06:48:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/flag-if-more-than-one-sample-on-same-date/m-p/731515#M227864</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-04-06T06:48:20Z</dc:date>
    </item>
    <item>
      <title>Re: flag if more than one sample on same date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/flag-if-more-than-one-sample-on-same-date/m-p/733181#M228479</link>
      <description>Thank you very much, mkeintz, this works perfectly. Apologies for any confusion created with the use of the name "multi", I agree that it probably wasn't the best term for this purpose.</description>
      <pubDate>Tue, 13 Apr 2021 02:26:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/flag-if-more-than-one-sample-on-same-date/m-p/733181#M228479</guid>
      <dc:creator>Merdock</dc:creator>
      <dc:date>2021-04-13T02:26:22Z</dc:date>
    </item>
    <item>
      <title>Re: flag if more than one sample on same date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/flag-if-more-than-one-sample-on-same-date/m-p/733182#M228480</link>
      <description>Thank you, KurtBremser!</description>
      <pubDate>Tue, 13 Apr 2021 02:27:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/flag-if-more-than-one-sample-on-same-date/m-p/733182#M228480</guid>
      <dc:creator>Merdock</dc:creator>
      <dc:date>2021-04-13T02:27:45Z</dc:date>
    </item>
    <item>
      <title>Re: flag if more than one sample on same date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/flag-if-more-than-one-sample-on-same-date/m-p/733183#M228481</link>
      <description>Thanks, ChrisNZ.&lt;BR /&gt;1. I will use the appropriate icon to paste my code next time, thank you for pointing this out.&lt;BR /&gt;2. The flag is 0 for first record of ID=1 and for the last record of ID=2 because there are no plasma samples taken on those dates. And as I've clearly specified, the flag will be 0 for blood samples collected on any given day if there's no plasma sample collected on same day.&lt;BR /&gt;3. It's not a mistake, see #2 above.</description>
      <pubDate>Tue, 13 Apr 2021 02:32:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/flag-if-more-than-one-sample-on-same-date/m-p/733183#M228481</guid>
      <dc:creator>Merdock</dc:creator>
      <dc:date>2021-04-13T02:32:28Z</dc:date>
    </item>
  </channel>
</rss>

