<?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: How to Accumulate Drug Sequences for Each Patient Based on Time Variable in SAS? in SAS Data Science</title>
    <link>https://communities.sas.com/t5/SAS-Data-Science/How-to-Accumulate-Drug-Sequences-for-Each-Patient-Based-on-Time/m-p/971508#M11059</link>
    <description>&lt;P&gt;For data processing on the SAS side using data step by group processing is a very valid approach. If your data resides in a database then pushing processing to the database using SQL (eventually DB native SQL) would be another option.&lt;/P&gt;
&lt;P&gt;Below code for processing on the SAS side.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've amended your sample data a bit with cases for repeated drugs at different time points. The logic for the group var takes currently only the first occurrence of a drug per patient. If you need something else then you need to tell us and also provide sample data that contains such cases.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's also always appreciated if you provide sample data already via working SAS code as done below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines truncover dsd dlm=',';
  input patient time drug :$8.;
  datalines;
1,1,A
1,2,B
2,1,B
2,2,A
2,3,B
3,1,AB
3,2,AB
3,3,BA
3,4,B
3,5,A B
3,5,b
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However you implement, you will in any case need to read the data twice, once for constructing the group variable and once for adding the group to all the rows of your source data.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Only sort the data if you can't be sure it's already sorted. If it could be sorted but you're not sure then you can use the presorted option with Proc Sort - but only if the data could already be sorted because presorted does a prescan (read) of the data so it's only beneficial for cases where the data is actually already sorted. If it's not already sorted the the option will add processing time.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have presorted;
  by patient time;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;About your Best Approach question:&lt;/P&gt;
&lt;P&gt;I'd say that depends on your needs. Coding for performance often means more complicated and though harder to maintain code. In my mind it's often more about keeping the balance and what's the right approach for a specific situation.&lt;BR /&gt;If you want to take performance tuning to the "extreme" then you need also to take your environment into consideration - throughput to disk both for WORK and permanent tables , available memory, data volume to process, etc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below two coding options.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Option 1: Keep code simple but not best performance. Creates an intermediary table.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* create and populate group variable per patient */
data inter(keep=patient group);
  set have;
  by patient time;

  /* Declare variables */
  length group $20;
  retain group;

  /* First time seeing a patient — initialize group */
  if first.patient then group = drug;
  /* add new drug if not already in group */
  else  
  if findw(group,drug,'|','it')=0 then group = catx('|', group, drug);

  if last.patient then output;
run;

/* Repopulate full group value across all rows for each patient */
data want;
  merge have inter;
  by patient;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Option 2: Performance tuned code with processing in a single data step. Avoids writing to an intermediary table.&lt;/P&gt;
