<?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 assign unique character variable to each observation in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-assign-unique-character-variable-to-each-observation/m-p/748678#M235148</link>
    <description>&lt;P&gt;Just for fun.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%let dsn=sashelp.heart;
%let string= ADBZYZ ;







%let dsid=%sysfunc(open(&amp;amp;dsn));
%let nobs=%sysfunc(attrn(&amp;amp;dsid,nlobs));
%let dsid=%sysfunc(close(&amp;amp;dsid));
%let n1=%sysfunc(rank(%substr(&amp;amp;string,1,1)));
%let n2=%sysfunc(rank(%substr(&amp;amp;string,2,1)));
%let n3=%sysfunc(rank(%substr(&amp;amp;string,3,1)));
%let n4=%sysfunc(rank(%substr(&amp;amp;string,4,1)));
%let n5=%sysfunc(rank(%substr(&amp;amp;string,5,1)));
%let n6=%sysfunc(rank(%substr(&amp;amp;string,6,1)));


%put _user_;

data key;
 do n1=&amp;amp;n1 to rank('Z');

  loop2+1;
   _n2=ifn(loop2=1,&amp;amp;n2,rank('A'));
 do n2=_n2 to rank('Z');

   loop3+1;
   _n3=ifn(loop3=1,&amp;amp;n3,rank('A'));
 do n3=_n3 to rank('Z');

   loop4+1;
   _n4=ifn(loop4=1,&amp;amp;n4,rank('A'));
 do n4=_n4 to rank('Z');

   loop5+1;
   _n5=ifn(loop5=1,&amp;amp;n5,rank('A'));
 do n5=_n5 to rank('Z');

   loop6+1;
   _n6=ifn(loop6=1,&amp;amp;n6,rank('A'));
 do n6=_n6 to rank('Z');
   n+1;string=cats(byte(n1),byte(n2),byte(n3),byte(n4),byte(n5),byte(n6));output;
   if n = &amp;amp;nobs then stop;
 end;end;end;end;end;end;
 keep string;
run;


data want;
 merge &amp;amp;dsn key;
run;&lt;/PRE&gt;</description>
    <pubDate>Thu, 17 Jun 2021 13:39:56 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2021-06-17T13:39:56Z</dc:date>
    <item>
      <title>How to assign unique character variable to each observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-assign-unique-character-variable-to-each-observation/m-p/748367#M235052</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to assign a unique 6 character variable to each observation in a dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;eg.&lt;/P&gt;&lt;P&gt;1="AAAAAA"&lt;/P&gt;&lt;P&gt;2="AAAAAB"&lt;/P&gt;&lt;P&gt;3="AAAAAC"&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;26="AAAAAZ"&lt;/P&gt;&lt;P&gt;27="AAAABA"&lt;/P&gt;&lt;P&gt;etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It needs to be dynamic based on the first observation which may not always be "AAAAAA".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What would be the best way of doing this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Wed, 16 Jun 2021 15:13:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-assign-unique-character-variable-to-each-observation/m-p/748367#M235052</guid>
      <dc:creator>Luke_D</dc:creator>
      <dc:date>2021-06-16T15:13:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to assign unique character variable to each observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-assign-unique-character-variable-to-each-observation/m-p/748384#M235055</link>
      <description>Please explain further what you mean by dynamic? Feel free to add more examples to illustrate the issue.&lt;BR /&gt;&lt;BR /&gt;Here's a method to create an anonymized key data set if that's what you're looking for. &lt;BR /&gt;&lt;A href="https://gist.github.com/statgeek/fd94b0b6e78815430c1340e8c19f8644" target="_blank"&gt;https://gist.github.com/statgeek/fd94b0b6e78815430c1340e8c19f8644&lt;/A&gt;</description>
      <pubDate>Wed, 16 Jun 2021 16:11:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-assign-unique-character-variable-to-each-observation/m-p/748384#M235055</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-06-16T16:11:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to assign unique character variable to each observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-assign-unique-character-variable-to-each-observation/m-p/748405#M235065</link>
      <description>I mean that obs 1 won't always start "AAAAAA" so the next observation would need to add the next letter in the sequence based on the values of the previous observation.&lt;BR /&gt;&lt;BR /&gt;eg.&lt;BR /&gt;1=""ADBBAB"&lt;BR /&gt;2="ADBBAC"&lt;BR /&gt;3="ADBBAD"&lt;BR /&gt;&lt;BR /&gt;Thanks for the link but I'm trying to get each observation to follow the sequence rather than just an anonymized key.</description>
      <pubDate>Wed, 16 Jun 2021 17:44:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-assign-unique-character-variable-to-each-observation/m-p/748405#M235065</guid>
      <dc:creator>Luke_D</dc:creator>
      <dc:date>2021-06-16T17:44:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to assign unique character variable to each observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-assign-unique-character-variable-to-each-observation/m-p/748417#M235070</link>
      <description>So you want each letter to be an alphabetic sequence?&lt;BR /&gt;Separate each character into an array. Then increment each level as needed. &lt;BR /&gt;The BYTE() function will convert a number to a character, 65 to 90 are the capitalized values, but you'll have to manually do the increment for Z to A to get the value. &lt;BR /&gt;&lt;BR /&gt;Essentially you're using a numbering system with base 26 instead of 10 so you have to do all the math calculations yourself. It would be much easier to use numbers......</description>
      <pubDate>Wed, 16 Jun 2021 17:58:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-assign-unique-character-variable-to-each-observation/m-p/748417#M235070</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-06-16T17:58:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to assign unique character variable to each observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-assign-unique-character-variable-to-each-observation/m-p/748421#M235072</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/386105"&gt;@Luke_D&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;Try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Create sample data for demonstration */

