Creating reference date using random number generator

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 13
Accepted Solution

Creating reference date using random number generator

[ Edited ]

Hello. I have a case-control study. For cases, I have a diagnosis date and am ensuring the date of first exposure occurred prior to the diagnosis date. I would like to create a {pseduo} reference date so that I can do the same for anyone that is control {case_status = 0}. Does anyone have an easy way to do create pseudo reference date, perhaps with a random number generator such as Ranuni? I have no experience using the random number generator options in SAS.

 

Ideally, the range for the pseudo reference date should be between that control's birth date and the day they took the survey. So the date range for the pseduo reference is specific to each control.


Accepted Solutions
Solution
‎01-03-2017 09:21 AM
Trusted Advisor
Posts: 1,374

Re: Creating reference date using random number generator

I came to a similar solution as @ballardw and converted it into a macro:

 

%macro calc_date(dt1, dt2, psedo_dt);   /* calculate a pseodo date between dt1 and dt2 */

       &pseudo_dt = &dt1 + int((&dt2-&dt1)*ranuni(0));

%mend calc_date;

 

data want;

 set have;

     if case_stat = 0 then

      %calc_date(birth_date, survey_date, refer_date);  /* adapt names to your data and needs */

run;

View solution in original post


All Replies
Super User
Posts: 10,497

Re: Creating reference date using random number generator

Here's one way:

data example;
   dob = '23JAN1988'd;
   surveydate= '23APR2011'd;
   pseudodate = dob + (floor((surveydate-dob) * ranuni(457)));
   format dob surveydate pseudodate mmddyy10.;
run;

Surveydate - dob is number of days between the two, multiplying times a uniform random will generate something in the 0 to than number of days range. The Floor is to get an integer number of days.

 

If you want more interval between dob and surveydate then use (surveydate-dob)/2 to get something that will center on half way.

Occasional Contributor
Posts: 13

Re: Creating reference date using random number generator

Thank you for such a quick response. Out of curiosity, since I'm trying to understand this still, do I have to choose a standard DOB for everyone (in your code sample Jan 23 1988)? That is - I can't actually just use the DOB variable that I already have in my dataset to make the pseudo date unique to each control's individual birth date?

Super User
Posts: 10,497

Re: Creating reference date using random number generator

I just provided two values to demonstrate with. Use the line that generates the pseudodate using your variable names. It should work as long as your date of birth and survey date variables are SAS date values.

Solution
‎01-03-2017 09:21 AM
Trusted Advisor
Posts: 1,374

Re: Creating reference date using random number generator

I came to a similar solution as @ballardw and converted it into a macro:

 

%macro calc_date(dt1, dt2, psedo_dt);   /* calculate a pseodo date between dt1 and dt2 */

       &pseudo_dt = &dt1 + int((&dt2-&dt1)*ranuni(0));

%mend calc_date;

 

data want;

 set have;

     if case_stat = 0 then

      %calc_date(birth_date, survey_date, refer_date);  /* adapt names to your data and needs */

run;

☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 232 views
  • 0 likes
  • 3 in conversation