BookmarkSubscribeRSS Feed
statadm
Fluorite | Level 6
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 !
2 REPLIES 2
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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
Patrick
Opal | Level 21
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

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 2786 views
  • 0 likes
  • 3 in conversation