turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- Problems creating random numbers for dates

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

07-15-2009 10:59 AM

I'm having a problem using the random number function for dates that have missing days.

Does anyone know how to get around assigning non existent days to certain months? As it is, I had to cut the number off at 28 in order not to get errors.

Using the following code:

randday= 1+31*ranuni(1);

randday2= INT(randday);

but had to change it to:

randday= 1+28*ranuni(1);

randday2= INT(randday);

to avoid errors for non-existent days by month.

Thanks!

Does anyone know how to get around assigning non existent days to certain months? As it is, I had to cut the number off at 28 in order not to get errors.

Using the following code:

randday= 1+31*ranuni(1);

randday2= INT(randday);

but had to change it to:

randday= 1+28*ranuni(1);

randday2= INT(randday);

to avoid errors for non-existent days by month.

Thanks!

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to statadm

07-15-2009 11:30 AM

Assuming you know the month, then this would work (ignores leap year).

IF month IN (9,4,6,11) THEN randday= 1+30*ranuni(1);

ELSE IF month = 2 then randday= 1+28*ranuni(1);

ELSE randday= 1+31*ranuni(1);

randday2= INT(randday);

IF month IN (9,4,6,11) THEN randday= 1+30*ranuni(1);

ELSE IF month = 2 then randday= 1+28*ranuni(1);

ELSE randday= 1+31*ranuni(1);

randday2= INT(randday);

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Doc_Duke

07-15-2009 11:41 AM

This seems to be able to work for one date, but I'm using this in a macro for multiple dates with the following code:

IF DELIV&_I._DAY=. then do;

DELIV&_I._DAY=RANDDAY2;

end;

DELIV&_I._DATE=MDY(DELIV&_I._MONTH, DELIV&_I._DAY, DELIV&_I._YEAR);

I'm going to try the code using this prior to assigning radday2 to the day variable:

IF DELIV&_I._MONTH IN (9,4,6,11) THEN randday= 1+30*ranuni(1);

ELSE IF DELIV&_I._MONTH = 2 then randday= 1+28*ranuni(1);

ELSE randday= 1+31*ranuni(1);

randday2= INT(randday);

Thanks for your help! Hopefully it works.

IF DELIV&_I._DAY=. then do;

DELIV&_I._DAY=RANDDAY2;

end;

DELIV&_I._DATE=MDY(DELIV&_I._MONTH, DELIV&_I._DAY, DELIV&_I._YEAR);

I'm going to try the code using this prior to assigning radday2 to the day variable:

IF DELIV&_I._MONTH IN (9,4,6,11) THEN randday= 1+30*ranuni(1);

ELSE IF DELIV&_I._MONTH = 2 then randday= 1+28*ranuni(1);

ELSE randday= 1+31*ranuni(1);

randday2= INT(randday);

Thanks for your help! Hopefully it works.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to statadm

07-15-2009 12:18 PM

Assuming you know the month and the year you could use the functions

end_of_month =

intnx('Month',MDY(DELIV&_I._MONTH,1, DELIV&_I._YEAR),0,'E');

maxdays = day (end_of_month);

to determine the number of days in the month and then use your random funtion

as follows

randday= int(1+maxdays*ranuni(1));

(The 0 tells SAS to advance the date specified (MDY(DELIV&_I._MONTH,1, DELIV&_I._YEAR) 0 months , the "E" sets that date to the end of the month, day returens the day part of the last date in the month.

Hope this helps

end_of_month =

intnx('Month',MDY(DELIV&_I._MONTH,1, DELIV&_I._YEAR),0,'E');

maxdays = day (end_of_month);

to determine the number of days in the month and then use your random funtion

as follows

randday= int(1+maxdays*ranuni(1));

(The 0 tells SAS to advance the date specified (MDY(DELIV&_I._MONTH,1, DELIV&_I._YEAR) 0 months , the "E" sets that date to the end of the month, day returens the day part of the last date in the month.

Hope this helps

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

07-15-2009 12:30 PM

Great, so this should actually account for leap year in the process.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to statadm

07-15-2009 03:24 PM

assuming you know the min and max of allowable dates (01jan2005 - 30jun2009, for example), you can use random functions.

[pre]

data randates;

mindate='01jan2005'd;

maxdate='30jun2009'd;

range = maxdate-mindate+1;

format mindate maxdate randate date9.;

do i = 1 to 10000;

RanDate = mindate + int(ranuni(12345)*range);

output;

end;

run;

[/pre]

using this method, you don't need to find the number of days in a given month/year, SAS just knows.

a simple min/max check shows the date limits were respected

[pre]

proc sql;

select distinct

min(randate) format=date9.

, max(randate) format=date9.

from randates;

quit;

[/pre]

output

[pre]

--------------------

01JAN2005 30JUN2009

[/pre]

[pre]

data randates;

mindate='01jan2005'd;

maxdate='30jun2009'd;

range = maxdate-mindate+1;

format mindate maxdate randate date9.;

do i = 1 to 10000;

RanDate = mindate + int(ranuni(12345)*range);

output;

end;

run;

[/pre]

using this method, you don't need to find the number of days in a given month/year, SAS just knows.

a simple min/max check shows the date limits were respected

[pre]

proc sql;

select distinct

min(randate) format=date9.

, max(randate) format=date9.

from randates;

quit;

[/pre]

output

[pre]

--------------------

01JAN2005 30JUN2009

[/pre]