<?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: Random character string with no duplicates in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Random-character-string-with-no-duplicates/m-p/829269#M327621</link>
    <description>&lt;P&gt;It seems to me that the program results in an infinite loop because the looping variable i is decremented and never reaches 500000.&lt;/P&gt;</description>
    <pubDate>Thu, 18 Aug 2022 18:58:41 GMT</pubDate>
    <dc:creator>Rick_SAS</dc:creator>
    <dc:date>2022-08-18T18:58:41Z</dc:date>
    <item>
      <title>Random character string with no duplicates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Random-character-string-with-no-duplicates/m-p/829232#M327603</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I need to create a variable with a random 7 character string with no duplicates. This is what I've tried so far but there are some duplicates. I'm not sure how to do this.&amp;nbsp; Can anyone help?&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data a(keep=final);&lt;BR /&gt;length final $7;&lt;BR /&gt;do i=1 to 500000;&lt;BR /&gt;do j=1 to 7;&lt;BR /&gt;substr(final,j)=byte(int(65+26*ranuni(0)));&lt;BR /&gt;end;&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Aug 2022 16:11:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Random-character-string-with-no-duplicates/m-p/829232#M327603</guid>
      <dc:creator>KatLinden</dc:creator>
      <dc:date>2022-08-18T16:11:47Z</dc:date>
    </item>
    <item>
      <title>Re: Random character string with no duplicates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Random-character-string-with-no-duplicates/m-p/829233#M327604</link>
      <description>&lt;P&gt;&lt;SPAN&gt;data a(keep=final);&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;length final $7;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;do i=1 to 500000;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;do j=1 to 7;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;bytep=&lt;SPAN&gt;byte(int(65+26*ranuni(0)));&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;if index(final,bytep) &amp;lt;=0 then do;&lt;BR /&gt;&lt;SPAN&gt;substr(final,j)=byte(int(65+26*ranuni(0)));&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;end;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;else;&lt;/P&gt;
&lt;P&gt;i = i-1;&lt;/P&gt;
&lt;P&gt;end;&lt;BR /&gt;&lt;SPAN&gt;end;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;output;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;end;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Aug 2022 16:17:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Random-character-string-with-no-duplicates/m-p/829233#M327604</guid>
      <dc:creator>smantha</dc:creator>
      <dc:date>2022-08-18T16:17:16Z</dc:date>
    </item>
    <item>
      <title>Re: Random character string with no duplicates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Random-character-string-with-no-duplicates/m-p/829249#M327612</link>
      <description>&lt;P&gt;Define "no duplicates". No duplicates of character within single value? No adjacent characters with same character? No overall value duplicating another?&lt;/P&gt;</description>
      <pubDate>Thu, 18 Aug 2022 17:21:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Random-character-string-with-no-duplicates/m-p/829249#M327612</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-08-18T17:21:00Z</dc:date>
    </item>
    <item>
      <title>Re: Random character string with no duplicates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Random-character-string-with-no-duplicates/m-p/829250#M327613</link>
      <description>&lt;P&gt;&lt;SPAN&gt;No overall value duplicating another.&amp;nbsp; Same character within a value is OK.&amp;nbsp; Thanks!&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Aug 2022 17:23:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Random-character-string-with-no-duplicates/m-p/829250#M327613</guid>
      <dc:creator>KatLinden</dc:creator>
      <dc:date>2022-08-18T17:23:54Z</dc:date>
    </item>
    <item>
      <title>Re: Random character string with no duplicates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Random-character-string-with-no-duplicates/m-p/829259#M327615</link>
      <description>&lt;P&gt;To avoid replicated characters within a value use the permutation (k out of n) routine:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
array a $1 a_1-a_26;
do i = 1 to dim(a);
    a{i} = byte(i - 1 + rank("A"));
    end;
seed = 87896;
length final $7;
do i = 1 to 50;
    call ranperk(seed, 7, of a{*});
    final = cats(of a_1-a_7);
    output;
    end;
