<?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 Randomly Pick a Value from Other Table in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-Randomly-Pick-a-Value-from-Other-Table/m-p/969956#M376968</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;PRE&gt;proc surveyselect data=tablea out=phone seed=123 sampsize=SampleSize &lt;FONT color="#FF0000"&gt;selectall&lt;/FONT&gt;;
strata state;
run;
&lt;/PRE&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Great idea, &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;, to use PROC SURVEYSELECT, but I'm pretty sure you actually wanted to use the options&lt;/P&gt;
&lt;PRE&gt;method=urs outhits outrandom&lt;/PRE&gt;
&lt;P&gt;instead of &lt;FONT face="courier new,courier"&gt;selectall&lt;/FONT&gt;.&lt;/P&gt;</description>
    <pubDate>Mon, 30 Jun 2025 10:30:38 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2025-06-30T10:30:38Z</dc:date>
    <item>
      <title>How to Randomly Pick a Value from Other Table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Randomly-Pick-a-Value-from-Other-Table/m-p/969939#M376961</link>
      <description>&lt;P&gt;Hi Team,&lt;/P&gt;
&lt;P&gt;Looking for some help here :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've got two SQL tables&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Table A:&amp;nbsp;&lt;/STRONG&gt;It contains a state and Phone numbers. Each state has got around 20 different numbers.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Table B :&lt;/STRONG&gt; This table got accounts, customers and states . It has around 40K accounts.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to randomly assign a phone number from table A to each account in table B, based on their state.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think this can be achieved using first and last variables but unable to come up with a solution. Will be great help if someone can please provide me some guidance here.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is a rough idea of what the tables contain.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Table A:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Balli_0-1751248883054.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/108081i832C923216623DEA/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Balli_0-1751248883054.png" alt="Balli_0-1751248883054.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Table B:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Balli_1-1751249556515.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/108082i2A5DA7A20A030C72/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Balli_1-1751249556515.png" alt="Balli_1-1751249556515.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Jun 2025 02:24:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Randomly-Pick-a-Value-from-Other-Table/m-p/969939#M376961</guid>
      <dc:creator>Balli</dc:creator>
      <dc:date>2025-06-30T02:24:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to Randomly Pick a Value from Other Table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Randomly-Pick-a-Value-from-Other-Table/m-p/969941#M376962</link>
      <description>&lt;P&gt;You could just add a random number to the phone number dataset and sort by STATE and the new variable with the random number to get the phone numbers in random order.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For a more detailed answer we probably need more information.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you want each account and each customer to have their own phone number?&amp;nbsp; Or do you need to use both account and customer together, like a combination key,&amp;nbsp; to assign a phone number?&lt;/P&gt;
&lt;P&gt;Do you want each account/customer pair to get a different number?&lt;/P&gt;
&lt;P&gt;Are there more accounts/customers for any state than the number of phone numbers you have for that state?&amp;nbsp; If so what do you want to do then? Is it ok if two account/customer get the same number?&lt;/P&gt;</description>
      <pubDate>Mon, 30 Jun 2025 02:39:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Randomly-Pick-a-Value-from-Other-Table/m-p/969941#M376962</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-06-30T02:39:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to Randomly Pick a Value from Other Table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Randomly-Pick-a-Value-from-Other-Table/m-p/969942#M376963</link>
      <description>&lt;P&gt;Hi Tom, thanks for your response. the problem is not about randomising the phone numbers, it is more about joining the two datasets. AccountIDs are unique and as I mentioned, there are around 40,00 accounts which means there will be many accounts per state and multiple accounts would have the same phone number.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Jun 2025 03:24:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Randomly-Pick-a-Value-from-Other-Table/m-p/969942#M376963</guid>
      <dc:creator>Balli</dc:creator>
      <dc:date>2025-06-30T03:24:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to Randomly Pick a Value from Other Table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Randomly-Pick-a-Value-from-Other-Table/m-p/969945#M376964</link>
      <description>&lt;P&gt;Please show the expected result making it easier to understand what you really want.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Jun 2025 05:31:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Randomly-Pick-a-Value-from-Other-Table/m-p/969945#M376964</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2025-06-30T05:31:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to Randomly Pick a Value from Other Table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Randomly-Pick-a-Value-from-Other-Table/m-p/969949#M376965</link>
      <description>&lt;P&gt;The approach Tom proposes is not that hard to implement. Please provide usable sample data if you're after code (=instead of screenshots two data steps that create the sample data).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Jun 2025 07:42:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Randomly-Pick-a-Value-from-Other-Table/m-p/969949#M376965</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2025-06-30T07:42:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to Randomly Pick a Value from Other Table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Randomly-Pick-a-Value-from-Other-Table/m-p/969950#M376966</link>
      <description>&lt;P&gt;That would be a lot of help if you could post your data as a Data Step or plain text, not just a picture.&lt;/P&gt;