data have;
set sashelp.class;
if _n_=1 then string='ADBBAB';
run;

/* Continue the alphabetic series of strings */

data want(drop=_:);
set have;
if _n_=1 then _s=string;
else do;
  _i=6-findc(_s,'Z',-6,'k');
  if _i&amp;gt;0 then substr(_s,7-_i)=repeat('A',6);
  if _i&amp;lt;6 then substr(_s,6-_i,1)=byte(rank(char(_s,6-_i))+1);
  else put 'CAUTION: Overflow "ZZZZZZ" --&amp;gt; "AAAAAA" occurred in obs. ' _n_;
  string=_s;
end;
retain _s;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 16 Jun 2021 18:04:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-assign-unique-character-variable-to-each-observation/m-p/748421#M235072</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-06-16T18:04:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to assign unique character variable to each observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-assign-unique-character-variable-to-each-observation/m-p/748546#M235082</link>
      <description>&lt;P&gt;Sir&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp; Brilliant!!! I had to read your idea twice to get the hang of it.&amp;nbsp; Yes, got it. Very neat indeed. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Jun 2021 19:52:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-assign-unique-character-variable-to-each-observation/m-p/748546#M235082</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2021-06-16T19:52:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to assign unique character variable to each observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-assign-unique-character-variable-to-each-observation/m-p/748622#M235115</link>
      <description>Thanks for the welcome and the fantastic solution!</description>
      <pubDate>Thu, 17 Jun 2021 07:13:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-assign-unique-character-variable-to-each-observation/m-p/748622#M235115</guid>
      <dc:creator>Luke_D</dc:creator>
      <dc:date>2021-06-17T07:13:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to assign unique character variable to each observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-assign-unique-character-variable-to-each-observation/m-p/748623#M235116</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Essentially you're using a numbering system with base 26 instead of 10 so you have to do all the math calculations yourself. It would be much easier to use numbers......&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;I agree, but the variable is limited to 6 bytes and using letters gives us 300m+ more combinations than base 10.&lt;/P&gt;</description>
      <pubDate>Thu, 17 Jun 2021 07:18:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-assign-unique-character-variable-to-each-observation/m-p/748623#M235116</guid>
      <dc:creator>Luke_D</dc:creator>
      <dc:date>2021-06-17T07:18:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to assign unique character variable to each observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-assign-unique-character-variable-to-each-observation/m-p/748678#M235148</link>
      <description>&lt;P&gt;Just for fun.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%let dsn=sashelp.heart;
%let string= ADBZYZ ;







