<?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 insert character in between a string without deleting other characters in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/How-to-insert-character-in-between-a-string-without-deleting/m-p/545516#M8041</link>
    <description>&lt;P&gt;If you want to avoid problems with short Item strings, tweak &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt; 's idea a bit:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test ;
  length Customer $18 Item $11 Want $14 ;
  input Customer &amp;amp; Item; 
  want = cats('(',catx("-", substrn(item,1,length(item)-1),char(item,length(item))),')');
datalines;
Bartco Corporation  143L
Cost Cutter's  142
Minimart Inc.  188SW
Bosco, LLC  908X321
K-MART.  A122L
Food Unlimited  1898XVWX
Shop and Drop  24L
Shop Marty's  222X
Pet's are Us  W100
Roger's Spirits  1522L
Artful Spirits  407XxxXxx
Up/Down mart Inc.  ABCdefghxxx
Tiny test  a
Nasty test  .
;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;             Obs    Customer              Item           Want

               1    Bartco Corporation    143L           (143-L)
               2    Cost Cutter's         142            (14-2)
               3    Minimart Inc.         188SW          (188S-W)
               4    Bosco, LLC            908X321        (908X32-1)
               5    K-MART.               A122L          (A122-L)
               6    Food Unlimited        1898XVWX       (1898XVW-X)
               7    Shop and Drop         24L            (24-L)
               8    Shop Marty's          222X           (222-X)
               9    Pet's are Us          W100           (W10-0)
              10    Roger's Spirits       1522L          (1522-L)
              11    Artful Spirits        407XxxXxx      (407XxxXx-x)
              12    Up/Down mart Inc.     ABCdefghxxx    (ABCdefghxx-x)
              13    Tiny test             a              (a)
              14    Nasty test                           ()
&lt;/PRE&gt;</description>
    <pubDate>Sat, 23 Mar 2019 19:07:24 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2019-03-23T19:07:24Z</dc:date>
    <item>
      <title>How to insert character in between a string without deleting other characters</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-insert-character-in-between-a-string-without-deleting/m-p/545437#M8024</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am quite confused about how to use the lengthn, cat, catx&amp;nbsp;and substr&amp;nbsp;functions all together&amp;nbsp;for string manipulation. I am trying to add a variable that inserts a dash between the 2nd to last character and the last character of Item and wraps the value in parentheses. For example, Item 144L becomes (144-L). Use the LENGTHN, SUBSTR, CAT and CATX functions. The&amp;nbsp;solution should be generalizable to any value of Item containing 2 or more characters. So far, I have been building the program using examples I have seen but I keep getting confused. Please can you help me fix my program and include explanations if u can. I just need to understand how all the functions work together to solve this problem. Thanks.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data CustomerDB ;
  input Customer  &amp;amp;      $18.
        Item      :      $11. ; 
datalines;
Bartco Corporation  143L
Cost Cutter's  142
Minimart Inc.  188SW
Bosco, LLC  908X321
K-MART.  A122L
Food Unlimited  1898XVWX
Shop and Drop  24L
Shop Marty's  222X
Pet's are Us  W100
Roger's Spirits  1522L
Artful Spirits  407XxxXxx
Up/Down mart Inc.  ABCdefghxxx
;
data newTemp;
	set CustomerDB;
	if lengthn(Item) ge 3 then
		secondtoLastRemoved = catx("-",(substr(Item,1,(lengthn(Item)-2))),(substr(Item,lengthn(Item), 1))
									); /*Dropping Second to last characters from string?*/
	else
		secondtoLastRemoved = Item;
	if findc(Item,'mart','i') gt 0 then
	bool = 1; /*setting boolean for words that have the string "mart" in them */
	else
	bool = 0;
	lowcaseCustomer = lowcase(compress(Customer,,'p')); /* Remove Punctuation and make letters lowercase */
run;
proc print data = newTemp;
title "CustomerDB string Manipulations";
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 23 Mar 2019 05:42:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-insert-character-in-between-a-string-without-deleting/m-p/545437#M8024</guid>
      <dc:creator>cheenaChuks</dc:creator>
      <dc:date>2019-03-23T05:42:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to insert character in between a string without deleting other characters</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-insert-character-in-between-a-string-without-deleting/m-p/545441#M8025</link>
      <description>&lt;P&gt;Before any explanation. Does this give you the desired result? &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data CustomerDB ;
  input Customer  &amp;amp;      $18.
        Item      :      $11. ; 