&lt;P&gt;Uses a "double DOW". The first loop populates the group variable, the 2nd DOW loop writes all rows to the target table with the populated group variable included. Also this approach requires the source data to be sorted by patient and time.&lt;BR /&gt;There are multiple papers discussing the DOW loop technique. Here one of them: &lt;A href="https://www.lexjansen.com/wuss/2009/tut/TUT-Allen.pdf" target="_self"&gt;Practical Uses of the DOW Loop &lt;/A&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want2(drop=_:);

  dcl hash h1();
  h1.defineKey('_drug');
  h1.defineDone();
  
  do _n_=1 by 1 until(last.patient);
    set have;
    by patient;
    /* Declare variables */
    length group $20;
    _drug=upcase(drug);
  
    /* First time seeing a patient — empty hash lookup table and start populating group */
    if first.patient then
      do;
        _rc=h1.clear();
        group=_drug;
        _rc=h1.add();
      end;
    else
    /* for same patient: add new drugs to group */
    if h1.check() ne 0 then
      do;
        group = catx('|', group, _drug);
        _rc=h1.add();
      end;
  end;
 
  do _n_=1 by 1 until(last.patient);
    set have;
    by patient;
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&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;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 26 Jul 2025 04:22:42 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2025-07-26T04:22:42Z</dc:date>
    <item>
      <title>How to Accumulate Drug Sequences for Each Patient Based on Time Variable in SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Science/How-to-Accumulate-Drug-Sequences-for-Each-Patient-Based-on-Time/m-p/971505#M11058</link>
      <description>&lt;P&gt;Hello&amp;nbsp; Community,&lt;/P&gt;&lt;P&gt;I hope you're doing well. I have a dataset with three key variables: &lt;CODE&gt;patient&lt;/CODE&gt;, &lt;CODE&gt;time&lt;/CODE&gt;, and &lt;CODE&gt;drug&lt;/CODE&gt;. My goal is to create a new variable (&lt;CODE&gt;group&lt;/CODE&gt;) that reflects the sequence of drugs that each patient receives based on their prescription time.&lt;/P&gt;&lt;P&gt;For instance, here’s a small sample of the data:&lt;/P&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;patient time drug &lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;In this example:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;P&gt;&lt;STRONG&gt;Patient 1&lt;/STRONG&gt; received drug &lt;STRONG&gt;A&lt;/STRONG&gt; at time &lt;STRONG&gt;1&lt;/STRONG&gt; and drug &lt;STRONG&gt;B&lt;/STRONG&gt; at time &lt;STRONG&gt;2&lt;/STRONG&gt;, so their &lt;CODE&gt;group&lt;/CODE&gt; should be &lt;CODE&gt;"AB"&lt;/CODE&gt;.&lt;/P&gt;&lt;/LI&gt;&lt;LI&gt;&lt;P&gt;&lt;STRONG&gt;Patient 2&lt;/STRONG&gt; received drug &lt;STRONG&gt;B&lt;/STRONG&gt; at time &lt;STRONG&gt;1&lt;/STRONG&gt; and drug &lt;STRONG&gt;A&lt;/STRONG&gt; at time &lt;STRONG&gt;2&lt;/STRONG&gt;, so their &lt;CODE&gt;group&lt;/CODE&gt; should be &lt;CODE&gt;"BA"&lt;/CODE&gt;.&lt;/P&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;I need to accumulate a sub-string for each patient based on the &lt;CODE&gt;drug&lt;/CODE&gt; sequence, where the order of the drugs is determined by the &lt;CODE&gt;time&lt;/CODE&gt; variable.&lt;/P&gt;&lt;P&gt;What I’ve Tried So Far:&lt;/P&gt;&lt;P&gt;I initially thought of using a &lt;CODE&gt;BY&lt;/CODE&gt; group processing approach in a &lt;CODE&gt;DATA&lt;/CODE&gt; step to accumulate the drug names for each patient, but I’m running into some challenges with concatenation and ensuring the drugs are ordered correctly.&lt;/P&gt;&lt;P&gt;Solution Requirements:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;&lt;P&gt;&lt;STRONG&gt;Sort by patient and time&lt;/STRONG&gt;: To ensure the drug sequence is accumulated in the correct order.&lt;/P&gt;&lt;/LI&gt;&lt;LI&gt;&lt;P&gt;&lt;STRONG&gt;Concatenate drug names&lt;/STRONG&gt;: For each patient, accumulate the drug sequence into a new variable called &lt;CODE&gt;group&lt;/CODE&gt;.&lt;/P&gt;&lt;/LI&gt;&lt;LI&gt;&lt;P&gt;&lt;STRONG&gt;Handle each patient separately&lt;/STRONG&gt;: The accumulated group should be assigned to each patient, but only once per patient (after all their records are processed).&lt;/P&gt;&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;Expected Output:&lt;/P&gt;&lt;P&gt;For each patient, the output should be:&lt;/P&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;patient time drug group &lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;AB&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;AB&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;BA&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;BA&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;HR /&gt;&lt;P&gt;Seeking Guidance:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;&lt;P&gt;&lt;STRONG&gt;Best Approach&lt;/STRONG&gt;: What’s the most efficient way to&amp;nbsp; handle this type of problem? Should I continue with a &lt;CODE&gt;BY&lt;/CODE&gt; group processing approach, or is there another technique that would work better (such as using &lt;CODE&gt;PROC SQL&lt;/CODE&gt; or another method)?&lt;/P&gt;&lt;/LI&gt;&lt;LI&gt;&lt;P&gt;&lt;STRONG&gt;Challenges with Concatenation&lt;/STRONG&gt;: How can I ensure that the drug names are concatenated in the right order for each patient, even if the data might not always come sorted by &lt;CODE&gt;time&lt;/CODE&gt;?&lt;/P&gt;&lt;/LI&gt;&lt;LI&gt;&lt;P&gt;&lt;STRONG&gt;Optimisation Tips&lt;/STRONG&gt;: If there’s a more optimised solution to handle larger datasets, I’d appreciate any pointers!&lt;/P&gt;&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;Conclusion:&lt;/P&gt;&lt;P&gt;This seems like a common issue where you need to group data by patient and time, but I’m hoping to make the solution as efficient as possible. Any advice or solutions from the community would be greatly appreciated!&lt;/P&gt;&lt;P&gt;For further context,&lt;/P&gt;&lt;P&gt;I am also exploring&amp;nbsp; to analysing healthcare data, which might help with more advanced drug sequence analysis. If anyone has experience combining with tools, I would love to hear your thoughts!&lt;/P&gt;&lt;P&gt;Thank you in advance!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Jul 2025 21:12:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Science/How-to-Accumulate-Drug-Sequences-for-Each-Patient-Based-on-Time/m-p/971505#M11058</guid>
      <dc:creator>ArdigenSupport</dc:creator>
      <dc:date>2025-07-25T21:12:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to Accumulate Drug Sequences for Each Patient Based on Time Variable in SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Science/How-to-Accumulate-Drug-Sequences-for-Each-Patient-Based-on-Time/m-p/971508#M11059</link>
      <description>&lt;P&gt;For data processing on the SAS side using data step by group processing is a very valid approach. If your data resides in a database then pushing processing to the database using SQL (eventually DB native SQL) would be another option.&lt;/P&gt;
