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

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

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
  • 2 replies
  • 2392 views
  • 0 likes
  • 3 in conversation