<?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: Splitting follow up time by exposure status in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Splitting-follow-up-time-by-exposure-status/m-p/766536#M242939</link>
    <description>&lt;P&gt;Thanks so much&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is a great solution if you are splitting one variable, but things become complicated when you are splitting by more than one, as there will be an overlap between the two variables timelines&lt;/P&gt;
&lt;P&gt;So for a certain person there will be say a start follow up period where diabetes=0 and medication=0, then the person becomes diabetic but without medication diabetes=1 and medication=0. Then when medication is started the variables become diabetes=1 medication=1. The current code however treats the two variables completely independently from each other, see this example with some modification:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%macro reassign (variables);
  %local var;
  %let var=%scan(&amp;amp;variables ,1);
  if &amp;amp;var.=1 and _ENTERED lt &amp;amp;var._DATE le _EXITED then do;
    ENTERED=_ENTERED  ; EXITED=&amp;amp;var._DATE; &amp;amp;var.=0; AGE=(_ENTERED  -BORN)/365; output;
    ENTERED=&amp;amp;var._date; EXITED=_EXITED   ; &amp;amp;var.=1; AGE=(&amp;amp;var._DATE-BORN)/365; output;
  end;

  
  if &amp;amp;var.=1 and &amp;amp;var._DATE le _ENTERED then do;
    ENTERED=_ENTERED  ; EXITED=_EXITED; &amp;amp;var.=1; AGE=(_ENTERED  -BORN)/365; output;
  end;


  if &amp;amp;var.=0 OR (&amp;amp;var.=1 and &amp;amp;var._DATE gt _EXITED) then do;
    ENTERED =_ENTERED ; EXITED =_EXITED ; &amp;amp;var.=0; AGE=(_ENTERED - BORN)/365;  output;
  end;
%mend;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then the data step&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data DATA2;
  set DATA1(rename=(ENTERED =_ENTERED  EXITED =_EXITED ));
  by ID;
  %reassign(diabetes);
  %reassign(medication);

  format ENTERED EXITED date7.;
  *drop _: ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So instead, the code will split the follow up time into 4 rows&lt;/P&gt;
&lt;P&gt;1. Diabetes=0 medication=0&lt;/P&gt;
&lt;P&gt;2. Diabetes=1, medication=0.&lt;/P&gt;
&lt;P&gt;3. Diabetes=0, medication=1&lt;/P&gt;
&lt;P&gt;4. Diabetes=1, medication=1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, splitting the data step into two will generate the right output:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DATA2;
  set DATA1(rename=(ENTERED =_ENTERED  EXITED =_EXITED ));
  %reassign(diabetes);
  format ENTERED EXITED date7.;
  drop _: ;
run;


data DATA3;
  set DATA2(rename=(ENTERED =_ENTERED  EXITED =_EXITED ));
  %reassign(medication);
  format ENTERED EXITED date7.;
  drop _: ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Any thoughts about how to modify the macro to get it to work within one data step?&lt;/P&gt;</description>
    <pubDate>Wed, 08 Sep 2021 10:38:07 GMT</pubDate>
    <dc:creator>ammarhm</dc:creator>
    <dc:date>2021-09-08T10:38:07Z</dc:date>
    <item>
      <title>Splitting follow up time by exposure status</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Splitting-follow-up-time-by-exposure-status/m-p/766168#M242786</link>
      <description>&lt;P&gt;Hi everyone,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;i am trying to prepare a dataset for a Cox regression analysis...&lt;/P&gt;
&lt;P&gt;Below is an example dataset to give you an idea of what I am trying to do. Let us say I am interested in studying the effect of age, diabetes and medication on survival. I am using the following dataset:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Data1;
attrib
id label = "ID"
born label = "Born" informat=ddmmyy8. format=date7.
entered label = "Entered" informat=ddmmyy8. format=date7.
exited label = "Exited" informat=ddmmyy8. format=date7.
diabetes label="Diabetes"
diabetes_date label = "Diabetes Date" informat=ddmmyy8. format=date7.
medication label="Medication"
medication_start label = "Med Start" informat=ddmmyy8. format=date7.
medication_end label = "Med End" informat=ddmmyy8. format=date7.
death label = "Death"
;
input id born entered exited diabetes diabetes_date medication medication_start medication_end  death;

