title: Counting observations using multiple BY groups SAS
body: <p>I am examining prescription patterns within a large EHR dataset. The data is structured so that we are given several key bits of information, such as patient_num, encounter_num, ordering_date, medication, age_event (age at event) etc. Example below:</p>
  
  <pre><code>    Patient_num  enc_num  ordering_date  medication  age_event
          1111     888888     07NOV2008    Wellbutrin     48
          1111     876578     11MAY2011    Bupropion      50
          2222     999999     08DEC2009    Amitriptyline  32
          2222     999999     08DEC2009    Escitalopram   32
          3333     656463     12APR2007    Imipramine     44
          3333     643211     21DEC2008    Zoloft         45
          3333     543213     02FEB2009    Fluoxetine     45
  </code></pre>
  
  <p>Currently I have the dataset sorted by patient_id then by ordering_date so that I can see what each individual was prescribed during their encounters in a longitudinal fashion. For now, I am most concerned with the prescription(s) that were made during their first visit. I wrote some code to count the number of prescriptions and had originally restricted later analyses to RX = 1, but as we can see, that doesn't work for people with multiple scripts on the same encounter (Patient 2222). </p>
  
  <pre><code>    data pt_meds_;
       set pt_meds;
       by patient_num;
       if first.patient_num then  RX = 1;
       else RX + 1;
      run;
  
       Patient_num  enc_num  ordering_date  medication  age_event  RX
          1111     888888     07NOV2008    Wellbutrin     48        1
          1111     876578     11MAY2011    Bupropion      50        2
          2222     999999     08DEC2009    Amitriptyline  32        1
          2222     999999     08DEC2009    Escitalopram   32        2
          3333     656463     12APR2007    Imipramine     44        1
          3333     643211     21DEC2008    Zoloft         45        2
          3333     543213     02FEB2009    Fluoxetine     45        3
  </code></pre>
  
  <p>I think it would be more appropriate to recode the encounter numbers into a new variable so that they reflect a style similar to the RX variable. Where each encounter is listed 1-n, and the number will repeat if multiple scripts are made in the same encounter. Such as below:</p>
  
  <pre><code>     Patient_num  enc_num  ordering_date  medication  age_event  RX   Enc_
          1111     888888     07NOV2008    Wellbutrin     48        1    1
          1111     876578     11MAY2011    Bupropion      50        2    2
          2222     999999     08DEC2009    Amitriptyline  32        1    1 
          2222     999999     08DEC2009    Escitalopram   32        2    1
          3333     656463     12APR2007    Imipramine     44        1    1
          3333     643211     21DEC2008    Zoloft         45        2    2
          3333     543213     02FEB2009    Fluoxetine     45        3    3
  </code></pre>
  
  <p>From what I have seen, this could be possible with a variant of the above code using 2 BY groups (patient_num &amp; enc_num), but I can't seem to get it. I think the first. / last. codes require sorting, but if I am to sort by enc_num, they won't be in chronological order because the encounter numbers are generated by the system and depend on all other encounters going in at that time.</p>
  
  <p>I tried to do the following code (using ordering_date instead because its already sorted properly) but everything under Enc_ is printed as a 1. I'm sure my logic is all wrong. Any thoughts?</p>
  
  <pre><code>    data pt_meds_test;
       set pt_meds_;
       by patient_num ordering_date;
       if first.patient_num; 
       if first.ordering_date then enc_ = 1;
       else enc_ + 1;
      run; 
  </code></pre>