stop;
keep i final;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Aug 2022 17:51:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Random-character-string-with-no-duplicates/m-p/829259#M327615</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2022-08-18T17:51:37Z</dc:date>
    </item>
    <item>
      <title>Re: Random character string with no duplicates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Random-character-string-with-no-duplicates/m-p/829261#M327616</link>
      <description>&lt;P&gt;I'm confused by the response that was accepted as the solution. When I submit that code, it produces a syntax error.&lt;/P&gt;</description>
      <pubDate>Thu, 18 Aug 2022 18:01:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Random-character-string-with-no-duplicates/m-p/829261#M327616</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2022-08-18T18:01:01Z</dc:date>
    </item>
    <item>
      <title>Re: Random character string with no duplicates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Random-character-string-with-no-duplicates/m-p/829265#M327618</link>
      <description>&lt;P&gt;Yes,&amp;nbsp; highlighted in red needs to be removed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;data a(keep=final);&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;length final $7;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;do i=1 to 500000;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;do j=1 to 7;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;bytep=&lt;SPAN&gt;byte(int(65+26*ranuni(0)));&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;if index(final,bytep) &amp;lt;=0 then do;&lt;BR /&gt;&lt;SPAN&gt;substr(final,j)=byte(int(65+26*ranuni(0)));&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;end;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;else;&lt;/P&gt;&lt;P&gt;i = i-1;&lt;/P&gt;&lt;P&gt;end;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;&lt;SPAN&gt;end;&amp;nbsp; This needs to be removed.&lt;/SPAN&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;SPAN&gt;output;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;end;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Aug 2022 18:14:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Random-character-string-with-no-duplicates/m-p/829265#M327618</guid>
      <dc:creator>KatLinden</dc:creator>
      <dc:date>2022-08-18T18:14:22Z</dc:date>
    </item>
    <item>
      <title>Re: Random character string with no duplicates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Random-character-string-with-no-duplicates/m-p/829269#M327621</link>
      <description>&lt;P&gt;It seems to me that the program results in an infinite loop because the looping variable i is decremented and never reaches 500000.&lt;/P&gt;</description>
      <pubDate>Thu, 18 Aug 2022 18:58:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Random-character-string-with-no-duplicates/m-p/829269#M327621</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2022-08-18T18:58:41Z</dc:date>
    </item>
    <item>
      <title>Re: Random character string with no duplicates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Random-character-string-with-no-duplicates/m-p/829282#M327632</link>
      <description>&lt;P&gt;I need unique values with no duplicates, not unique characters within a value.&lt;/P&gt;</description>
      <pubDate>Thu, 18 Aug 2022 19:52:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Random-character-string-with-no-duplicates/m-p/829282#M327632</guid>
      <dc:creator>KatLinden</dc:creator>
      <dc:date>2022-08-18T19:52:37Z</dc:date>
    </item>
    <item>
      <title>Re: Random character string with no duplicates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Random-character-string-with-no-duplicates/m-p/829283#M327633</link>
      <description>&lt;P&gt;I think you're right.&amp;nbsp; I cancelled the solution.&amp;nbsp; Thank you.&lt;/P&gt;</description>
      <pubDate>Thu, 18 Aug 2022 19:55:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Random-character-string-with-no-duplicates/m-p/829283#M327633</guid>
      <dc:creator>KatLinden</dc:creator>
      <dc:date>2022-08-18T19:55:32Z</dc:date>
    </item>
    <item>
      <title>Re: Random character string with no duplicates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Random-character-string-with-no-duplicates/m-p/829289#M327638</link>
      <description>&lt;P&gt;As you generate random strings, try to store them as the key in a hash object (the h.add() method below).&amp;nbsp; If the key is already in the hash object (left at default behavior of never accommodating duplicates), then h.add() will return a non-zero.&amp;nbsp; Skip those non-zero instances and generate another random string.&amp;nbsp; But when h.add() returns a zero, then output the string and increment variable i.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The variable i below in the sequence number of the series of &lt;U&gt;&lt;EM&gt;&lt;STRONG&gt;unique&lt;/STRONG&gt;&lt;/EM&gt;&lt;/U&gt; random strings generated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:);
  _characters ='ABCDEFGHIJKLMNOPQRSTUVWXYZ';

  length string $7;
  declare hash h ();
    h.definekey('string');
    h.definedone();

  call streaminit(105986);
  do i=1 by 0 until (i&amp;gt;500000);
    string=' ';
    do _c=1 to 7;
      string=cats(string,char(_characters,rand('integer',26)));
    end;
    if h.add()=0 then do; /*If h.add()=0 this is not a duplicate*/
      output;
      i=i+1;
    end;
  end;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note: there are 26**7 possible random strings.&amp;nbsp; This program would become an infinite loop if you tried to generate more than 26**7 unique strings (=8,031,810,176).&amp;nbsp; &amp;nbsp;And it would become might slow long before than sample size.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But you're only asking for 500,000.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BTW, the do loop construction&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  do i=1 by 0 until (i&amp;gt;500000);
    .... other code ...;
    if some condition then i=i+1;
  end;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;is equivalent to the more common:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  i=1;
  do until (i&amp;gt;500000);
    .... other code ...;
    if some condition then i=i+1;
  end;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 18 Aug 2022 20:40:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Random-character-string-with-no-duplicates/m-p/829289#M327638</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-08-18T20:40:48Z</dc:date>
    </item>
    <item>
      <title>Re: Random character string with no duplicates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Random-character-string-with-no-duplicates/m-p/829292#M327640</link>
      <description>&lt;P&gt;My question is: Do the strings really have to be unique and random? Often people want unique identifiers for items or transactions or whatever. If that is the case, can you use&lt;/P&gt;