cards ;
1 14/12/74 30/07/04 22/01/10 1 01/04/02 1 12/05/06 . 0
2 20/02/34 03/09/84 30/03/12 0 . 1 07/07/99 02/05/06 1
3 17/08/51 22/09/89 09/04/06 1 23/05/02 1 23/05/87 11/06/04 0
4 17/09/74 12/07/92 05/08/07 0 . 0 . . 0
;
run ;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As "diabetes" and "medication" are time varying covariate, I will need to split follow-up time to reflect that exposure. The code below illustrates how to do that for the variable diabetes:&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 Data2;
set Data1 (rename=(entered=_entered exited=_exited));
by id;
if diabetes=1 and _entered lt diabetes_date le _exited then do;
entered=_entered; exited=diabetes_date; diabetes=0; Age=(_entered-born)/365;output;
entered=diabetes_date; exited=_exited; diabetes=1; Age=(diabetes_date-born)/365; output;
end;
else do;
entered=_entered; exited=_exited; diabetes=0; Age=(_entered-born)/365; output;
end;
format entered exited date7.;
drop _entered _exited;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And then the analysis:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc phreg data=Data2;
class diabetes;
model (entered exited)*death(0)=age diabetes/ ties=exact risklimits;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You get the idea, I hope.&lt;/P&gt;
&lt;P&gt;However, I have several time-varying covariates, some of which have start and end date like the variable "medication" in the dataset above, other exposures start before entry date etc, and while one way to do this would be to repeat the steps above for each variable, I do wonder if there is an easier way?&lt;/P&gt;
&lt;P&gt;I would appreciate your help.&lt;/P&gt;
&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Mon, 06 Sep 2021 08:18:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Splitting-follow-up-time-by-exposure-status/m-p/766168#M242786</guid>
      <dc:creator>ammarhm</dc:creator>
      <dc:date>2021-09-06T08:18:02Z</dc:date>
    </item>
    <item>
      <title>Re: Splitting follow up time by exposure status</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Splitting-follow-up-time-by-exposure-status/m-p/766287#M242847</link>
      <description>Have you considered an array?</description>
      <pubDate>Tue, 07 Sep 2021 01:29:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Splitting-follow-up-time-by-exposure-status/m-p/766287#M242847</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-09-07T01:29:42Z</dc:date>
    </item>
    <item>
      <title>Re: Splitting follow up time by exposure status</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Splitting-follow-up-time-by-exposure-status/m-p/766311#M242856</link>
      <description>Thanks Reza,&lt;BR /&gt;No not really .. I cannot say arrays are my strong side, would very much appreciate a sample code if you don't mind and have the time?&lt;BR /&gt;Kind regards&lt;BR /&gt;AM</description>
      <pubDate>Tue, 07 Sep 2021 05:20:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Splitting-follow-up-time-by-exposure-status/m-p/766311#M242856</guid>
      <dc:creator>ammarhm</dc:creator>
      <dc:date>2021-09-07T05:20:09Z</dc:date>
    </item>
    <item>
      <title>Re: Splitting follow up time by exposure status</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Splitting-follow-up-time-by-exposure-status/m-p/766490#M242923</link>
      <description>&lt;P&gt;You could write a short macro, like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let variables = DIABETES ;
%macro reassign;
  %local var;
  %let var=%scan(&amp;amp;variables ,1);
  if &amp;amp;var.=1 and _ENTERED lt &amp;amp;var._DATE le _EXITED then do;
    ENTERED=_ENTERED  ; EXITED=&amp;amp;var._DATE; &amp;amp;var.=0; AGE=(_ENTERED  -BORN)/365; output;
    ENTERED=&amp;amp;var._date; EXITED=_EXITED   ; &amp;amp;var.=1; AGE=(&amp;amp;var._DATE-BORN)/365; output;
  end;
  else do;
    ENTERED =_ENTERED ; EXITED =_EXITED ; &amp;amp;var.=0; AGE=(_ENTERED - BORN)/365;  output;
  end;
%mend;

data DATA2;
  set DATA1(rename=(ENTERED =_ENTERED  EXITED =_EXITED ));
  by ID;
  %reassign;
  format ENTERED EXITED date7.;
  drop _: ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If this suits, you can add a loop to the macro to go through a list of variable names&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Sep 2021 22:19:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Splitting-follow-up-time-by-exposure-status/m-p/766490#M242923</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-09-07T22:19:28Z</dc:date>
    </item>
    <item>
      <title>Re: Splitting follow up time by exposure status</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Splitting-follow-up-time-by-exposure-status/m-p/766536#M242939</link>
      <description>&lt;P&gt;Thanks so much&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is a great solution if you are splitting one variable, but things become complicated when you are splitting by more than one, as there will be an overlap between the two variables timelines&lt;/P&gt;
