<?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 Randomly assign a date (which also satisfies a condition) from another list of possible dates in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Randomly-assign-a-date-which-also-satisfies-a-condition-from/m-p/577832#M163778</link>
    <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to assign a randomly selected date to observations in data set A from another list of possible dates in data set B. However, for some observations in data set A, there is another date variable and I want the randomly selected date (with replacement) from data set B to be at least 30 days before the observed data in data set A. Below is how the two data sets (A &amp;amp; B) look and the want data set C:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Data set A&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp;date1&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 02JAN2018&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 31JUL2000&lt;/P&gt;&lt;P&gt;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Data set B&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;date2&lt;/P&gt;&lt;P&gt;30JUL2019&lt;/P&gt;&lt;P&gt;03MAR1999&lt;/P&gt;&lt;P&gt;09SEP2016&lt;/P&gt;&lt;P&gt;01JAN2017&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Want Data set C&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp;date1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;date2&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01JAN2017&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 02JAN2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 03MAR1999&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 30JUL2019&lt;/P&gt;&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 31JUL2000&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;03MAR1999&lt;/P&gt;&lt;P&gt;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 01JAN2017&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;How could I do this? &lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Greatly appreciate your help.&amp;nbsp; Regards, -Raj&lt;/P&gt;</description>
    <pubDate>Tue, 30 Jul 2019 17:30:20 GMT</pubDate>
    <dc:creator>raj00728</dc:creator>
    <dc:date>2019-07-30T17:30:20Z</dc:date>
    <item>
      <title>Randomly assign a date (which also satisfies a condition) from another list of possible dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Randomly-assign-a-date-which-also-satisfies-a-condition-from/m-p/577832#M163778</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to assign a randomly selected date to observations in data set A from another list of possible dates in data set B. However, for some observations in data set A, there is another date variable and I want the randomly selected date (with replacement) from data set B to be at least 30 days before the observed data in data set A. Below is how the two data sets (A &amp;amp; B) look and the want data set C:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Data set A&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp;date1&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 02JAN2018&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 31JUL2000&lt;/P&gt;&lt;P&gt;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Data set B&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;date2&lt;/P&gt;&lt;P&gt;30JUL2019&lt;/P&gt;&lt;P&gt;03MAR1999&lt;/P&gt;&lt;P&gt;09SEP2016&lt;/P&gt;&lt;P&gt;01JAN2017&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Want Data set C&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp;date1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;date2&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01JAN2017&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 02JAN2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 03MAR1999&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 30JUL2019&lt;/P&gt;&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 31JUL2000&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;03MAR1999&lt;/P&gt;&lt;P&gt;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 01JAN2017&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;How could I do this? &lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Greatly appreciate your help.&amp;nbsp; Regards, -Raj&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jul 2019 17:30:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Randomly-assign-a-date-which-also-satisfies-a-condition-from/m-p/577832#M163778</guid>
      <dc:creator>raj00728</dc:creator>
      <dc:date>2019-07-30T17:30:20Z</dc:date>
    </item>
    <item>
      <title>Re: Randomly assign a date (which also satisfies a condition) from another list of possible dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Randomly-assign-a-date-which-also-satisfies-a-condition-from/m-p/577851#M163779</link>
      <description>&lt;P&gt;Dealing with random numbers in SQL is always a bit tricky, but here goes:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want(drop=rnd) as
select ID, date1, date2, rand("uniform") as rnd
from A, B
where A.date1 &amp;lt;= intnx("day", date2, 30)
group by ID
having rnd = min(rnd);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note: I used the fact that missing values are always inferior to non-missing values in SAS (including SAS/SQL).&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jul 2019 18:17:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Randomly-assign-a-date-which-also-satisfies-a-condition-from/m-p/577851#M163779</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-07-30T18:17:05Z</dc:date>
    </item>
    <item>
      <title>Re: Randomly assign a date (which also satisfies a condition) from another list of possible dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Randomly-assign-a-date-which-also-satisfies-a-condition-from/m-p/577862#M163787</link>
      <description>&lt;P&gt;Hi PG,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for a quick response.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I ran your code for my data, but it is not working as per my need. The observations where there is "date1" in data set A, I would like to have the randomly selected date (from data set B) to be at least 30 days before the "date1."&lt;/P&gt;&lt;P&gt;I am also loosing observations from data set A, which should not be the case if the random selection is with replacement.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jul 2019 18:43:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Randomly-assign-a-date-which-also-satisfies-a-condition-from/m-p/577862#M163787</guid>
      <dc:creator>raj00728</dc:creator>
      <dc:date>2019-07-30T18:43:01Z</dc:date>
    </item>
    <item>
      <title>Re: Randomly assign a date (which also satisfies a condition) from another list of possible dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Randomly-assign-a-date-which-also-satisfies-a-condition-from/m-p/577864#M163789</link>
      <description>&lt;P&gt;Could you provide a representative example of those problem cases?&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jul 2019 18:47:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Randomly-assign-a-date-which-also-satisfies-a-condition-from/m-p/577864#M163789</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-07-30T18:47:31Z</dc:date>
    </item>
    <item>
      <title>Re: Randomly assign a date (which also satisfies a condition) from another list of possible dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Randomly-assign-a-date-which-also-satisfies-a-condition-from/m-p/577871#M163794</link>
      <description>&lt;P&gt;Here is the screenshot of the data set I get after running the code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID is &lt;STRONG&gt;ID&lt;/STRONG&gt; from data set A&lt;/P&gt;&lt;P&gt;DOD is &lt;STRONG&gt;date1&lt;/STRONG&gt; from data set A&lt;/P&gt;&lt;P&gt;KC_Dx_Dt is &lt;STRONG&gt;date2&lt;/STRONG&gt; which should be randomly selected from data set B.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For ex., for ID=3 or 4, the condition that &lt;EM&gt;date2 (KC_Dx_Dt) should be at least 30 day before date1 (DOD)&lt;/EM&gt;&amp;nbsp;is not meeting.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, I had 100 observations in data set A, however, here in the want data set, I have 98 observations.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Picture1.jpg" style="width: 128px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/31425i5AF7A14195D72CE8/image-size/small?v=v2&amp;amp;px=200" role="button" title="Picture1.jpg" alt="Picture1.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jul 2019 21:07:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Randomly-assign-a-date-which-also-satisfies-a-condition-from/m-p/577871#M163794</guid>
      <dc:creator>raj00728</dc:creator>
      <dc:date>2019-07-30T21:07:57Z</dc:date>
    </item>
    <item>
      <title>Re: Randomly assign a date (which also satisfies a condition) from another list of possible dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Randomly-assign-a-date-which-also-satisfies-a-condition-from/m-p/577884#M163798</link>
      <description>&lt;P&gt;So, I had gotten it backwards... Try this instead:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want(drop=rnd) as