&lt;P&gt;Below code for processing on the SAS side.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've amended your sample data a bit with cases for repeated drugs at different time points. The logic for the group var takes currently only the first occurrence of a drug per patient. If you need something else then you need to tell us and also provide sample data that contains such cases.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's also always appreciated if you provide sample data already via working SAS code as done below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines truncover dsd dlm=',';
  input patient time drug :$8.;
  datalines;
1,1,A
1,2,B
2,1,B
2,2,A
2,3,B
3,1,AB
3,2,AB
3,3,BA
3,4,B
3,5,A B
3,5,b
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However you implement, you will in any case need to read the data twice, once for constructing the group variable and once for adding the group to all the rows of your source data.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Only sort the data if you can't be sure it's already sorted. If it could be sorted but you're not sure then you can use the presorted option with Proc Sort - but only if the data could already be sorted because presorted does a prescan (read) of the data so it's only beneficial for cases where the data is actually already sorted. If it's not already sorted the the option will add processing time.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have presorted;
  by patient time;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;About your Best Approach question:&lt;/P&gt;
&lt;P&gt;I'd say that depends on your needs. Coding for performance often means more complicated and though harder to maintain code. In my mind it's often more about keeping the balance and what's the right approach for a specific situation.&lt;BR /&gt;If you want to take performance tuning to the "extreme" then you need also to take your environment into consideration - throughput to disk both for WORK and permanent tables , available memory, data volume to process, etc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below two coding options.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Option 1: Keep code simple but not best performance. Creates an intermediary table.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* create and populate group variable per patient */
data inter(keep=patient group);
  set have;
  by patient time;

  /* Declare variables */
  length group $20;
  retain group;

  /* First time seeing a patient — initialize group */
  if first.patient then group = drug;
  /* add new drug if not already in group */
  else  
  if findw(group,drug,'|','it')=0 then group = catx('|', group, drug);

  if last.patient then output;
run;

/* Repopulate full group value across all rows for each patient */
data want;
  merge have inter;
  by patient;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Option 2: Performance tuned code with processing in a single data step. Avoids writing to an intermediary table.&lt;/P&gt;
&lt;P&gt;Uses a "double DOW". The first loop populates the group variable, the 2nd DOW loop writes all rows to the target table with the populated group variable included. Also this approach requires the source data to be sorted by patient and time.&lt;BR /&gt;There are multiple papers discussing the DOW loop technique. Here one of them: &lt;A href="https://www.lexjansen.com/wuss/2009/tut/TUT-Allen.pdf" target="_self"&gt;Practical Uses of the DOW Loop &lt;/A&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want2(drop=_:);

  dcl hash h1();
  h1.defineKey('_drug');
  h1.defineDone();
  
  do _n_=1 by 1 until(last.patient);
    set have;
    by patient;
    /* Declare variables */
    length group $20;
    _drug=upcase(drug);
  
    /* First time seeing a patient — empty hash lookup table and start populating group */
    if first.patient then
      do;
        _rc=h1.clear();
        group=_drug;
        _rc=h1.add();
      end;
    else
    /* for same patient: add new drugs to group */
    if h1.check() ne 0 then
      do;
        group = catx('|', group, _drug);
        _rc=h1.add();
      end;
  end;
 
  do _n_=1 by 1 until(last.patient);
    set have;
    by patient;
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&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;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 26 Jul 2025 04:22:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Science/How-to-Accumulate-Drug-Sequences-for-Each-Patient-Based-on-Time/m-p/971508#M11059</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2025-07-26T04:22:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to Accumulate Drug Sequences for Each Patient Based on Time Variable in SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Science/How-to-Accumulate-Drug-Sequences-for-Each-Patient-Based-on-Time/m-p/971509#M11060</link>
      <description>&lt;P&gt;That would be easy if there were not duplicated drug within a patient.&lt;/P&gt;
