<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Problems creating random numbers for dates in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Problems-creating-random-numbers-for-dates/m-p/54921#M11691</link>
    <description>I'm having a problem using the random number function for dates that have missing days.  &lt;BR /&gt;
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.&lt;BR /&gt;
&lt;BR /&gt;
Using the following code:&lt;BR /&gt;
&lt;BR /&gt;
randday= 1+31*ranuni(1); &lt;BR /&gt;
randday2= INT(randday);&lt;BR /&gt;
&lt;BR /&gt;
but had to change it to:&lt;BR /&gt;
&lt;BR /&gt;
randday= 1+28*ranuni(1); &lt;BR /&gt;
randday2= INT(randday);&lt;BR /&gt;
&lt;BR /&gt;
to avoid errors for non-existent days by month.&lt;BR /&gt;
Thanks!</description>
    <pubDate>Wed, 15 Jul 2009 14:59:47 GMT</pubDate>
    <dc:creator>statadm</dc:creator>
    <dc:date>2009-07-15T14:59:47Z</dc:date>
    <item>
      <title>Problems creating random numbers for dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problems-creating-random-numbers-for-dates/m-p/54921#M11691</link>
      <description>I'm having a problem using the random number function for dates that have missing days.  &lt;BR /&gt;
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.&lt;BR /&gt;
&lt;BR /&gt;
Using the following code:&lt;BR /&gt;
&lt;BR /&gt;
randday= 1+31*ranuni(1); &lt;BR /&gt;
randday2= INT(randday);&lt;BR /&gt;
&lt;BR /&gt;
but had to change it to:&lt;BR /&gt;
&lt;BR /&gt;
randday= 1+28*ranuni(1); &lt;BR /&gt;
randday2= INT(randday);&lt;BR /&gt;
&lt;BR /&gt;
to avoid errors for non-existent days by month.&lt;BR /&gt;
Thanks!</description>
      <pubDate>Wed, 15 Jul 2009 14:59:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problems-creating-random-numbers-for-dates/m-p/54921#M11691</guid>
      <dc:creator>statadm</dc:creator>
      <dc:date>2009-07-15T14:59:47Z</dc:date>
    </item>
    <item>
      <title>Re: Problems creating random numbers for dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problems-creating-random-numbers-for-dates/m-p/54922#M11692</link>
      <description>Assuming you know the month, then this would work (ignores leap year).&lt;BR /&gt;
&lt;BR /&gt;
IF month IN (9,4,6,11) THEN randday= 1+30*ranuni(1);&lt;BR /&gt;
ELSE IF month = 2 then randday= 1+28*ranuni(1);&lt;BR /&gt;
ELSE randday= 1+31*ranuni(1);&lt;BR /&gt;
randday2= INT(randday);</description>
      <pubDate>Wed, 15 Jul 2009 15:30:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problems-creating-random-numbers-for-dates/m-p/54922#M11692</guid>
      <dc:creator>Doc_Duke</dc:creator>
      <dc:date>2009-07-15T15:30:31Z</dc:date>
    </item>
    <item>
      <title>Re: Problems creating random numbers for dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problems-creating-random-numbers-for-dates/m-p/54923#M11693</link>
      <description>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:&lt;BR /&gt;
&lt;BR /&gt;
	&lt;BR /&gt;
			IF DELIV&amp;amp;_I._DAY=. then do;&lt;BR /&gt;
				DELIV&amp;amp;_I._DAY=RANDDAY2;&lt;BR /&gt;
				end;&lt;BR /&gt;
&lt;BR /&gt;
		&lt;BR /&gt;
DELIV&amp;amp;_I._DATE=MDY(DELIV&amp;amp;_I._MONTH, DELIV&amp;amp;_I._DAY, DELIV&amp;amp;_I._YEAR);&lt;BR /&gt;
&lt;BR /&gt;
I'm going to try the code using this prior to assigning radday2 to the day variable:&lt;BR /&gt;
&lt;BR /&gt;
	IF DELIV&amp;amp;_I._MONTH IN (9,4,6,11) THEN randday= 1+30*ranuni(1);&lt;BR /&gt;
			ELSE IF DELIV&amp;amp;_I._MONTH = 2 then randday= 1+28*ranuni(1);&lt;BR /&gt;
			ELSE randday= 1+31*ranuni(1);&lt;BR /&gt;
			randday2= INT(randday); &lt;BR /&gt;