select ID, date1, date2, rand("uniform") as rnd
from A, B
where -date1 &amp;lt;= -intnx("day", date2, 30)
group by ID
having rnd = min(rnd);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(here again, handling comparisons involving missing values requires a bit of gymnastics. I use the fact that -Missing = Missing &amp;lt; Non-missing)&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jul 2019 19:41:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Randomly-assign-a-date-which-also-satisfies-a-condition-from/m-p/577884#M163798</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-07-30T19:41:19Z</dc:date>
    </item>
    <item>
      <title>Re: Randomly assign a date (which also satisfies a condition) from another list of possible dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Randomly-assign-a-date-which-also-satisfies-a-condition-from/m-p/577885#M163799</link>
      <description>&lt;P&gt;Thank you. This code is picking/assigning a &lt;STRONG&gt;date2&lt;/STRONG&gt; which meets the condition, however, I am still loosing observations. Now have 95 observations instead of 100.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Revised Response:&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;This is code is working fine. I have also figured out the reason why I was having lesser number of observations in my "want" data set. It was because there was no "date" from the "data set B" which could have satisfied the condition for a date value in data set A (date1).&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Thank you, PG!&amp;nbsp;&lt;/EM&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jul 2019 20:07:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Randomly-assign-a-date-which-also-satisfies-a-condition-from/m-p/577885#M163799</guid>
      <dc:creator>raj00728</dc:creator>
      <dc:date>2019-07-30T20:07:58Z</dc:date>
    </item>
    <item>
      <title>Re: Randomly assign a date (which also satisfies a condition) from another list of possible dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Randomly-assign-a-date-which-also-satisfies-a-condition-from/m-p/577893#M163806</link>
      <description>&lt;P&gt;&lt;SPAN&gt;How could I add a&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;"seed"&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;number?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jul 2019 20:32:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Randomly-assign-a-date-which-also-satisfies-a-condition-from/m-p/577893#M163806</guid>
      <dc:creator>raj00728</dc:creator>
      <dc:date>2019-07-30T20:32:11Z</dc:date>
    </item>
    <item>
      <title>Re: Randomly assign a date (which also satisfies a condition) from another list of possible dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Randomly-assign-a-date-which-also-satisfies-a-condition-from/m-p/577914#M163818</link>
      <description>&lt;P&gt;As far as I can tell, you can't give a seed to the rand function in SQL. You could replace rand("uniform") with ranuni(767667) where 767667 is the seed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that ranuni is a deprecated function. From the SAS documentation:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;"&lt;EM&gt;The [RANUNI] function is suitable for small samples and for applications that do not require a sophisticated random-number generator.&lt;/EM&gt;"&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jul 2019 22:34:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Randomly-assign-a-date-which-also-satisfies-a-condition-from/m-p/577914#M163818</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-07-30T22:34:48Z</dc:date>
    </item>
    <item>
      <title>Re: Randomly assign a date (which also satisfies a condition) from another list of possible dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Randomly-assign-a-date-which-also-satisfies-a-condition-from/m-p/577916#M163820</link>
      <description>&lt;P&gt;If you want all obs from dataset A, with missing values for date2 when there is no candidate from dataset B, change the code to:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want(drop=rnd) as
select ID, date1, date2, ranuni(767667) as rnd
from A left join B
on -date1 &amp;lt;= -intnx("day", date2, 30)
group by ID
having rnd = min(rnd);
select * from want;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 30 Jul 2019 22:43:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Randomly-assign-a-date-which-also-satisfies-a-condition-from/m-p/577916#M163820</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-07-30T22:43:57Z</dc:date>
    </item>
    <item>
      <title>Re: Randomly assign a date (which also satisfies a condition) from another list of possible dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Randomly-assign-a-date-which-also-satisfies-a-condition-from/m-p/577934#M163834</link>
      <description>&lt;P&gt;Thank you for the info and your help!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards, -Raj&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jul 2019 01:14:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Randomly-assign-a-date-which-also-satisfies-a-condition-from/m-p/577934#M163834</guid>
      <dc:creator>raj00728</dc:creator>
      <dc:date>2019-07-31T01:14:29Z</dc:date>
    </item>
  </channel>
</rss>

