<?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: Swapping the WORD in String in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Swapping-the-WORD-in-String/m-p/592693#M169961</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16518"&gt;@keen_sas&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;do you mean text cuting&amp;amp;pasting?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards missover dlm = "|";
input Original : $ 200. 	Required : $ 200.	Comment : $ 20.;
cards;
CUSTOMER DOB TERM descending|CUSTOMER DOB descending TERM|	 
CUSTOMER DOB descending TERM|CUSTOMER descending DOB TERM|	 
CUSTOMER DOB TERM|CUSTOMER DOB TERM|Nochange
CUSTOMER descending|descending CUSTOMER|
;
run;

data want;
  word = "descending";
  set have;

  pos = FINDW(Original, word); 
  if pos = 0 then Result = Original; 
  else
    do;
    length start s1 end $ 200;
      L = lengthn(word);
      end = substr(Original, pos + L);
      start = substr(Original, 1, pos-1);
      s1 =  strip(scan(start, -1));

      x = pos-1-lengthn(s1)-1;
      if x &amp;gt; 0 then start = substr(Original, 1, x);
               else start = "";

      Result = catx(" ",start,word,s1,end);
    end;

  equal = (Result = Required);
  output;
  drop start word s1 end pos L x;
run;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;All the best&lt;/P&gt;&lt;P&gt;Bart&lt;/P&gt;</description>
    <pubDate>Mon, 30 Sep 2019 14:27:49 GMT</pubDate>
    <dc:creator>yabwon</dc:creator>
    <dc:date>2019-09-30T14:27:49Z</dc:date>
    <item>
      <title>Swapping the WORD in String</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Swapping-the-WORD-in-String/m-p/592669#M169955</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to swap the word in a string based on the keyword. As per the below table i am trying to swap the word descending prior to its word as below. Original is the variable provided and i am trying to convert into variable REQUIRED only by swapping the word descending just before it. Reason for doing this is Original is the column used in SQL syntax for sorting and Required is the column used in Proc sort Sorting . In SQL Descending option is used after the variable and in proc sort before the variables.Any possibilities to convert as shown below&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="634"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="262"&gt;Original&amp;nbsp;&lt;/TD&gt;
&lt;TD width="221"&gt;Required&lt;/TD&gt;
&lt;TD width="151"&gt;Comment&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;CUSTOMER DOB TERM descending&lt;/TD&gt;
&lt;TD&gt;CUSTOMER DOB descending TERM&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;CUSTOMER DOB descending TERM&lt;/TD&gt;
&lt;TD&gt;CUSTOMER descending DOB&amp;nbsp; TERM&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;CUSTOMER DOB TERM&lt;/TD&gt;
&lt;TD&gt;CUSTOMER DOB TERM&lt;/TD&gt;
&lt;TD&gt;Nochange&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;CUSTOMER descending&lt;/TD&gt;
&lt;TD&gt;descending CUSTOMER&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Mon, 30 Sep 2019 13:40:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Swapping-the-WORD-in-String/m-p/592669#M169955</guid>
      <dc:creator>keen_sas</dc:creator>
      <dc:date>2019-09-30T13:40:53Z</dc:date>
    </item>
    <item>
      <title>Re: Swapping the WORD in String</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Swapping-the-WORD-in-String/m-p/592684#M169960</link>
      <description>&lt;P&gt;See this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input string $60.;
datalines;
CUSTOMER DOB TERM descending
CUSTOMER DOB descending TERM
CUSTOMER DOB TERM
CUSTOMER descending
;

%let refstring = descending;

data want;
set have;
length newstring $60;
index = findw(string,"&amp;amp;refstring.",' ','e');
do i = 1 to index - 2;
  newstring = catx(' ',newstring,scan(string,i));
end;
if index then newstring = catx(' ',newstring,"&amp;amp;refstring.",scan(string,index-1));
do i = index + 1 to countw(string);
  newstring = catx(' ',newstring,scan(string,i));
end;
drop i index string;
rename newstring=string;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 30 Sep 2019 14:16:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Swapping-the-WORD-in-String/m-p/592684#M169960</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-09-30T14:16:50Z</dc:date>
    </item>
    <item>
      <title>Re: Swapping the WORD in String</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Swapping-the-WORD-in-String/m-p/592693#M169961</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16518"&gt;@keen_sas&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;do you mean text cuting&amp;amp;pasting?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards missover dlm = "|";
input Original : $ 200. 	Required : $ 200.	Comment : $ 20.;
cards;
CUSTOMER DOB TERM descending|CUSTOMER DOB descending TERM|	 
CUSTOMER DOB descending TERM|CUSTOMER descending DOB TERM|	 
CUSTOMER DOB TERM|CUSTOMER DOB TERM|Nochange
CUSTOMER descending|descending CUSTOMER|
;
run;

data want;
  word = "descending";
  set have;

  pos = FINDW(Original, word); 
  if pos = 0 then Result = Original; 
  else
    do;
    length start s1 end $ 200;
      L = lengthn(word);
      end = substr(Original, pos + L);
      start = substr(Original, 1, pos-1);
      s1 =  strip(scan(start, -1));

      x = pos-1-lengthn(s1)-1;
      if x &amp;gt; 0 then start = substr(Original, 1, x);
               else start = "";

      Result = catx(" ",start,word,s1,end);
    end;

  equal = (Result = Required);
  output;
  drop start word s1 end pos L x;
run;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;All the best&lt;/P&gt;&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Mon, 30 Sep 2019 14:27:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Swapping-the-WORD-in-String/m-p/592693#M169961</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2019-09-30T14:27:49Z</dc:date>
    </item>
    <item>
      <title>Re: Swapping the WORD in String</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Swapping-the-WORD-in-String/m-p/592836#M170030</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16518"&gt;@keen_sas&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If there are no name literals containing blanks (such as &lt;FONT face="courier new,courier"&gt;'abc def'n&lt;/FONT&gt;) among the variable names, the below regular expression could be used:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
required=original; /* set length */
required=prxchange('s/(\S+)\s+(descending)/\2 \1/i', -1, original);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The "&lt;FONT face="courier new,courier"&gt;i&lt;/FONT&gt;" modifier makes the search case insensitive so that &lt;FONT face="courier new,courier"&gt;DESCENDING&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;Descending&lt;/FONT&gt; etc. would be found as well. With a slight modification the abbreviated keyword &lt;FONT face="courier new,courier"&gt;desc&lt;/FONT&gt;, which is valid in PROC SQL, but not in PROC SORT, could be included:&lt;/P&gt;
&lt;PRE&gt;'s/(\S+)\s+(descending|desc)/descending \1/i'&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit: Improved the regex in order to allow multiple and different whitespace characters between variable name and "descending" (or "desc", resp.) and name literals not containing a whitespace character.&lt;/P&gt;</description>
      <pubDate>Tue, 01 Oct 2019 06:27:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Swapping-the-WORD-in-String/m-p/592836#M170030</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-10-01T06:27:58Z</dc:date>
    </item>
  </channel>
</rss>