%let dsid=%sysfunc(open(&amp;amp;dsn));
%let nobs=%sysfunc(attrn(&amp;amp;dsid,nlobs));
%let dsid=%sysfunc(close(&amp;amp;dsid));
%let n1=%sysfunc(rank(%substr(&amp;amp;string,1,1)));
%let n2=%sysfunc(rank(%substr(&amp;amp;string,2,1)));
%let n3=%sysfunc(rank(%substr(&amp;amp;string,3,1)));
%let n4=%sysfunc(rank(%substr(&amp;amp;string,4,1)));
%let n5=%sysfunc(rank(%substr(&amp;amp;string,5,1)));
%let n6=%sysfunc(rank(%substr(&amp;amp;string,6,1)));


%put _user_;

data key;
 do n1=&amp;amp;n1 to rank('Z');

  loop2+1;
   _n2=ifn(loop2=1,&amp;amp;n2,rank('A'));
 do n2=_n2 to rank('Z');

   loop3+1;
   _n3=ifn(loop3=1,&amp;amp;n3,rank('A'));
 do n3=_n3 to rank('Z');

   loop4+1;
   _n4=ifn(loop4=1,&amp;amp;n4,rank('A'));
 do n4=_n4 to rank('Z');

   loop5+1;
   _n5=ifn(loop5=1,&amp;amp;n5,rank('A'));
 do n5=_n5 to rank('Z');

   loop6+1;
   _n6=ifn(loop6=1,&amp;amp;n6,rank('A'));
 do n6=_n6 to rank('Z');
   n+1;string=cats(byte(n1),byte(n2),byte(n3),byte(n4),byte(n5),byte(n6));output;
   if n = &amp;amp;nobs then stop;
 end;end;end;end;end;end;
 keep string;
run;


data want;
 merge &amp;amp;dsn key;
run;&lt;/PRE&gt;</description>
      <pubDate>Thu, 17 Jun 2021 13:39:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-assign-unique-character-variable-to-each-observation/m-p/748678#M235148</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-06-17T13:39:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to assign unique character variable to each observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-assign-unique-character-variable-to-each-observation/m-p/748719#M235166</link>
      <description>&lt;P&gt;Combining&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;'s idea&lt;/P&gt;
&lt;BLOCKQUOTE&gt;Essentially you're using a numbering system with base 26 instead of 10 so you have to do all the math calculations yourself.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;'s magic &lt;A href="https://communities.sas.com/t5/SAS-Programming/Converting-a-19-length-number-to-hex-and-then-to-a-string/m-p/653973/highlight/true#M196445" target="_blank" rel="noopener"&gt;%ConvertBase macro&lt;/A&gt; one can calculate the &lt;EM&gt;n&lt;/EM&gt;-th value of the sequence (for an almost arbitrary positive integer&amp;nbsp;&lt;EM&gt;n&lt;/EM&gt;) directly from the first without looping through the intermediate values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let value1=ADBZZU;
%let n=19;
%let c=%sysfunc(collate(65,90)); /* assuming the ASCII collating sequence */
%let d=0123456789%sysfunc(collate(65,80));
%let value&amp;amp;n=%sysfunc(translate(%substr(%ConvertBase(10,26,%eval(%ConvertBase(26,10,%sysfunc(translate(&amp;amp;value1,&amp;amp;d,&amp;amp;c)))+&amp;amp;n-1+26**6)),2),&amp;amp;c,&amp;amp;d));
%put &amp;amp;&amp;amp;value&amp;amp;n;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The code looks a bit cumbersome because the mathematical base-26 system uses digits 0, ..., 9, A, ..., P rather than A, ..., Z. That's where the TRANSLATE and COLLATE functions come into play.&lt;/P&gt;</description>
      <pubDate>Thu, 17 Jun 2021 15:30:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-assign-unique-character-variable-to-each-observation/m-p/748719#M235166</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-06-17T15:30:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to assign unique character variable to each observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-assign-unique-character-variable-to-each-observation/m-p/748722#M235168</link>
      <description>6 bytes for a numeric variable supports values up to 137,438,953,472 which is 444 times more values than using 6 characters. &lt;BR /&gt;1 byte is not a character when it comes to numeric values.</description>
      <pubDate>Thu, 17 Jun 2021 15:42:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-assign-unique-character-variable-to-each-observation/m-p/748722#M235168</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-06-17T15:42:16Z</dc:date>
    </item>
  </channel>
</rss>