datalines;
Bartco Corporation  143L
Cost Cutter's  142
Minimart Inc.  188SW
Bosco, LLC  908X321
K-MART.  A122L
Food Unlimited  1898XVWX
Shop and Drop  24L
Shop Marty's  222X
Pet's are Us  W100
Roger's Spirits  1522L
Artful Spirits  407XxxXxx
Up/Down mart Inc.  ABCdefghxxx
;

data want;
   set CustomerDB;
   length NewItem $50;
   NewItem=cats("(", substr(Item, 1, length(Item)-1), "-", char(Item, length(Item)), ")");
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 23 Mar 2019 06:12:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-insert-character-in-between-a-string-without-deleting/m-p/545441#M8025</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-03-23T06:12:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to insert character in between a string without deleting other characters</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-insert-character-in-between-a-string-without-deleting/m-p/545450#M8026</link>
      <description>&lt;P&gt;When working with char-variables it is important to use the length-statement to define the variable with suitable length.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think that&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;&lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;findc&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;Item&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'mart'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'i'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;gt&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;0&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt;&lt;/LI-CODE&gt;
&lt;P&gt;has at least to issues: The variable "Item" seems to be wrongly used, it should be "Customer", right? The function findc is used to verify the presence of letters in a string, not to find words. Maybe findw is what you need here.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To debug such a step use additional variables for lengthn(Item) and the substrings you take to see what sas does.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please post the expected result, i am not sure that i fully understand the logic you want applied:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data newTemp;
    set CustomerDB;
    
    length secondtoLastRemoved $ 14;

    if lengthn(Item) ge 3 then do;
        secondtoLastRemoved = cats('(', substr(Item, 1, lengthn(Item)-1), '-', substr(Item, lengthn(Item)-1, 1), ')');
    end;
    else do;
        secondtoLastRemoved=Item;
    end;

    /*setting boolean for words that have the string "mart" in them */
    bool = index(lowcase(Customer), 'mart') &amp;gt; 0;
    *bool = findw(Customer, 'mart', , 'spit') &amp;gt; 0;
    
     /* Remove Punctuation and make letters lowercase */
    lowcaseCustomer = lowcase(compress(Customer, , 'p'));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And again &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp; was faster &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 23 Mar 2019 06:39:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-insert-character-in-between-a-string-without-deleting/m-p/545450#M8026</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-03-23T06:39:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to insert character in between a string without deleting other characters</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-insert-character-in-between-a-string-without-deleting/m-p/545451#M8027</link>
      <description>&lt;P&gt;However,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;&amp;nbsp;was way more thorough in his explanation &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 23 Mar 2019 06:41:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-insert-character-in-between-a-string-without-deleting/m-p/545451#M8027</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-03-23T06:41:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to insert character in between a string without deleting other characters</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-insert-character-in-between-a-string-without-deleting/m-p/545467#M8032</link>
      <description>&lt;P&gt;You did not post the output yet .&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 CustomerDB ;
  input Customer  &amp;amp;      $18.
        Item      :      $11. ; 
want=prxchange('s/^(\d+)([a-z]+)$/($1-$2)/i',1,strip(item));
datalines;
Bartco Corporation  143L
Cost Cutter's  142
Minimart Inc.  188SW
Bosco, LLC  908X321
K-MART.  A122L
Food Unlimited  1898XVWX
Shop and Drop  24L
Shop Marty's  222X
Pet's are Us  W100
Roger's Spirits  1522L
Artful Spirits  407XxxXxx
Up/Down mart Inc.  ABCdefghxxx
;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 23 Mar 2019 11:31:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-insert-character-in-between-a-string-without-deleting/m-p/545467#M8032</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-03-23T11:31:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to insert character in between a string without deleting other characters</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-insert-character-in-between-a-string-without-deleting/m-p/545485#M8034</link>
      <description>&lt;P&gt;You didn't show what you want the result to look like.&amp;nbsp; Here is what your words said to do.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test ;
  length Customer $18 Item $11 Want $14 ;
  input Customer &amp;amp; Item; 
  want = cats('(',substr(item,1,length(item)-1),'-',char(item,length(item)),')');
datalines;
Bartco Corporation  143L
Cost Cutter's  142
Minimart Inc.  188SW
Bosco, LLC  908X321
K-MART.  A122L
Food Unlimited  1898XVWX
Shop and Drop  24L
Shop Marty's  222X
Pet's are Us  W100
Roger's Spirits  1522L
Artful Spirits  407XxxXxx
Up/Down mart Inc.  ABCdefghxxx
;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;Obs    Customer              Item           Want

  1    Bartco Corporation    143L           (143-L)
  2    Cost Cutter's         142            (14-2)
  3    Minimart Inc.         188SW          (188S-W)
  4    Bosco, LLC            908X321        (908X32-1)
  5    K-MART.               A122L          (A122-L)
  6    Food Unlimited        1898XVWX       (1898XVW-X)
  7    Shop and Drop         24L            (24-L)
  8    Shop Marty's          222X           (222-X)
  9    Pet's are Us          W100           (W10-0)
 10    Roger's Spirits       1522L          (1522-L)
 11    Artful Spirits        407XxxXxx      (407XxxXx-x)
 12    Up/Down mart Inc.     ABCdefghxxx    (ABCdefghxx-x)
