<?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: For every unique ID: check conditions in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/For-every-unique-ID-check-conditions/m-p/629147#M186014</link>
    <description>&lt;P&gt;What do you expect the output to look like?&lt;/P&gt;
&lt;P&gt;A data set (for further processing) a report (people read these)?&lt;/P&gt;</description>
    <pubDate>Tue, 03 Mar 2020 15:26:53 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2020-03-03T15:26:53Z</dc:date>
    <item>
      <title>For every unique ID: check conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-every-unique-ID-check-conditions/m-p/629141#M186010</link>
      <description>&lt;P class="_1qeIAgB0cPwnLhDF9XSiJM"&gt;Hi,&lt;/P&gt;&lt;P class="_1qeIAgB0cPwnLhDF9XSiJM"&gt;I am not very used to code in SAS. My background is in R. But I want to do the following thing and I could need some help:&lt;/P&gt;&lt;P class="_1qeIAgB0cPwnLhDF9XSiJM"&gt;Imagine the following data:&lt;/P&gt;&lt;PRE class="_3GnarIQX9tD_qsgXkfSDz1"&gt;&lt;CODE class="_34q3PgLsx9zIU5BiSOjFoM"&gt;SubjectId Seq ActivityId
1         1   A
1         2   B 
2         1   A 
2         2   B 
2         3   C
2         4   D 
2         5   A 
3         1   A 
3         2   C 
3         3   D 
4         1   C 
4         2   B &lt;/CODE&gt;&lt;/PRE&gt;&lt;P class="_1qeIAgB0cPwnLhDF9XSiJM"&gt;As you can see I have (different) many rows per unique SubjectID. I have two conditions that I want to check if fulfilled:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;&lt;P class="_1qeIAgB0cPwnLhDF9XSiJM"&gt;Imagine that Seq is a visit at the doctor. In total, there are 6 possible doctor visits(Seq: 1-6), and I want to check if every unique SubjectID has been to the doctor all 6 visits or if some is missing?&lt;/P&gt;&lt;/LI&gt;&lt;LI&gt;&lt;P class="_1qeIAgB0cPwnLhDF9XSiJM"&gt;I want to see if every unique SubjectId has done ActivityId A and B.&lt;/P&gt;&lt;/LI&gt;&lt;/OL&gt;&lt;P class="_1qeIAgB0cPwnLhDF9XSiJM"&gt;Do you know how to do this in SAS? Please feel free do ask if you miss any information.&lt;/P&gt;</description>
      <pubDate>Tue, 03 Mar 2020 15:11:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-every-unique-ID-check-conditions/m-p/629141#M186010</guid>
      <dc:creator>Born_Aioli</dc:creator>
      <dc:date>2020-03-03T15:11:39Z</dc:date>
    </item>
    <item>
      <title>Re: For every unique ID: check conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-every-unique-ID-check-conditions/m-p/629147#M186014</link>
      <description>&lt;P&gt;What do you expect the output to look like?&lt;/P&gt;
&lt;P&gt;A data set (for further processing) a report (people read these)?&lt;/P&gt;</description>
      <pubDate>Tue, 03 Mar 2020 15:26:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-every-unique-ID-check-conditions/m-p/629147#M186014</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-03-03T15:26:53Z</dc:date>
    </item>
    <item>
      <title>Re: For every unique ID: check conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-every-unique-ID-check-conditions/m-p/629148#M186015</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/314853"&gt;@Born_Aioli&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;it looks like good example for double DoW-loop:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input SubjectId Seq ActivityId $;
cards;
1 1 A
1 2 B 
2 1 A 
2 2 B 
2 3 C
2 4 D 
2 5 A
2 6 A  
3 1 A 
3 2 C 
3 3 D 
4 1 C 
4 2 B 
;
run;

data want;

allVisits = sum(1,2,3,4,5,6);
activityA = 0;
activityB = 0;
do until(last.SubjectId);
  set have;
  by SubjectId;
  allVisits + (-Seq);
  activityA + (ActivityId = "A"); drop activityA;
  activityB + (ActivityId = "B"); drop activityB;
end;
allVisits = (not allVisits);
activity = activityA and activityB;

do until(last.SubjectId);
  set have;
  by SubjectId;
  output;
end;

