Help using Base SAS procedures

Count a value within group to assign new identifier

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 8
Accepted Solution

Count a value within group to assign new identifier

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 create a new variable named pregnancy_no to identify which pregnancy it is. 

 


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? 


Capture.PNG

Accepted Solutions
Solution
‎06-09-2016 05:35 AM
Super User
Posts: 10,500

Re: Count a value within group to assign new identifier

[ Edited ]

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. 

 

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.

 

If by any chance you have delivery/temination/miscarriage date information that would be the best way to determine when to reset the counter.

 

Here's my example data with date values and some code that works for most cases.

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) > 40 then do;
      count= count+1;
      FirstPdate= VisitDate;
   end;
run;

 

View solution in original post


All Replies
Respected Advisor
Posts: 4,646

Re: Count a value within group to assign new identifier

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.

PG
Solution
‎06-09-2016 05:35 AM
Super User
Posts: 10,500

Re: Count a value within group to assign new identifier

[ Edited ]

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. 

 

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.

 

If by any chance you have delivery/temination/miscarriage date information that would be the best way to determine when to reset the counter.

 

Here's my example data with date values and some code that works for most cases.

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) > 40 then do;
      count= count+1;
      FirstPdate= VisitDate;
   end;
run;

 

Occasional Contributor
Posts: 8

Re: Count a value within group to assign new identifier

Thank you for the solution! I appreciate your points about SAS formatting the dates and accounting for miscarriages. 

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 223 views
  • 1 like
  • 3 in conversation