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-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!

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
  • 2232 views
  • 0 likes
  • 3 in conversation