run;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Read about it in&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21262"&gt;@hashman&lt;/a&gt;&amp;nbsp;article here:&amp;nbsp;&lt;A href="http://support.sas.com/resources/papers/proceedings13/126-2013.pdf" target="_blank" rel="noopener"&gt;http://support.sas.com/resources/papers/proceedings13/126-2013.pdf&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;All the bets&lt;/P&gt;&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Tue, 03 Mar 2020 15:28:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-every-unique-ID-check-conditions/m-p/629148#M186015</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-03-03T15:28:50Z</dc:date>
    </item>
    <item>
      <title>Re: For every unique ID: check conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-every-unique-ID-check-conditions/m-p/629149#M186016</link>
      <description>&lt;P&gt;The optimal output is a new dataset where the unique SubjectId:s that are not fullfilling the 2 conditions are presented: including two new variables that tell us what conditions are not met, e.g. "Seq_missing" that includes what sequences that are missing (1-6) and "ActivityId_missing" that includes what ActivityId that is missing (A or/and B)&lt;/P&gt;</description>
      <pubDate>Tue, 03 Mar 2020 15:31:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-every-unique-ID-check-conditions/m-p/629149#M186016</guid>
      <dc:creator>Born_Aioli</dc:creator>
      <dc:date>2020-03-03T15:31:19Z</dc:date>
    </item>
    <item>
      <title>Re: For every unique ID: check conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-every-unique-ID-check-conditions/m-p/629151#M186018</link>
      <description>&lt;P&gt;In that case my previous code requires only one DoW-loop:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input SubjectId Seq ActivityId $;
cards;
1 1 A
1 2 B 
2 1 A 
2 2 B 
2 3 C
2 4 D 
2 5 A
2 6 A  
3 1 A 
3 2 C 
3 3 D 
4 1 C 
4 2 B 
;
run;

data want;
keep SubjectId allVisits activity;
allVisits = sum(1,2,3,4,5,6);
activityA = 0;
activityB = 0;
do until(last.SubjectId);
  set have;
  by SubjectId;
  allVisits + (-Seq);
  activityA + (ActivityId = "A"); drop activityA;
  activityB + (ActivityId = "B"); drop activityB;
end;
allVisits = (not allVisits);
activity = activityA and activityB;
if not(activity and allVisits);
run;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;All the best&lt;/P&gt;&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Tue, 03 Mar 2020 15:35:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-every-unique-ID-check-conditions/m-p/629151#M186018</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-03-03T15:35:54Z</dc:date>
    </item>
    <item>
      <title>Re: For every unique ID: check conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-every-unique-ID-check-conditions/m-p/629164#M186026</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/314853"&gt;@Born_Aioli&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is an attempt to achieve this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input SubjectId Seq ActivityId $;
	datalines;
1         1   A
1         2   B 
2         1   A 
2         2   B 
2         3   C
2         4   D 
2         5   A 
3         1   A 
3         2   C 
3         3   D 
4         1   C 
4         2   B 
;
run;

/* ActivityId_missing */
proc transpose data=have out=have_tr;
	var ActivityId;
	by SubjectId;
run;

data have2 (keep=SubjectId ActivityId_missing);
	set have_tr;
	length ActivityId_missing $10.;
	if findc(catx("",of col:),"A")&amp;gt;0 and findc(catx("",of col:),"B")&amp;gt;0 then ActivityId_missing = "";
	else if findc(cats("",of col:),"A")&amp;gt;0 then ActivityId_missing = "B";
	else if findc(cats("",of col:),"B")&amp;gt;0 then ActivityId_missing = "A";
	else ActivityId_missing = "A and B";
run;

/* Seq_missing */
proc sql;
	create table have3 as
	select SubjectId, case when count(Seq)+1&amp;lt;6 then catx("-",put(count(Seq)+1,best.),"6") else "" end as Seq_missing
	from have
	group by SubjectId;
quit;

/* Final table */
proc sql;
	create table want as
	select a.SubjectId,b.Seq_missing,a.ActivityId_missing from
	have2 as a inner join have3 as b
	on a.SubjectId=b.SubjectId;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture d’écran 2020-03-03 à 17.08.54.png" style="width: 200px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/36545iA3C999EE6F93AC58/image-size/small?v=v2&amp;amp;px=200" role="button" title="Capture d’écran 2020-03-03 à 17.08.54.png" alt="Capture d’écran 2020-03-03 à 17.08.54.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Mar 2020 16:16:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-every-unique-ID-check-conditions/m-p/629164#M186026</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-03-03T16:16:32Z</dc:date>
    </item>
    <item>
      <title>Re: For every unique ID: check conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/For-every-unique-ID-check-conditions/m-p/629392#M186128</link>
      <description>&lt;P&gt;The first condition works fine!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;According to the second condition, is it possible to get the output for the missing steps like: 3,4,5,6 instead of 3-6? It is because I cannot be sure that the answer wouldn't be: 3,4,6 (like if visit 5 isn't done). I cannot figure out how to do it.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks a lot!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Mar 2020 13:37:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/For-every-unique-ID-check-conditions/m-p/629392#M186128</guid>
      <dc:creator>Born_Aioli</dc:creator>
      <dc:date>2020-03-04T13:37:17Z</dc:date>
    </item>
  </channel>
</rss>

