<?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: Rearranging a Character String into specific, non-alphabetical order in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Rearranging-a-Character-String-into-specific-non-alphabetical/m-p/752426#M237017</link>
    <description>&lt;P&gt;If you always want the first character of FLAG, you could use:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;length hierarchy $ 1;&lt;/P&gt;
&lt;P&gt;hierarchy = flag;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There's only room to store one character, so the first character gets stored.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another possibility:&amp;nbsp; don't bother to create HIERARCHY.&amp;nbsp; Just use FLAG as is.&amp;nbsp; For example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc freq data=have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;tables flag;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;format flag $1.;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
    <pubDate>Tue, 06 Jul 2021 21:11:36 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2021-07-06T21:11:36Z</dc:date>
    <item>
      <title>Rearranging a Character String into specific, non-alphabetical order</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rearranging-a-Character-String-into-specific-non-alphabetical/m-p/752423#M237015</link>
      <description>&lt;P&gt;All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm currently working with some data that runs through various different codes, each of which may append a new value to some flag field. At the end of it all, we want to set a hierarchy of the flags already applied. At the moment I'm doing so by using a bunch of &lt;EM&gt;if find(flag, '*') gt 0 then ...&amp;nbsp;&lt;/EM&gt;statements, but I'm wondering if there's an easier way.&amp;nbsp;My thought is if I can sort&amp;nbsp;&lt;EM&gt;&lt;STRONG&gt;Flag&lt;/STRONG&gt;&lt;/EM&gt;&amp;nbsp;in order of my hierarchy then I can basically just grab the left-most character and use that. I found &lt;A href="https://communities.sas.com/t5/General-SAS-Programming/Rearranging-a-Character-Strings-Into-alphabetical-order/td-p/394722" target="_self"&gt;this post&lt;/A&gt; to get me started on how I could sort the&amp;nbsp;&lt;STRONG&gt;&lt;EM&gt;Flag&lt;/EM&gt;&lt;/STRONG&gt; field alphabetically, but I'm wondering if there's some way to expand that beyond just alphabetical?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data and hierarchy logic I have&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;Flag&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;Hierarchy&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;QVRS&lt;/TD&gt;&lt;TD&gt;R&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;BEWC&lt;/TD&gt;&lt;TD&gt;C&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;RT&lt;/TD&gt;&lt;TD&gt;R&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;QTS&lt;/TD&gt;&lt;TD&gt;T&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;if find(flag, 'R') gt 0 then Hierarchy = 'R';
else if find(flag, 'C') gt 0 then Hierarchy = 'C';
else if find(flag, 'W') gt 0 then Hierarchy = 'W';
else if find(flag, 'T') gt 0 then Hierarchy = 'T';
else ... &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data and hierarchy logic I want&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;Flag&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;Hierarchy&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;RQVS&lt;/TD&gt;&lt;TD&gt;R&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;CEBW&lt;/TD&gt;&lt;TD&gt;C&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;RT&lt;/TD&gt;&lt;TD&gt;R&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;TQS&lt;/TD&gt;&lt;TD&gt;T&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;Hierarchy = substr(flag, 1, 1);&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, in case it matters, I'm using SAS 9.4.&lt;/P&gt;</description>
      <pubDate>Tue, 06 Jul 2021 20:58:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rearranging-a-Character-String-into-specific-non-alphabetical/m-p/752423#M237015</guid>
      <dc:creator>Burton_Gustice</dc:creator>
      <dc:date>2021-07-06T20:58:05Z</dc:date>
    </item>
    <item>
      <title>Re: Rearranging a Character String into specific, non-alphabetical order</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rearranging-a-Character-String-into-specific-non-alphabetical/m-p/752426#M237017</link>
      <description>&lt;P&gt;If you always want the first character of FLAG, you could use:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;length hierarchy $ 1;&lt;/P&gt;
