<?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: Count a value within group to assign new identifier in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Count-a-value-within-group-to-assign-new-identifier/m-p/276202#M58640</link>
    <description>&lt;P&gt;Thank you for the solution! I appreciate your points about SAS formatting the dates and accounting for miscarriages.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 09 Jun 2016 09:38:17 GMT</pubDate>
    <dc:creator>rogersaj</dc:creator>
    <dc:date>2016-06-09T09:38:17Z</dc:date>
    <item>
      <title>Count a value within group to assign new identifier</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Count-a-value-within-group-to-assign-new-identifier/m-p/276104#M58624</link>
      <description>&lt;P&gt;I have several years worth of prenatal care records. A single woman might show up in the record a few times with the current pregnancy, then in two years show up again with her next pregnancy (see table). I've sorted by patient ID and visit number, but am trying to &lt;STRONG&gt;create a new variable named pregnancy_no&lt;/STRONG&gt;&amp;nbsp;to identify which pregnancy it is.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;My thought was that if within "SubjectID" I sort by visit number and date, then when it jumps to a 1 again (ie. starting with visit 1 and then having another visit 1) I can assign a +1 for Pregnancy_no. Help?&amp;nbsp;&lt;/P&gt;&lt;BR /&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/12644i52134B2F8D408A0E/image-size/large?v=1.0&amp;amp;px=600" border="0" alt="Capture.PNG" title="Capture.PNG" /&gt;</description>
      <pubDate>Wed, 08 Jun 2016 20:38:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Count-a-value-within-group-to-assign-new-identifier/m-p/276104#M58624</guid>
      <dc:creator>rogersaj</dc:creator>
      <dc:date>2016-06-08T20:38:40Z</dc:date>
    </item>
    <item>
      <title>Re: Count a value within group to assign new identifier</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Count-a-value-within-group-to-assign-new-identifier/m-p/276114#M58627</link>
      <description>&lt;P&gt;Sort by SubjectID and Date, add one to preg No when visit No is equal to one or date jumps by more than 9 months.&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jun 2016 21:13:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Count-a-value-within-group-to-assign-new-identifier/m-p/276114#M58627</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-06-08T21:13:59Z</dc:date>
    </item>
    <item>
      <title>Re: Count a value within group to assign new identifier</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Count-a-value-within-group-to-assign-new-identifier/m-p/276127#M58628</link>
      <description>&lt;P&gt;First step: make sure your dates are SAS date valued. Date values make it easier to determine intevals between visits. It isn't obvious from your picture of values that the dates are SAS dates.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would suggest examining an interval based on the first date of a pregnancy for comparisons. I have had data involving women's health that showed women with visits at no longer than 4 month intervals that were recorded as pregnant at every visit for 14 months. Research revealed a miscarriage in the sequence.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If by any chance you have delivery/temination/miscarriage date&amp;nbsp;information that would be the best way to determine when to reset the counter.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's my example data with date values and some code that works for most cases.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   informat Subject $5. Name $10. VisitDate mmddyy10.;
   format VisitDate mmddyy10.;
   input subject name visitdate;
datalines;
1  Sally 01/05/2010
1  Sally 03/15/2010
1  Sally 06/17/2010
1  Sally 01/20/2011
1  Sally 03/05/2011
1  Sally 06/09/2011
2  Susan 05/05/2010
2  Susan 08/23/2010
;
run;
/* yes it's entered this way put your data may very*/
proc sort data=have; 
   by Subject VisitDate;
run;

data want;
   set have;
   by Subject visitdate;
   retain count FirstPDate;
   Format FirstPDate mmddyy10.;
   label count      = 'Pregnancy Count'
         FirstPDate = 'First visit date of pregnancy'
   ;
   if first.subject then do;
      count = 1;
      FirstPdate = VisitDate;
   end;
   /* more than 40 weeks since the first prenatal care visit should work fairly well*/
   Else if intck('week',FirstPdate,VisitDate) &amp;gt; 40 then do;
      count= count+1;
      FirstPdate= VisitDate;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jun 2016 14:31:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Count-a-value-within-group-to-assign-new-identifier/m-p/276127#M58628</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-06-09T14:31:57Z</dc:date>
    </item>
    <item>
      <title>Re: Count a value within group to assign new identifier</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Count-a-value-within-group-to-assign-new-identifier/m-p/276202#M58640</link>
      <description>&lt;P&gt;Thank you for the solution! I appreciate your points about SAS formatting the dates and accounting for miscarriages.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jun 2016 09:38:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Count-a-value-within-group-to-assign-new-identifier/m-p/276202#M58640</guid>
      <dc:creator>rogersaj</dc:creator>
      <dc:date>2016-06-09T09:38:17Z</dc:date>
    </item>
  </channel>
</rss>

