BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
rogersaj
Obsidian | Level 7

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
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

3 REPLIES 3
PGStats
Opal | Level 21

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
ballardw
Super User

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;

 

rogersaj
Obsidian | Level 7

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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