<?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: Change in binary status across visits in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Change-in-binary-status-across-visits/m-p/823047#M324993</link>
    <description>&lt;P&gt;Your data is in perfect shape to solve the problem, no need to transpose it.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set dat1;
   by Id;
   
   length num_changes 8;
   retain num_changes;
   
   last_status = lag(status);
   
   if first.id then do;
      num_changes = 0;
   end;
   else do;
      num_changes = num_changes + (last_status ^= status);
   end;
   
   if last.id then do;
      output;
   end;
   
   drop last_status status;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 13 Jul 2022 04:27:46 GMT</pubDate>
    <dc:creator>andreas_lds</dc:creator>
    <dc:date>2022-07-13T04:27:46Z</dc:date>
    <item>
      <title>Change in binary status across visits</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-in-binary-status-across-visits/m-p/823045#M324992</link>
      <description>&lt;P&gt;I have a dataset with over 700 records, similar in format to the sample dataset below.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Each participant (ID) can have up to a maximum of 5 visits (visit=00 through 04); some might have only one visit, others might have more than one.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Status is a binary variable that denotes their disease status (0=negative, 1=positive for disease) and this status can change from visit to visit. For example, a participant might be positive at baseline (Status=1 at visit=00) and then negative at the next visit(s), another participant might have the same status across all visits and so on.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to know how many times participants had change in Status from visit to visit but not sure how to do this programmatically. I am thinking to present counts for all the different combinations of disease status across the different visits that the participants had. For example, with a total of 5 visits, we’d have a total of 2^5=32 possible combinations, like so:&lt;/P&gt;
&lt;P&gt;0-0-0-0-0,&amp;nbsp; N=N1 (where N1 is number of patients who had Status=0 at all 5 visits)&lt;/P&gt;
&lt;P&gt;1-0-0-0-0, &amp;nbsp;N=N2 (N2 is number of patients who had Status=1 at Visit=00 but 0 at all subsequent visits)&lt;/P&gt;
&lt;P&gt;0-1-0-0-0, &amp;nbsp;N=N3 (N3 is number of patients who had Status=1 at Visit=01 but 0 at all other visits)&lt;/P&gt;
&lt;P&gt;ETC.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My only idea for how to approach this is to transpose the data and then manually list all the 32 different combinations with if-then statements and flag them so I can then add them up and see how many of each I have but this seems really cumbersome, inefficient and probably not the best solution since it doesn't address cases where participant had just two or three visits with a change in status somewhere along the way (For example, case of ID=002 or 008, 009 etc.). Does anybody have any suggestions or sample code for how they would approach this problem? Code of what I’ve done so far is shown below.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dat1;
	input ID$ Visit$ Status$;
datalines;
001	00	0
001	01	0
001	02	0
001	03	0
001	04	0
002	00	1
002	01	0
002	02	0
002	03	1
003	01	1
003	02	1
004	00	1
004	02	1
004	03	0
005	00	1
006	02	0
007	02	0
007	04	0
008	00	1
008	02	1
008	03	0
009	00	0
009	01	1
009	02	0
009	03	1
010	01	1
011	00	1
011	01	0
011	02	0
011	03	1
011	04	0
;
run;
proc print data=dat1; run;

*Convert data from long to wide with transpose;
proc transpose data=dat1 out=dat_wide prefix=Status;
    by ID;
    id visit;
    var status;
run;
proc print data=dat_wide; run;

data dat_wide2;
	set dat_wide;
	if Status00=0 and Status01=0 and Status02=0 and Status03=0 and Status04=0 then flag1=1;

	if Status00=0 and Status01=0 and Status02=0 and Status03=0 and Status04=1 then flag2=1;

	if Status00=0 and Status01=0 and Status02=0 and Status03=1 and Status04=0 then flag3=1;
	/*etc for the remaining 29 combinations*/
run;
proc print data=dat_wide2; run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jul 2022 04:19:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-in-binary-status-across-visits/m-p/823045#M324992</guid>
      <dc:creator>Merdock</dc:creator>
      <dc:date>2022-07-13T04:19:21Z</dc:date>
    </item>
    <item>
      <title>Re: Change in binary status across visits</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-in-binary-status-across-visits/m-p/823047#M324993</link>
      <description>&lt;P&gt;Your data is in perfect shape to solve the problem, no need to transpose it.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set dat1;
   by Id;
   
   length num_changes 8;
   retain num_changes;
   
   last_status = lag(status);
   
   if first.id then do;
      num_changes = 0;
   end;
   else do;
      num_changes = num_changes + (last_status ^= status);
   end;
   
   if last.id then do;
      output;
   end;
   
   drop last_status status;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jul 2022 04:27:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-in-binary-status-across-visits/m-p/823047#M324993</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2022-07-13T04:27:46Z</dc:date>
    </item>
    <item>
      <title>Re: Change in binary status across visits</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-in-binary-status-across-visits/m-p/823076#M325007</link>
      <description>&lt;P&gt;Do this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data changes;
set dat1;
by id;
change = ifn(first.id,0,status ne lag(status));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can now perform summary statistics on change as needed (overall, by id, by visit).&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jul 2022 09:46:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-in-binary-status-across-visits/m-p/823076#M325007</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-07-13T09:46:20Z</dc:date>
    </item>
    <item>
      <title>Re: Change in binary status across visits</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-in-binary-status-across-visits/m-p/825325#M325994</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;, thank you kindly for your help, this helps solve my problem! Accepted it as solution.&lt;BR /&gt;Cheers!</description>
      <pubDate>Mon, 25 Jul 2022 17:41:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-in-binary-status-across-visits/m-p/825325#M325994</guid>
      <dc:creator>Merdock</dc:creator>
      <dc:date>2022-07-25T17:41:16Z</dc:date>
    </item>
    <item>
      <title>Re: Change in binary status across visits</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Change-in-binary-status-across-visits/m-p/825326#M325995</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;, thank you so much, this works as well!</description>
      <pubDate>Mon, 25 Jul 2022 17:41:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Change-in-binary-status-across-visits/m-p/825326#M325995</guid>
      <dc:creator>Merdock</dc:creator>
      <dc:date>2022-07-25T17:41:58Z</dc:date>
    </item>
  </channel>
</rss>