&lt;P&gt;0000001,&amp;nbsp;0000002,&amp;nbsp;0000003, ..., 500000.&lt;/P&gt;
&lt;P&gt;These are obviously unique, and can be easily mapped to 7-character strings.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'd like to interject a little probability theory to explain why "random" and "unique" are at odds with each other.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The OP proposes a set of S = 26**7 = 8 billion possible 7-character strings and asks for&amp;nbsp;N=500k unique IDs chosen from within that set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Several solutions attempt to generate a random string, check to see if it was already generated, and then regenerate a new string if necessary. This algorithm becomes inefficient when N is greater than about 100k.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why? Because this formulation is equivalent to the famous Birthday Problem in probability theory. When you start drawing elements uniformly at random from the set S, it doesn't take long before you start drawing duplicates. In fact, after you have drawn 105,520 items, the probability that the next item has been seen before is greater than 50%. By the time you have drawn 200,000 items, the probability of drawing a duplicate value is about 97.5%. For more about the Birthday Problem, see&lt;/P&gt;
&lt;P&gt;&lt;A href="https://blogs.sas.com/content/iml/2012/04/09/vectorized-computations-and-the-birthday-matching-problem.html" target="_blank"&gt;https://blogs.sas.com/content/iml/2012/04/09/vectorized-computations-and-the-birthday-matching-problem.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;and&lt;/P&gt;
&lt;P&gt;&lt;A href="https://blogs.sas.com/content/iml/2013/07/03/duplicates-in-random-numbers.html" target="_blank"&gt;https://blogs.sas.com/content/iml/2013/07/03/duplicates-in-random-numbers.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Therefore, you reach a point at which most of your time is spent generating random numbers that have not been seen before. The "randomness" and "uniqueness" are at odds.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I suggest that the OP generate an arithmetic sequence of&amp;nbsp;integers less than 26**7, which is easy to do, and then map those integers to 7-character strings in the natural way. Because 26**7 is larger than constant('EXACTINT'), the OP might want to limit the integers to the interval [1, 2**31-1], which still is very large.&lt;/P&gt;</description>
      <pubDate>Thu, 18 Aug 2022 21:14:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Random-character-string-with-no-duplicates/m-p/829292#M327640</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2022-08-18T21:14:00Z</dc:date>
    </item>
    <item>
      <title>Re: Random character string with no duplicates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Random-character-string-with-no-duplicates/m-p/829295#M327643</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Therefore, you reach a point at which most of your time is spent generating random numbers that have not been seen before.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Thanks for elaborating on the mathematical background. Luckily, if the sample size is as small as 500,000 out of 26**7, only a few (~10 - 20) hits of already selected items occur, so that performance is hardly impacted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;The variable i below in the sequence number of the series of &lt;U&gt;&lt;EM&gt;&lt;STRONG&gt;unique&lt;/STRONG&gt;&lt;/EM&gt;&lt;/U&gt; random strings generated.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If variable &lt;FONT face="courier new,courier"&gt;i&lt;/FONT&gt; is not needed, one could use&lt;/P&gt;
&lt;PRE&gt;do until(h.num_items=500000);
  ...
end;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Aug 2022 21:59:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Random-character-string-with-no-duplicates/m-p/829295#M327643</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-08-18T21:59:37Z</dc:date>
    </item>
    <item>
      <title>Re: Random character string with no duplicates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Random-character-string-with-no-duplicates/m-p/829296#M327644</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Therefore, you reach a point at which most of your time is spent generating random numbers that have not been seen before.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Thanks for elaborating on the mathematical background. Luckily, if the sample size is as small as 500,000 out of 26**7, only a few (~10 - 20) hits of already selected items occur, so that performance is hardly impacted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;The variable i below in the sequence number of the series of &lt;U&gt;&lt;EM&gt;&lt;STRONG&gt;unique&lt;/STRONG&gt;&lt;/EM&gt;&lt;/U&gt; random strings generated.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If variable &lt;FONT face="courier new,courier"&gt;i&lt;/FONT&gt; is not needed, one could use&lt;/P&gt;
&lt;PRE&gt;do until(h.num_items=500000);
  ...
end;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Agreed: the variable i in my response is not necessary, but I wanted a means to restore original order, should any sorting occur.&amp;nbsp; BTW, with the streaminit value I used, there were only 11 instances of duplicates to be skipped.&lt;/P&gt;</description>
      <pubDate>Thu, 18 Aug 2022 22:11:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Random-character-string-with-no-duplicates/m-p/829296#M327644</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-08-18T22:11:59Z</dc:date>
    </item>
    <item>
      <title>Re: Random character string with no duplicates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Random-character-string-with-no-duplicates/m-p/829298#M327646</link>
      <description>&lt;P&gt;Ugly brute force: Create all 7 character strings allowed in a data set. (18 lines of code should do it using nested DO loops)&lt;/P&gt;
