DATA Step, Macro, Functions and more

Random Number generator to impute month and day as part of date question

Reply
Frequent Contributor
Posts: 78

Random Number generator to impute month and day as part of date question

I have a program to impute either a missing day or month and day using ranuni.

The problem is that the random date sometimes falls into a range that is impossible, ie. less than a start date.

Does anyone know of a way to restrict this randomization so that the imputed day/month is not less than the day/month of the start date if they fall within the same month/year ?

Thanks in advance !
Super Contributor
Super Contributor
Posts: 3,174

Re: Random Number generator to impute month and day as part of date question

You can use the INTNX function to set a low/high start/stop range within your code.

DATA _NULL_;
FORMAT LOWDATE HIDATE DATE9.;
LOWDATE = INTNX('MONTH',TODAY(),-12,'SAMEDAY');
HIDATE = INTNX('MONTH',TODAY(),12,'SAMEDAY');
PUTLOG _ALL_;
RUN;

Scott Barry
SBBWorks, Inc.

Useful SAS support website reference on this topic:

http://support.sas.com/techsup/technote/ts668.pdf
Respected Advisor
Posts: 3,902

Re: Random Number generator to impute month and day as part of date question

I believe the code below does what you're looking for:

data _null_;
format StartDate RandomDate date9.;
StartDate=today();
DateRange=60;
do i=1 to 20;
/* RandomDate within range StartDate to Startdate+59 days */
RandomDate=Startdate+floor(ranuni(0)*DateRange);
put RandomDate=;
end;
run;

If you need to calculate the date range more dynamically then look at Scott's code.

HTH
Patrick
Ask a Question
Discussion stats
  • 2 replies
  • 1074 views
  • 0 likes
  • 3 in conversation