<?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 Re: How to select random rows from SQL all-to-all join? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-random-rows-from-SQL-all-to-all-join/m-p/588828#M168347</link>
    <description>&lt;P&gt;Thank you!!&lt;/P&gt;</description>
    <pubDate>Sun, 15 Sep 2019 13:47:16 GMT</pubDate>
    <dc:creator>jmoody77</dc:creator>
    <dc:date>2019-09-15T13:47:16Z</dc:date>
    <item>
      <title>How to select random rows from SQL all-to-all join?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-random-rows-from-SQL-all-to-all-join/m-p/588734#M168305</link>
      <description>&lt;P&gt;Hi -&lt;/P&gt;&lt;P&gt;I'm doing a conditional all-to-all merge in SQL with the code below.&amp;nbsp; It works; but seems silly to have to create the table twice.&amp;nbsp; I'm looking for a way to select randomly from the rows of the joined table before writing it out.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
 create table inftrans as select
  a.patientid as patientid, a.hospsize as hospsize, b.patientid as inf_source, 1 as newinf,
  a.hospid as whereinf, a.daysin as dayinf 
  from work.patients (where=(infected=0)) as a, work.patients (where=(infected=1)) as b
  where a.hospid=b.hospid &amp;amp; a.patientid^=b.patientid;
  create table infrtrans as select * from inftrans
    where ranbin(0,1,10/(hospsize**2))=1;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;When I try adding the condition to the first where statement like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
 create table inftrans as select
  a.patientid as patientid, a.hospsize as hospsize, b.patientid as inf_source, 1 as newinf,
  a.hospid as whereinf, a.daysin as dayinf 
  from work.patients (where=(infected=0)) as a, work.patients (where=(infected=1)) as b
  where a.hospid=b.hospid &amp;amp; a.patientid^=b.patientid &amp;amp; ranbin(0,1,10/(hospsize**2))=1;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;inftrans will apply the ranbin() condition to a single row from the input dataset &amp;amp; thus be true multiple times.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So the question is if there is a cleaner way to get a random subset of the dyad-level dataset?&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;&lt;P&gt;Jim&lt;/P&gt;</description>
      <pubDate>Sat, 14 Sep 2019 16:06:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-random-rows-from-SQL-all-to-all-join/m-p/588734#M168305</guid>
      <dc:creator>jmoody77</dc:creator>
      <dc:date>2019-09-14T16:06:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to select random rows from SQL all-to-all join?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-random-rows-from-SQL-all-to-all-join/m-p/588750#M168308</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/290271"&gt;@jmoody77&lt;/a&gt;&amp;nbsp;and welcome to the SAS Support Communities!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could apply the condition involving RANBIN via a WHERE= dataset option in the first CREATE TABLE statement:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;create table infrtrans(where=(ranbin(...)=1)) as ...&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(Personally, I would prefer a positive random seed value to obtain a reproducible dataset, but that's your decision.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another option would be to create a view (&lt;A href="https://documentation.sas.com/?docsetId=sqlproc&amp;amp;docsetTarget=n0nolnbokay91in1gouzgw3xzl5e.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_blank" rel="noopener"&gt;CREATE VIEW statement&lt;/A&gt;) and to perform the random selection based on the view (be it with PROC SQL as you did or with PROC SURVEYSELECT if SAS/STAT is available or in a DATA step).&lt;/P&gt;</description>
      <pubDate>Sat, 14 Sep 2019 18:12:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-random-rows-from-SQL-all-to-all-join/m-p/588750#M168308</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-09-14T18:12:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to select random rows from SQL all-to-all join?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-random-rows-from-SQL-all-to-all-join/m-p/588828#M168347</link>
      <description>&lt;P&gt;Thank you!!&lt;/P&gt;</description>
      <pubDate>Sun, 15 Sep 2019 13:47:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-random-rows-from-SQL-all-to-all-join/m-p/588828#M168347</guid>
      <dc:creator>jmoody77</dc:creator>
      <dc:date>2019-09-15T13:47:16Z</dc:date>
    </item>
  </channel>
</rss>