&lt;P&gt;Otherwise,you need PROC SORT+NODUPKEY option to eliminate these duplicated drug.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines truncover dsd dlm=',';
  input patient time drug :$8.;
  datalines;
1,1,A
1,2,B
2,1,B
2,2,A
;

data want;
do until(last.patient);
 set have;
 by patient;
 length group $ 80;
 group=cats(group,drug);
end;
do until(last.patient);
 set have;
 by patient;
 output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 26 Jul 2025 04:37:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Science/How-to-Accumulate-Drug-Sequences-for-Each-Patient-Based-on-Time/m-p/971509#M11060</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2025-07-26T04:37:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to Accumulate Drug Sequences for Each Patient Based on Time Variable in SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Science/How-to-Accumulate-Drug-Sequences-for-Each-Patient-Based-on-Time/m-p/972342#M11068</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/476309"&gt;@ArdigenSupport&lt;/a&gt;&amp;nbsp;, I copied my answer to&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/235176"&gt;@pink_poodle&lt;/a&gt;&amp;nbsp;'s question in the other post here. The basic idea is: (1) first use &lt;EM&gt;lag() &lt;/EM&gt;function or &lt;EM&gt;proc transpose,&amp;nbsp;&lt;/EM&gt;and then &lt;EM&gt;cat()&lt;/EM&gt; function to create the patient group column, and then (2) use &lt;EM&gt;sql join&lt;/EM&gt; to produce the final table. And BTW, I think &lt;EM&gt;lag()&lt;/EM&gt; function and &lt;EM&gt;proc transpose&lt;/EM&gt; are suitable techniques for dealing with data from this kind of typical crossover or repeated measure design.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(1) use &lt;EM&gt;lag()&lt;/EM&gt; function&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have1;
	input patient time drug $;
	datalines;
1 1 A
1 2 B
2 1 B
2 2 A
;
run;
proc print data=have1;run;
data want1;
   set have1;
   by patient;
   drugprev1=lag1(drug);
   if first.patient then drugprev1=' ';
   if last.patient then group=cat(compress(drugprev1),
                                  compress(drug));
run;
proc print data=want1;run;
proc sql;
create table patientgrp as
select distinct patient,group
   from want1
   where group is not null;
select * from patientgrp;
quit;
proc sql;
create table wantfinal as
select w.patient,w.time,pg.group
   from want1 as w left join
        patientgrp as pg
   on w.patient=pg.patient;
select * from wantfinal;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dxiao2017_0-1754660246175.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/108918iC21192076025D48F/image-size/large?v=v2&amp;amp;px=999" role="button" title="dxiao2017_0-1754660246175.png" alt="dxiao2017_0-1754660246175.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(2) use &lt;EM&gt;proc transpose&lt;/EM&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input patient time drug $;
	datalines;
1 1 A
1 2 B
1 3 B
2 1 B
2 2 A
;
run;
proc print data=have;run;
data have1;
   set have;
   timechar=cat('t',put(time,1.));
run;
proc print data=have1;run;
proc transpose data=have1 out=want;
   by patient;
   id timechar;
   var drug;
run;
proc print data=want;run;
data want1;
   set want;
   group=compress(cat(t1,t2,t3));
run;
proc print data=want1;run;
proc sql;
select h.*,
       w1.group
   from have as h left join
        want1 w1
   on h.patient=w1.patient
   order by h.patient,h.time;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dxiao2017_3-1754661163390.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/108921iEB689D63CE1BAF1E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="dxiao2017_3-1754661163390.png" alt="dxiao2017_3-1754661163390.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dxiao2017_4-1754661218892.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/108922i0D5FE1475046AD8A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="dxiao2017_4-1754661218892.png" alt="dxiao2017_4-1754661218892.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Aug 2025 14:15:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Science/How-to-Accumulate-Drug-Sequences-for-Each-Patient-Based-on-Time/m-p/972342#M11068</guid>
      <dc:creator>dxiao2017</dc:creator>
      <dc:date>2025-08-08T14:15:22Z</dc:date>
    </item>
  </channel>
</rss>