&lt;P&gt;Proc Survey select to select a desired number of them.&lt;/P&gt;
&lt;P&gt;Merge with the original data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Result is random values.&lt;/P&gt;
&lt;P&gt;Small example with 3 letter combos:&lt;/P&gt;
&lt;PRE&gt;data junk;
  length x $ 7;
  do i=65 to 90;
  do j=65 to 90;
  do k=65 to 90;
    x=cats(byte(i),byte(j),byte(k));
    output;
  end;
  end;
  end;
  keep x;
run; 

proc surveyselect data=junk out=rand noprint
     sampsize=25;
run;

/* if rand has the same number of records as 
   have a 1-1 match
*/
data want;
   merge have
         rand
   ;
run;&lt;/PRE&gt;
&lt;P&gt;Note that you can query the metadata about your data set to get a macro variable to hold the number of records to request for a 1-1 sampsize of an existing data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Aug 2022 22:47:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Random-character-string-with-no-duplicates/m-p/829298#M327646</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-08-18T22:47:07Z</dc:date>
    </item>
    <item>
      <title>Re: Random character string with no duplicates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Random-character-string-with-no-duplicates/m-p/829328#M327661</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;BTW, with the streaminit value I used, there were only 11 instances of duplicates to be skipped.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This is very plausible. I had observed between 8 and 23 in a few trials. According to formulas I've just found in&amp;nbsp;Volume I of&amp;nbsp;&lt;A href="https://documentation.sas.com/doc/en/statug/15.2/statug_introbayes_sect050.htm#statug_introbayesfell_w68" target="_blank" rel="noopener"&gt;Feller (1968)&lt;/A&gt;, p. 225, the expected value is 15.56 (see code below). So, in this case the cost of avoiding duplicates is rather the maintenance of the lookup table than the 0.003% additional samples needed on average.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let n=%sysevalf(26**7);
%let r=500000;

data _null_;
do k=0 to &amp;amp;r-1;
  s+1/(&amp;amp;n-k);
end;
E_exact_=&amp;amp;n*s-&amp;amp;r;
E_approx=&amp;amp;n*log((&amp;amp;n+0.5)/(&amp;amp;n-&amp;amp;r+0.5))-&amp;amp;r;
put (E:)(=best16./);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 Aug 2022 09:30:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Random-character-string-with-no-duplicates/m-p/829328#M327661</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-08-19T09:30:09Z</dc:date>
    </item>
    <item>
      <title>Re: Random character string with no duplicates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Random-character-string-with-no-duplicates/m-p/829347#M327672</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*Try UUIDGEN() function.
if you only want alpha, 
you could get rid of thoese digits*/
data a;
do i=1 to 50000;
want=uuidgen(123);
output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 19 Aug 2022 12:43:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Random-character-string-with-no-duplicates/m-p/829347#M327672</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-08-19T12:43:51Z</dc:date>
    </item>
    <item>
      <title>Re: Random character string with no duplicates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Random-character-string-with-no-duplicates/m-p/829360#M327675</link>
      <description>&lt;P&gt;The OP has selected an answer, but I am still curious about the reason for this question.&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/211617"&gt;@KatLinden&lt;/a&gt;&amp;nbsp;Why do you want the character strings to be random? How will these strings be used?&lt;/P&gt;</description>
      <pubDate>Fri, 19 Aug 2022 13:48:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Random-character-string-with-no-duplicates/m-p/829360#M327675</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2022-08-19T13:48:54Z</dc:date>
    </item>
    <item>
      <title>Re: Random character string with no duplicates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Random-character-string-with-no-duplicates/m-p/834104#M329780</link>
      <description>&lt;P&gt;The discussion on this thread inspired me to think about this problem and write up a solution. My approach: Use base 26 to convert a set of unique integers into a set of unique strings. If you expect to assign IDs to N subjects, you can use strings that have k characters, where N &amp;lt; 26^k.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;How to generate the ID values (strings) from integers: &lt;A href="https://blogs.sas.com/content/iml/2022/09/14/base-26-integer-string.html" target="_self"&gt;"Base 26: A mapping from integers to strings"&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;How to randomly select N unique IDs: &lt;A href="https://blogs.sas.com/content/iml/2022/09/19/random-id-values-sas.html" target="_self"&gt;"Generate random ID values for subjects in SAS"&lt;/A&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;The primary advantage of this technique over some of the other proposals is that it ensures uniqueness of the ID values. You don't have to check whether a random string has already been assigned.&lt;/P&gt;</description>
      <pubDate>Mon, 19 Sep 2022 13:54:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Random-character-string-with-no-duplicates/m-p/834104#M329780</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2022-09-19T13:54:51Z</dc:date>
    </item>
  </channel>
</rss>