&lt;BR /&gt;
Thanks for your help!  Hopefully it works.</description>
      <pubDate>Wed, 15 Jul 2009 15:41:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problems-creating-random-numbers-for-dates/m-p/54923#M11693</guid>
      <dc:creator>statadm</dc:creator>
      <dc:date>2009-07-15T15:41:33Z</dc:date>
    </item>
    <item>
      <title>Re: Problems creating random numbers for dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problems-creating-random-numbers-for-dates/m-p/54924#M11694</link>
      <description>Assuming you know the month and the year you could use the functions &lt;BR /&gt;
&lt;BR /&gt;
  end_of_month = &lt;BR /&gt;
      intnx('Month',MDY(DELIV&amp;amp;_I._MONTH,1, DELIV&amp;amp;_I._YEAR),0,'E'); &lt;BR /&gt;
  maxdays = day (end_of_month);&lt;BR /&gt;
&lt;BR /&gt;
to determine the number of days in the month and then use your random funtion&lt;BR /&gt;
as follows&lt;BR /&gt;
&lt;BR /&gt;
  randday= int(1+maxdays*ranuni(1)); &lt;BR /&gt;
&lt;BR /&gt;
(The 0 tells SAS to advance the date specified (MDY(DELIV&amp;amp;_I._MONTH,1, DELIV&amp;amp;_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.&lt;BR /&gt;
&lt;BR /&gt;
Hope this helps</description>
      <pubDate>Wed, 15 Jul 2009 16:18:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problems-creating-random-numbers-for-dates/m-p/54924#M11694</guid>
      <dc:creator>LAP</dc:creator>
      <dc:date>2009-07-15T16:18:18Z</dc:date>
    </item>
    <item>
      <title>Re: Problems creating random numbers for dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problems-creating-random-numbers-for-dates/m-p/54925#M11695</link>
      <description>Great, so this should actually account for leap year in the process.</description>
      <pubDate>Wed, 15 Jul 2009 16:30:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problems-creating-random-numbers-for-dates/m-p/54925#M11695</guid>
      <dc:creator>statadm</dc:creator>
      <dc:date>2009-07-15T16:30:54Z</dc:date>
    </item>
    <item>
      <title>Re: Problems creating random numbers for dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problems-creating-random-numbers-for-dates/m-p/54926#M11696</link>
      <description>assuming you know the min and max of allowable dates (01jan2005 - 30jun2009, for example), you can use random functions.&lt;BR /&gt;
[pre]&lt;BR /&gt;
data randates;&lt;BR /&gt;
  mindate='01jan2005'd;&lt;BR /&gt;
  maxdate='30jun2009'd;&lt;BR /&gt;
  range = maxdate-mindate+1;&lt;BR /&gt;
  format mindate maxdate randate date9.;&lt;BR /&gt;
  do i = 1 to 10000;&lt;BR /&gt;
    RanDate = mindate + int(ranuni(12345)*range);&lt;BR /&gt;
    output;&lt;BR /&gt;
  end;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
using this method, you don't need to find the number of days in a given month/year, SAS just knows.&lt;BR /&gt;
&lt;BR /&gt;
a simple min/max check shows the date limits were respected&lt;BR /&gt;
[pre]&lt;BR /&gt;
proc sql;&lt;BR /&gt;
  select distinct&lt;BR /&gt;
    min(randate) format=date9.&lt;BR /&gt;
  , max(randate) format=date9.&lt;BR /&gt;
  from randates;&lt;BR /&gt;
quit;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
output&lt;BR /&gt;
[pre]&lt;BR /&gt;
--------------------&lt;BR /&gt;
01JAN2005  30JUN2009&lt;BR /&gt;
[/pre]</description>
      <pubDate>Wed, 15 Jul 2009 19:24:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problems-creating-random-numbers-for-dates/m-p/54926#M11696</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-07-15T19:24:46Z</dc:date>
    </item>
  </channel>
</rss>