&lt;P&gt;Nobody would like to type it for you and you would miss the solution from someone.&lt;/P&gt;
&lt;PRE&gt;data tablea;
input state $ phone;
cards;
NSW 111
NSW 112
NSW 123
NSW 114
QLD 121
QLD 211
QLD 141
QLD 411
;
data tableb;
input accountid state $;
cards;
1 NSW
2 QLD
3 VIC
;

proc surveyselect data=tablea out=phone seed=123 sampsize=1;
strata state;
run;
proc sort data=phone;by state;run;
proc sort data=tableb;by state;run;
data want;
 merge tableb(in=ina) phone;
 by state;
 if ina;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Jun 2025 09:27:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Randomly-Pick-a-Value-from-Other-Table/m-p/969950#M376966</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2025-06-30T09:27:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to Randomly Pick a Value from Other Table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Randomly-Pick-a-Value-from-Other-Table/m-p/969951#M376967</link>
      <description>&lt;P&gt;If it is a replaced sampling ,and each accountid from the same state could have different phone.&lt;/P&gt;
&lt;P&gt;Try this one :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data tablea;
input state $ phone;
cards;
NSW 111
NSW 112
NSW 123
NSW 114
QLD 121
QLD 211
QLD 141
QLD 411
;
data tableb;
input accountid state $;
cards;
1 NSW
8 NSW
2 QLD
4 QLD
5 QLD
3 VIC
;

proc sql;
create table SampleSize as
select state,count(*) as SampleSize
 from tableb
  group by state;
quit;

proc surveyselect data=tablea out=phone seed=123 sampsize=SampleSize selectall;
strata state;
run;
proc sort data=phone;by state;run;
proc sort data=tableb;by state;run;
data want;
 merge tableb(in=ina) phone;
 by state;
 if ina;
run;&lt;/PRE&gt;</description>
      <pubDate>Mon, 30 Jun 2025 09:50:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Randomly-Pick-a-Value-from-Other-Table/m-p/969951#M376967</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2025-06-30T09:50:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to Randomly Pick a Value from Other Table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Randomly-Pick-a-Value-from-Other-Table/m-p/969956#M376968</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;PRE&gt;proc surveyselect data=tablea out=phone seed=123 sampsize=SampleSize &lt;FONT color="#FF0000"&gt;selectall&lt;/FONT&gt;;
strata state;
run;
&lt;/PRE&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Great idea, &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;, to use PROC SURVEYSELECT, but I'm pretty sure you actually wanted to use the options&lt;/P&gt;
&lt;PRE&gt;method=urs outhits outrandom&lt;/PRE&gt;
&lt;P&gt;instead of &lt;FONT face="courier new,courier"&gt;selectall&lt;/FONT&gt;.&lt;/P&gt;</description>
      <pubDate>Mon, 30 Jun 2025 10:30:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Randomly-Pick-a-Value-from-Other-Table/m-p/969956#M376968</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2025-06-30T10:30:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to Randomly Pick a Value from Other Table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Randomly-Pick-a-Value-from-Other-Table/m-p/969961#M376970</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/153806"&gt;@Balli&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/153806"&gt;@Balli&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;(...)&lt;/P&gt;
&lt;P&gt;I think this can be achieved using first and last variables ...&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Here is an approach using the FIRST.STATE and LAST.STATE variables: The observation numbers of the first and last observation of each state in dataset HAVE_A (assuming they are grouped) are stored in a temporary lookup table (hash object). While reading dataset HAVE_B they are retrieved and a (uniformly distributed) random number between them is selected using the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p0fpeei0opypg8n1b06qe4r040lv.htm#p0i0papjdn73pzn1kxrjoyygmdfb" target="_blank" rel="noopener"&gt;RAND function&lt;/A&gt;. This random number, in turn, is used in the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/p00hxg3x8lwivcn1f0e9axziw57y.htm#p0u2yq8x2b1fu4n1m0d5hzam9hjv" target="_blank" rel="noopener"&gt;POINT= option&lt;/A&gt; of the SET statement retrieving a phone number from dataset HAVE_A.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Create sample data for demonstration */