&lt;/PRE&gt;</description>
      <pubDate>Sat, 23 Mar 2019 14:50:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-insert-character-in-between-a-string-without-deleting/m-p/545485#M8034</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-03-23T14:50:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to insert character in between a string without deleting other characters</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-insert-character-in-between-a-string-without-deleting/m-p/545516#M8041</link>
      <description>&lt;P&gt;If you want to avoid problems with short Item strings, tweak &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt; 's idea a bit:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test ;
  length Customer $18 Item $11 Want $14 ;
  input Customer &amp;amp; Item; 
  want = cats('(',catx("-", substrn(item,1,length(item)-1),char(item,length(item))),')');
datalines;
Bartco Corporation  143L
Cost Cutter's  142
Minimart Inc.  188SW
Bosco, LLC  908X321
K-MART.  A122L
Food Unlimited  1898XVWX
Shop and Drop  24L
Shop Marty's  222X
Pet's are Us  W100
Roger's Spirits  1522L
Artful Spirits  407XxxXxx
Up/Down mart Inc.  ABCdefghxxx
Tiny test  a
Nasty test  .
;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;             Obs    Customer              Item           Want

               1    Bartco Corporation    143L           (143-L)
               2    Cost Cutter's         142            (14-2)
               3    Minimart Inc.         188SW          (188S-W)
               4    Bosco, LLC            908X321        (908X32-1)
               5    K-MART.               A122L          (A122-L)
               6    Food Unlimited        1898XVWX       (1898XVW-X)
               7    Shop and Drop         24L            (24-L)
               8    Shop Marty's          222X           (222-X)
               9    Pet's are Us          W100           (W10-0)
              10    Roger's Spirits       1522L          (1522-L)
              11    Artful Spirits        407XxxXxx      (407XxxXx-x)
              12    Up/Down mart Inc.     ABCdefghxxx    (ABCdefghxx-x)
              13    Tiny test             a              (a)
              14    Nasty test                           ()
&lt;/PRE&gt;</description>
      <pubDate>Sat, 23 Mar 2019 19:07:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-insert-character-in-between-a-string-without-deleting/m-p/545516#M8041</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-03-23T19:07:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to insert character in between a string without deleting other characters</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-insert-character-in-between-a-string-without-deleting/m-p/545543#M8045</link>
      <description>&lt;P&gt;Yes it does but please could you explain the program and the functions. That is what I was confused about.&lt;/P&gt;</description>
      <pubDate>Sun, 24 Mar 2019 01:11:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-insert-character-in-between-a-string-without-deleting/m-p/545543#M8045</guid>
      <dc:creator>cheenaChuks</dc:creator>
      <dc:date>2019-03-24T01:11:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to insert character in between a string without deleting other characters</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-insert-character-in-between-a-string-without-deleting/m-p/545546#M8046</link>
      <description>&lt;P&gt;Hi Andreas,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for the explanation and also pointing out the mistake I made with the findc&amp;nbsp;function. Yes, I was trying to find the name of the customer that had the string 'mart'.&amp;nbsp; Here is a snippet of the code and output.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test ;
  length Customer $18 Item $11 Want $14 ;
  input Customer &amp;amp; Item; 
  want = cats('(',substr(item,1,length(item)-1),'-',char(item,length(item)),')');
  bool = index(lowcase(Customer), 'mart') &amp;gt; 0;
datalines;
Bartco Corporation  143L
Cost Cutter's  142
Minimart Inc.  188SW
Bosco, LLC  908X321
K-MART.  A122L
Food Unlimited  1898XVWX
Shop and Drop  24L
Shop Marty's  222X
Pet's are Us  W100
Roger's Spirits  1522L
Artful Spirits  407XxxXxx
Up/Down mart Inc.  ABCdefghxxx
;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 24 Mar 2019 01:55:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-insert-character-in-between-a-string-without-deleting/m-p/545546#M8046</guid>
      <dc:creator>cheenaChuks</dc:creator>
      <dc:date>2019-03-24T01:55:40Z</dc:date>
    </item>
  </channel>
</rss>