&lt;P&gt;So for a certain person there will be say a start follow up period where diabetes=0 and medication=0, then the person becomes diabetic but without medication diabetes=1 and medication=0. Then when medication is started the variables become diabetes=1 medication=1. The current code however treats the two variables completely independently from each other, see this example with some modification:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%macro reassign (variables);
  %local var;
  %let var=%scan(&amp;amp;variables ,1);
  if &amp;amp;var.=1 and _ENTERED lt &amp;amp;var._DATE le _EXITED then do;
    ENTERED=_ENTERED  ; EXITED=&amp;amp;var._DATE; &amp;amp;var.=0; AGE=(_ENTERED  -BORN)/365; output;
    ENTERED=&amp;amp;var._date; EXITED=_EXITED   ; &amp;amp;var.=1; AGE=(&amp;amp;var._DATE-BORN)/365; output;
  end;

  
  if &amp;amp;var.=1 and &amp;amp;var._DATE le _ENTERED then do;
    ENTERED=_ENTERED  ; EXITED=_EXITED; &amp;amp;var.=1; AGE=(_ENTERED  -BORN)/365; output;
  end;


  if &amp;amp;var.=0 OR (&amp;amp;var.=1 and &amp;amp;var._DATE gt _EXITED) then do;
    ENTERED =_ENTERED ; EXITED =_EXITED ; &amp;amp;var.=0; AGE=(_ENTERED - BORN)/365;  output;
  end;
%mend;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then the data step&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data DATA2;
  set DATA1(rename=(ENTERED =_ENTERED  EXITED =_EXITED ));
  by ID;
  %reassign(diabetes);
  %reassign(medication);

  format ENTERED EXITED date7.;
  *drop _: ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So instead, the code will split the follow up time into 4 rows&lt;/P&gt;
&lt;P&gt;1. Diabetes=0 medication=0&lt;/P&gt;
&lt;P&gt;2. Diabetes=1, medication=0.&lt;/P&gt;
&lt;P&gt;3. Diabetes=0, medication=1&lt;/P&gt;
&lt;P&gt;4. Diabetes=1, medication=1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, splitting the data step into two will generate the right output:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DATA2;
  set DATA1(rename=(ENTERED =_ENTERED  EXITED =_EXITED ));
  %reassign(diabetes);
  format ENTERED EXITED date7.;
  drop _: ;
run;


data DATA3;
  set DATA2(rename=(ENTERED =_ENTERED  EXITED =_EXITED ));
  %reassign(medication);
  format ENTERED EXITED date7.;
  drop _: ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Any thoughts about how to modify the macro to get it to work within one data step?&lt;/P&gt;</description>
      <pubDate>Wed, 08 Sep 2021 10:38:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Splitting-follow-up-time-by-exposure-status/m-p/766536#M242939</guid>
      <dc:creator>ammarhm</dc:creator>
      <dc:date>2021-09-08T10:38:07Z</dc:date>
    </item>
    <item>
      <title>Re: Splitting follow up time by exposure status</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Splitting-follow-up-time-by-exposure-status/m-p/767008#M243103</link>
      <description>&lt;P&gt;&lt;EM&gt;&amp;gt;&amp;nbsp;Any thoughts about how to modify the macro to get it to work within one data step?&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;I don't understand all the needs, but the complexity involved would outweigh the benefit imho.&lt;/P&gt;
&lt;P&gt;Keeping legible code is always a high priority.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Unless the table is massive and reading it several times is a liability, keep it simple :).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It doesn't have to be wordy, even without a macro (so it's simple), it can still be compact:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DATA2;
  set DATA1(drop=_: rename=(ENTERED =_ENTERED  EXITED =_EXITED ));
  %reassign(DIABETES);
data DATA3;
  set DATA2(drop=_: rename=(ENTERED =_ENTERED  EXITED =_EXITED ));
  %reassign(MEDICATION);
data FINAL;
  set DATA3(drop=_: rename=(ENTERED =_ENTERED  EXITED =_EXITED ));
  %reassign(OTHER);
  format ENTERED EXITED date7.;
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>Fri, 10 Sep 2021 07:28:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Splitting-follow-up-time-by-exposure-status/m-p/767008#M243103</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-09-10T07:28:50Z</dc:date>
    </item>
  </channel>
</rss>