%let states='NSW','QLD','VIC','WA','ACT','NT','SA','TAS';

data have_A;
do State=&amp;amp;states;
  do _n_=1 to 23-mod(rank(md5(State)),7);
    Phone=ranuni(2718)*(2**31-1);
    output;
  end;
end;
run;

data have_B;
call streaminit(27182818);
do _n_=1 to 40000;
  AccountID=ranuni(3142)*(2**31-1);
  length State $3;
  State=choosec(rand('integer',8),&amp;amp;states);
  output;
end;
run;

proc sql;
insert into have_B
values(123456789,'XYZ');
quit;


/* Assign a randomly selected phone number from have_A to each account in have_B, based on their state */

data want(drop=n1 n2);
call streaminit(31415927);
if _n_=1 then do;
  if 0 then set have_B have_A;
  dcl hash h(); /* lookup table for the first and last obs. number per state in have_A */
  h.definekey('State');
  h.definedata('n1','n2');
  h.definedone();
  do _n_=1 by 1 until(last);
    set have_A end=last;
    by state notsorted; /* Dataset have_A must be grouped by State. */
    if first.state then n1=_n_;
    if last.state then do;
      n2=_n_;
      h.add();
    end;
  end;
end;
set have_b;
if h.find()=0 then do;
  _n_=rand('integer',n1,n2);
  set have_A(keep=phone) point=_n_;
end;
else call missing(phone);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Alternatively, you could store the phone numbers in the hash object and retrieve them from there (e.g., using a sequence number as a second key item).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But I think a solution using PROC SURVEYSELECT (&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;'s idea) is easier to understand.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit: Inserted the ELSE statement at the end of the program to avoid retaining a phone number from the previous observation if an unexpected value of &lt;FONT face="courier new,courier"&gt;State&lt;/FONT&gt; occurs.&lt;/P&gt;</description>
      <pubDate>Mon, 30 Jun 2025 14:30:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Randomly-Pick-a-Value-from-Other-Table/m-p/969961#M376970</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2025-06-30T14:30:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to Randomly Pick a Value from Other Table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Randomly-Pick-a-Value-from-Other-Table/m-p/970012#M376988</link>
      <description>&lt;P&gt;You have a small dataset of phone numbers by state, and a "large" dataset of accounts.&amp;nbsp; I understand that you are fine with randomly assigning a given phone number to multiple qualifying accounts.&amp;nbsp; Here's a program that avoids the need to sort the large dataset just to facilitate a merge:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Using&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;'s sample data:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data phone_arrays (keep=state _nphones _col:);
  do _nphones=1 by 1 until (last.state);
    set tablea  (where=(not missing(phone)));
    by state;
    array _col {50};
    _col{_nphones}=phone;
  end;
run;

data want (drop=_:);
  set tableb;
  if _n_=1 then do;
    if 0 then set phone_arrays ;
    declare hash h (dataset:'phone_arrays');
      h.definekey('state'); 
      h.definedata(all:'Y');
      h.definedone();
  end;
  array col {*} _col: ;
  
  if h.find()=0 then phnum=col{ceil(_nphones*ranuni(1508915))};
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The _COL array is given an arbitrary size -- large enough to account for the largest group of available phone numbers.&lt;/P&gt;
&lt;P&gt;This assumes the TABLEA dataset is already sorted by state.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 01 Jul 2025 00:49:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Randomly-Pick-a-Value-from-Other-Table/m-p/970012#M376988</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2025-07-01T00:49:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to Randomly Pick a Value from Other Table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Randomly-Pick-a-Value-from-Other-Table/m-p/970073#M377007</link>
      <description>Thanks. appreciate your response. This is very quick and elegant solution. Exactly what I was after &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Wed, 02 Jul 2025 01:37:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Randomly-Pick-a-Value-from-Other-Table/m-p/970073#M377007</guid>
      <dc:creator>Balli</dc:creator>
      <dc:date>2025-07-02T01:37:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to Randomly Pick a Value from Other Table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Randomly-Pick-a-Value-from-Other-Table/m-p/970074#M377008</link>
      <description>&lt;P&gt;Thanks everyone for the response and sorry for not including the sample data and the expected results. I will try to include more details next time.&lt;/P&gt;</description>
      <pubDate>Wed, 02 Jul 2025 01:41:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Randomly-Pick-a-Value-from-Other-Table/m-p/970074#M377008</guid>
      <dc:creator>Balli</dc:creator>
      <dc:date>2025-07-02T01:41:47Z</dc:date>
    </item>
  </channel>
</rss>