&lt;P&gt;hierarchy = flag;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There's only room to store one character, so the first character gets stored.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another possibility:&amp;nbsp; don't bother to create HIERARCHY.&amp;nbsp; Just use FLAG as is.&amp;nbsp; For example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc freq data=have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;tables flag;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;format flag $1.;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Jul 2021 21:11:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rearranging-a-Character-String-into-specific-non-alphabetical/m-p/752426#M237017</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2021-07-06T21:11:36Z</dc:date>
    </item>
    <item>
      <title>Re: Rearranging a Character String into specific, non-alphabetical order</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rearranging-a-Character-String-into-specific-non-alphabetical/m-p/752447#M237030</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/388544"&gt;@Burton_Gustice&lt;/a&gt;&amp;nbsp;and welcome to the SAS Support Communities!&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/388544"&gt;@Burton_Gustice&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I'm currently working with some data that runs through various different codes, each of which may &lt;STRONG&gt;append a new value to some flag field.&lt;/STRONG&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I think &lt;STRONG&gt;this&lt;/STRONG&gt; is where a change would make your task easier: Better append a new &lt;EM&gt;observation&lt;/EM&gt; to some &lt;EM&gt;dataset&lt;/EM&gt;, i.e., work with a "long" ("vertical") data structure, with observations containing &lt;EM&gt;one&lt;/EM&gt; value (see dataset HAVE below).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then it's much easier to create any desired sort order (using PROC SQL's powerful ORDER BY clause). It's also easy to create the string of concatenated values from there, if needed (see the last DATA step below).&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;data have;
input id val $1.;
cards;
1 Q
1 V
1 R
1 S
2 B
2 E
2 W
2 C
3 R
3 T
4 Q
4 T
4 S
;

proc sql;
create table sorted as
select * from have
order by id, find('RTQCEBWVS',val);
quit;

data want(drop=val);
length flag $8 hierarchy $1;
do until(last.id);
  set sorted;
  by id;
  flag=cats(flag,val);
  if first.id then hierarchy=val;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The string in the first argument of the FIND function should reflect the hierarchy of flags (i.e., 'R' has the highest priority, etc.). It's important that &lt;EM&gt;all&lt;/EM&gt; possible "values" are contained in that string. Otherwise, additional code would be needed to deal with "unexpected" values. (It's probably a good idea to add such code anyway to make the program more robust.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Of course, you can also create the "long" dataset from your existing data if you don't want to change the process upstream.&lt;/P&gt;</description>
      <pubDate>Tue, 06 Jul 2021 21:58:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rearranging-a-Character-String-into-specific-non-alphabetical/m-p/752447#M237030</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-07-06T21:58:32Z</dc:date>
    </item>
    <item>
      <title>Re: Rearranging a Character String into specific, non-alphabetical order</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rearranging-a-Character-String-into-specific-non-alphabetical/m-p/752482#M237043</link>
      <description>&lt;P&gt;Store the ordered list of all possible codes in a retained variable _ORDERED_FLAGS.&amp;nbsp; &amp;nbsp;Then rebuild each flag string by looping through _ORDERED_FLAGS looking for matches in the initial, unordered, value of FLAG:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input flag $4. ;
datalines;
QVRS
BEWC
RT
QTS
run;

data want (drop=_: i);
  set have;

  retain _ordered_flags 'RTQVSCEBW';

  _tmp_flag=flag;
  call missing(flag);

  do i=1 by 1 until(length(flag)=length(_tmp_flag));
    if findc(_tmp_flag,char(_ordered_flags,i)) then flag=cats(flag,char(_ordered_flags,i));
  end;
  length hierarchy $1;
  hierarchy=flag;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Make sure all possible flag values are in the retained value for _ORDERED_FLAGS.&amp;nbsp; &amp;nbsp;I had to guess based on your example.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, this code assumes that no individual flag code appears more than once.in each FLAG string.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, because HIERARCHY is given a length of 1, the simple assignment&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  hierarchy=flag;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;will copy only the first letter.&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jul 2021 02:52:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rearranging-a-Character-String-into-specific-non-alphabetical/m-p/752482#M237043</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-07-07T02:52:46Z</dc:date>
    </item>
    <item>
      <title>Re: Rearranging a Character String into specific, non-alphabetical order</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rearranging-a-Character-String-into-specific-non-alphabetical/m-p/752485#M237045</link>
      <description>&lt;P&gt;I think this one makes the most sense to me and works the best/is the easiest to integrate with the way my process is currently set up. Thank you!!&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jul 2021 03:34:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rearranging-a-Character-String-into-specific-non-alphabetical/m-p/752485#M237045</guid>
      <dc:creator>Burton_Gustice</dc:creator>
      <dc:date>2021-07-07T03:34:52Z</dc:date>
    </item>
  </channel>
</rss>

