<?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 How to find the target string based on position and length? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-target-string-based-on-position-and-length/m-p/563458#M157970</link>
    <description>&lt;P&gt;Dear all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am &lt;SPAN&gt;trying to separate the company name from address/introduction information. In order to get the most accurate result, I expect to separate the NAME variable based on company suffix (which in this example, are 'CO', 'LTD', 'INC', 'CO LTD', 'CO LP', 'CORP'). However, these values include at least one company suffix. So I expect to 1. find the first company suffix that appears, and then 2. find the length of company suffix. 3. if two types of company suffixes have same POSITION number, I expect to use the company suffix which has longer LENGTH. 4.sepate the NAME variable based on that company suffix.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;for example, for value,&lt;/SPAN&gt;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;AMANA CO LP A LTD PARTNERSHIP UNDER THE LAWS OF USA-DELAWARE&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;SPAN&gt;there are three kinds of company suffix, which are 'CO', 'CO LP' and ‘LTD’, &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I expect to use&amp;nbsp;&lt;/SPAN&gt;CALL PRXSUBSTR to get the position and length of these three company suffixes, and then&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1.exclude ‘LTD’ because the position of 'LTD' is behind the position of 'CO' and 'CO LP'. and then&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;3.exclude 'CO' because the length of 'CO' is shorter than the length of 'CO LP'&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;4. separate the NAME variable based on 'CO LP', namely, get 'AMANA CO LP' as a result.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;However, I do not know how to use the code most efficiently. Especially how to get the company suffix I need, and the position and length of it. Could you please give me some suggestion about this?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data HAVE;&lt;BR /&gt;input NAME :&amp;amp; $800.;&lt;BR /&gt;call prxsubstr(' CO ',NAME,POS_CO,LEN_CO);&lt;BR /&gt;call prxsubstr(' LTD ',NAME,POS_LTD,LEN_LTD);&lt;BR /&gt;call prxsubstr(' INC ',NAME,POS_INC,LEN_INC);&lt;BR /&gt;call prxsubstr(' CO LTD ',NAME,POS_CO_LTD,LEN_CO_LTD);&lt;BR /&gt;call prxsubstr(' CO LP ',NAME,POS_CO_LP,LEN_CO_LP);&lt;BR /&gt;call prxsubstr(' CORP ',NAME,POS_CORP,LEN_CORP);&lt;BR /&gt; /* find the target company suffix and its length */&lt;BR /&gt;if find(NAME,' CO ')&amp;gt;0 then do;&lt;BR /&gt;if prxmatch('/(.*) CO\s?([(\(.*\))|(\[.*\])|(\{.*\})|(''.*'')|(".*")]+)(.*)/',NAME )=0&lt;BR /&gt;and (countc(NAME, '()') ne 1 or countc(NAME, '[]') ne 1&lt;BR /&gt;or countc(NAME, '{}') ne 1 or countc(NAME, '"') ne 1 or countc(NAME, "'") ne 1) then do;&lt;BR /&gt;NAME_B=substr(NAME,1,find(NAME,' CO ')+2);&lt;BR /&gt;NAME_address=strip(substr(NAME,find(NAME,' CO ')+4, length(NAME)));&lt;BR /&gt;end;&lt;BR /&gt;else do;&lt;BR /&gt;NAME_B=substr(NAME,1,find(NAME,' CO ')+2);&lt;BR /&gt;NAME_address=strip(substr(NAME,find(NAME,' CO ')+4, length(NAME)));&lt;BR /&gt;end;&lt;BR /&gt;end;&lt;BR /&gt;if find(NAME,' LTD ')&amp;gt;0 then do;&lt;BR /&gt;if prxmatch('/(.*) LTD\s?([(\(.*\))|(\[.*\])|(\{.*\})|(''.*'')|(".*")]+)(.*)/',NAME )=0&lt;BR /&gt;and (countc(NAME, '()') ne 1 or countc(NAME, '[]') ne 1&lt;BR /&gt;or countc(NAME, '{}') ne 1 or countc(NAME, '"') ne 1 or countc(NAME, "'") ne 1) then do;&lt;BR /&gt;NAME_B=substr(NAME,1,find(NAME,' LTD ')+3);&lt;BR /&gt;NAME_address=strip(substr(NAME,find(NAME,' LTD ')+5, length(NAME)));&lt;BR /&gt;end;&lt;BR /&gt;else do;&lt;BR /&gt;NAME_B=substr(NAME,1,find(NAME,' LTD ')+3);&lt;BR /&gt;NAME_address=strip(substr(NAME,find(NAME,' LTD ')+5, length(NAME)));&lt;BR /&gt;end;&lt;BR /&gt;end;&lt;BR /&gt;if find(NAME,' INC ')&amp;gt;0 then do;&lt;BR /&gt;if prxmatch('/(.*) INC\s?([(\(.*\))|(\[.*\])|(\{.*\})|(''.*'')|(".*")]+)(.*)/',NAME )=0&lt;BR /&gt;and (countc(NAME, '()') ne 1 or countc(NAME, '[]') ne 1&lt;BR /&gt;or countc(NAME, '{}') ne 1 or countc(NAME, '"') ne 1 or countc(NAME, "'") ne 1) then do;&lt;BR /&gt;NAME_B=substr(NAME,1,find(NAME,' INC ')+3);&lt;BR /&gt;NAME_address=strip(substr(NAME,find(NAME,' INC ')+5, length(NAME)));&lt;BR /&gt;end;&lt;BR /&gt;else do;&lt;BR /&gt;NAME_B=substr(NAME,1,find(NAME,' INC ')+3);&lt;BR /&gt;NAME_address=strip(substr(NAME,find(NAME,' INC ')+5, length(NAME)));&lt;BR /&gt;end;&lt;BR /&gt;end;&lt;BR /&gt;if find(NAME,' CO LTD ')&amp;gt;0 then do;&lt;BR /&gt;if prxmatch('/(.*) CO LTD\s?([(\(.*\))|(\[.*\])|(\{.*\})|(''.*'')|(".*")]+)(.*)/',NAME )=0&lt;BR /&gt;and (countc(NAME, '()') ne 1 or countc(NAME, '[]') ne 1&lt;BR /&gt;or countc(NAME, '{}') ne 1 or countc(NAME, '"') ne 1 or countc(NAME, "'") ne 1) then do;&lt;BR /&gt;NAME_B=substr(NAME,1,find(NAME,' CO LTD ')+6);&lt;BR /&gt;NAME_address=strip(substr(NAME,find(NAME,' CO LTD ')+8, length(NAME)));&lt;BR /&gt;end;&lt;BR /&gt;else do;&lt;BR /&gt;NAME_B=substr(NAME,1,find(NAME,' CO LTD ')+6);&lt;BR /&gt;NAME_address=strip(substr(NAME,find(NAME,' CO LTD ')+8, length(NAME)));&lt;BR /&gt;end;&lt;BR /&gt;end; &lt;BR /&gt;if find(NAME,' CO LP ')&amp;gt;0 then do;&lt;BR /&gt;if prxmatch('/(.*) CO LP\s?([(\(.*\))|(\[.*\])|(\{.*\})|(''.*'')|(".*")]+)(.*)/',NAME )=0&lt;BR /&gt;and (countc(NAME, '()') ne 1 or countc(NAME, '[]') ne 1&lt;BR /&gt;or countc(NAME, '{}') ne 1 or countc(NAME, '"') ne 1 or countc(NAME, "'") ne 1) then do;&lt;BR /&gt;NAME_B=substr(NAME,1,find(NAME,' CO LP ')+5);&lt;BR /&gt;NAME_address=strip(substr(NAME,find(NAME,' CO LP ')+7, length(NAME)));&lt;BR /&gt;end;&lt;BR /&gt;else do;&lt;BR /&gt;NAME_B=substr(NAME,1,find(NAME,' CO LP ')+5);&lt;BR /&gt;NAME_address=strip(substr(NAME,find(NAME,' CO LP ')+7, length(NAME)));&lt;BR /&gt;end;&lt;BR /&gt;end;&lt;BR /&gt;if find(NAME,' CORP ')&amp;gt;0 then do;&lt;BR /&gt;if prxmatch('/(.*) CORP\s?([(\(.*\))|(\[.*\])|(\{.*\})|(''.*'')|(".*")]+)(.*)/',NAME )=0&lt;BR /&gt;and (countc(NAME, '()') ne 1 or countc(NAME, '[]') ne 1&lt;BR /&gt;or countc(NAME, '{}') ne 1 or countc(NAME, '"') ne 1 or countc(NAME, "'") ne 1) then do;&lt;BR /&gt;NAME_B=substr(NAME,1,find(NAME,' CORP ')+3);&lt;BR /&gt;NAME_address=strip(substr(NAME,find(NAME,' CORP ')+5, length(NAME)));&lt;BR /&gt;end;&lt;BR /&gt;else do;&lt;BR /&gt;NAME_B=substr(NAME,1,find(NAME,' CORP ')+3);&lt;BR /&gt;NAME_address=strip(substr(NAME,find(NAME,' CORP ')+5, length(NAME)));&lt;BR /&gt;end;&lt;BR /&gt;end;&lt;BR /&gt;&lt;BR /&gt;cards;&lt;BR /&gt;3M INNOVATIVE PROPERTIES CO NIPPON TELEGRAPH &amp;amp; TELEPHONE CORP &lt;BR /&gt;A. STUCKI CO A DELAWARE CORP&lt;BR /&gt;AMANA CO LP A LTD PARTNERSHIP UNDER THE LAWS OF USA-DELAWARE&lt;BR /&gt;FEDERAL-HOFFMAN INC D.B.A. FEDERAL CARTRIDGE CO&lt;BR /&gt;3M CO (MINNESOTA MINING AND MANUFACTURING COMPANY) &lt;BR /&gt;3M CO (MINNESOTA MINING AND MANUFACTURING COMPANY &lt;BR /&gt;3M CO (MINNESOTA MINING AND MANUFACTURING COMPANY) A LTD IN UK &lt;BR /&gt;run; &lt;/PRE&gt;&lt;P&gt;as a result, I expect to get&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;NAME&lt;/TD&gt;&lt;TD&gt;NAME_B&lt;/TD&gt;&lt;TD&gt;NAME_address&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3M INNOVATIVE PROPERTIES CO NIPPON TELEGRAPH &amp;amp; TELEPHONE CORP&lt;/TD&gt;&lt;TD&gt;3M INNOVATIVE PROPERTIES CO&lt;/TD&gt;&lt;TD&gt;NIPPON TELEGRAPH &amp;amp; TELEPHONE CORP&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;A. STUCKI CO A DELAWARE CORP&lt;/TD&gt;&lt;TD&gt;A. STUCKI CO&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;A DELAWARE CORP&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;AMANA CO LP A LTD PARTNERSHIP UNDER THE LAWS OF USA-DELAWARE&lt;/TD&gt;&lt;TD&gt;AMANA CO LP&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;A LTD PARTNERSHIP UNDER THE LAWS OF USA-DELAWARE&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;FEDERAL-HOFFMAN INC D.B.A. FEDERAL CARTRIDGE CO&lt;/TD&gt;&lt;TD&gt;FEDERAL-HOFFMAN INC&lt;/TD&gt;&lt;TD&gt;D.B.A. FEDERAL CARTRIDGE CO&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3M CO (MINNESOTA MINING AND MANUFACTURING COMPANY)&lt;/TD&gt;&lt;TD&gt;3M CO (MINNESOTA MINING AND MANUFACTURING COMPANY)&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3M CO (MINNESOTA MINING AND MANUFACTURING COMPANY&lt;/TD&gt;&lt;TD&gt;3M CO&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;(MINNESOTA MINING AND MANUFACTURING COMPANY&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3M CO (MINNESOTA MINING AND MANUFACTURING COMPANY) A LTD IN UK&lt;/TD&gt;&lt;TD&gt;3M CO (MINNESOTA MINING AND MANUFACTURING COMPANY)&lt;/TD&gt;&lt;TD&gt;A LTD IN UK&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
    <pubDate>Tue, 04 Jun 2019 08:01:49 GMT</pubDate>
    <dc:creator>Alexxxxxxx</dc:creator>
    <dc:date>2019-06-04T08:01:49Z</dc:date>
    <item>
      <title>How to find the target string based on position and length?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-target-string-based-on-position-and-length/m-p/563458#M157970</link>
      <description>&lt;P&gt;Dear all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am &lt;SPAN&gt;trying to separate the company name from address/introduction information. In order to get the most accurate result, I expect to separate the NAME variable based on company suffix (which in this example, are 'CO', 'LTD', 'INC', 'CO LTD', 'CO LP', 'CORP'). However, these values include at least one company suffix. So I expect to 1. find the first company suffix that appears, and then 2. find the length of company suffix. 3. if two types of company suffixes have same POSITION number, I expect to use the company suffix which has longer LENGTH. 4.sepate the NAME variable based on that company suffix.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;for example, for value,&lt;/SPAN&gt;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;AMANA CO LP A LTD PARTNERSHIP UNDER THE LAWS OF USA-DELAWARE&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;SPAN&gt;there are three kinds of company suffix, which are 'CO', 'CO LP' and ‘LTD’, &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I expect to use&amp;nbsp;&lt;/SPAN&gt;CALL PRXSUBSTR to get the position and length of these three company suffixes, and then&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1.exclude ‘LTD’ because the position of 'LTD' is behind the position of 'CO' and 'CO LP'. and then&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;3.exclude 'CO' because the length of 'CO' is shorter than the length of 'CO LP'&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;4. separate the NAME variable based on 'CO LP', namely, get 'AMANA CO LP' as a result.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;However, I do not know how to use the code most efficiently. Especially how to get the company suffix I need, and the position and length of it. Could you please give me some suggestion about this?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data HAVE;&lt;BR /&gt;input NAME :&amp;amp; $800.;&lt;BR /&gt;call prxsubstr(' CO ',NAME,POS_CO,LEN_CO);&lt;BR /&gt;call prxsubstr(' LTD ',NAME,POS_LTD,LEN_LTD);&lt;BR /&gt;call prxsubstr(' INC ',NAME,POS_INC,LEN_INC);&lt;BR /&gt;call prxsubstr(' CO LTD ',NAME,POS_CO_LTD,LEN_CO_LTD);&lt;BR /&gt;call prxsubstr(' CO LP ',NAME,POS_CO_LP,LEN_CO_LP);&lt;BR /&gt;call prxsubstr(' CORP ',NAME,POS_CORP,LEN_CORP);&lt;BR /&gt; /* find the target company suffix and its length */&lt;BR /&gt;if find(NAME,' CO ')&amp;gt;0 then do;&lt;BR /&gt;if prxmatch('/(.*) CO\s?([(\(.*\))|(\[.*\])|(\{.*\})|(''.*'')|(".*")]+)(.*)/',NAME )=0&lt;BR /&gt;and (countc(NAME, '()') ne 1 or countc(NAME, '[]') ne 1&lt;BR /&gt;or countc(NAME, '{}') ne 1 or countc(NAME, '"') ne 1 or countc(NAME, "'") ne 1) then do;&lt;BR /&gt;NAME_B=substr(NAME,1,find(NAME,' CO ')+2);&lt;BR /&gt;NAME_address=strip(substr(NAME,find(NAME,' CO ')+4, length(NAME)));&lt;BR /&gt;end;&lt;BR /&gt;else do;&lt;BR /&gt;NAME_B=substr(NAME,1,find(NAME,' CO ')+2);&lt;BR /&gt;NAME_address=strip(substr(NAME,find(NAME,' CO ')+4, length(NAME)));&lt;BR /&gt;end;&lt;BR /&gt;end;&lt;BR /&gt;if find(NAME,' LTD ')&amp;gt;0 then do;&lt;BR /&gt;if prxmatch('/(.*) LTD\s?([(\(.*\))|(\[.*\])|(\{.*\})|(''.*'')|(".*")]+)(.*)/',NAME )=0&lt;BR /&gt;and (countc(NAME, '()') ne 1 or countc(NAME, '[]') ne 1&lt;BR /&gt;or countc(NAME, '{}') ne 1 or countc(NAME, '"') ne 1 or countc(NAME, "'") ne 1) then do;&lt;BR /&gt;NAME_B=substr(NAME,1,find(NAME,' LTD ')+3);&lt;BR /&gt;NAME_address=strip(substr(NAME,find(NAME,' LTD ')+5, length(NAME)));&lt;BR /&gt;end;&lt;BR /&gt;else do;&lt;BR /&gt;NAME_B=substr(NAME,1,find(NAME,' LTD ')+3);&lt;BR /&gt;NAME_address=strip(substr(NAME,find(NAME,' LTD ')+5, length(NAME)));&lt;BR /&gt;end;&lt;BR /&gt;end;&lt;BR /&gt;if find(NAME,' INC ')&amp;gt;0 then do;&lt;BR /&gt;if prxmatch('/(.*) INC\s?([(\(.*\))|(\[.*\])|(\{.*\})|(''.*'')|(".*")]+)(.*)/',NAME )=0&lt;BR /&gt;and (countc(NAME, '()') ne 1 or countc(NAME, '[]') ne 1&lt;BR /&gt;or countc(NAME, '{}') ne 1 or countc(NAME, '"') ne 1 or countc(NAME, "'") ne 1) then do;&lt;BR /&gt;NAME_B=substr(NAME,1,find(NAME,' INC ')+3);&lt;BR /&gt;NAME_address=strip(substr(NAME,find(NAME,' INC ')+5, length(NAME)));&lt;BR /&gt;end;&lt;BR /&gt;else do;&lt;BR /&gt;NAME_B=substr(NAME,1,find(NAME,' INC ')+3);&lt;BR /&gt;NAME_address=strip(substr(NAME,find(NAME,' INC ')+5, length(NAME)));&lt;BR /&gt;end;&lt;BR /&gt;end;&lt;BR /&gt;if find(NAME,' CO LTD ')&amp;gt;0 then do;&lt;BR /&gt;if prxmatch('/(.*) CO LTD\s?([(\(.*\))|(\[.*\])|(\{.*\})|(''.*'')|(".*")]+)(.*)/',NAME )=0&lt;BR /&gt;and (countc(NAME, '()') ne 1 or countc(NAME, '[]') ne 1&lt;BR /&gt;or countc(NAME, '{}') ne 1 or countc(NAME, '"') ne 1 or countc(NAME, "'") ne 1) then do;&lt;BR /&gt;NAME_B=substr(NAME,1,find(NAME,' CO LTD ')+6);&lt;BR /&gt;NAME_address=strip(substr(NAME,find(NAME,' CO LTD ')+8, length(NAME)));&lt;BR /&gt;end;&lt;BR /&gt;else do;&lt;BR /&gt;NAME_B=substr(NAME,1,find(NAME,' CO LTD ')+6);&lt;BR /&gt;NAME_address=strip(substr(NAME,find(NAME,' CO LTD ')+8, length(NAME)));&lt;BR /&gt;end;&lt;BR /&gt;end; &lt;BR /&gt;if find(NAME,' CO LP ')&amp;gt;0 then do;&lt;BR /&gt;if prxmatch('/(.*) CO LP\s?([(\(.*\))|(\[.*\])|(\{.*\})|(''.*'')|(".*")]+)(.*)/',NAME )=0&lt;BR /&gt;and (countc(NAME, '()') ne 1 or countc(NAME, '[]') ne 1&lt;BR /&gt;or countc(NAME, '{}') ne 1 or countc(NAME, '"') ne 1 or countc(NAME, "'") ne 1) then do;&lt;BR /&gt;NAME_B=substr(NAME,1,find(NAME,' CO LP ')+5);&lt;BR /&gt;NAME_address=strip(substr(NAME,find(NAME,' CO LP ')+7, length(NAME)));&lt;BR /&gt;end;&lt;BR /&gt;else do;&lt;BR /&gt;NAME_B=substr(NAME,1,find(NAME,' CO LP ')+5);&lt;BR /&gt;NAME_address=strip(substr(NAME,find(NAME,' CO LP ')+7, length(NAME)));&lt;BR /&gt;end;&lt;BR /&gt;end;&lt;BR /&gt;if find(NAME,' CORP ')&amp;gt;0 then do;&lt;BR /&gt;if prxmatch('/(.*) CORP\s?([(\(.*\))|(\[.*\])|(\{.*\})|(''.*'')|(".*")]+)(.*)/',NAME )=0&lt;BR /&gt;and (countc(NAME, '()') ne 1 or countc(NAME, '[]') ne 1&lt;BR /&gt;or countc(NAME, '{}') ne 1 or countc(NAME, '"') ne 1 or countc(NAME, "'") ne 1) then do;&lt;BR /&gt;NAME_B=substr(NAME,1,find(NAME,' CORP ')+3);&lt;BR /&gt;NAME_address=strip(substr(NAME,find(NAME,' CORP ')+5, length(NAME)));&lt;BR /&gt;end;&lt;BR /&gt;else do;&lt;BR /&gt;NAME_B=substr(NAME,1,find(NAME,' CORP ')+3);&lt;BR /&gt;NAME_address=strip(substr(NAME,find(NAME,' CORP ')+5, length(NAME)));&lt;BR /&gt;end;&lt;BR /&gt;end;&lt;BR /&gt;&lt;BR /&gt;cards;&lt;BR /&gt;3M INNOVATIVE PROPERTIES CO NIPPON TELEGRAPH &amp;amp; TELEPHONE CORP &lt;BR /&gt;A. STUCKI CO A DELAWARE CORP&lt;BR /&gt;AMANA CO LP A LTD PARTNERSHIP UNDER THE LAWS OF USA-DELAWARE&lt;BR /&gt;FEDERAL-HOFFMAN INC D.B.A. FEDERAL CARTRIDGE CO&lt;BR /&gt;3M CO (MINNESOTA MINING AND MANUFACTURING COMPANY) &lt;BR /&gt;3M CO (MINNESOTA MINING AND MANUFACTURING COMPANY &lt;BR /&gt;3M CO (MINNESOTA MINING AND MANUFACTURING COMPANY) A LTD IN UK &lt;BR /&gt;run; &lt;/PRE&gt;&lt;P&gt;as a result, I expect to get&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;NAME&lt;/TD&gt;&lt;TD&gt;NAME_B&lt;/TD&gt;&lt;TD&gt;NAME_address&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3M INNOVATIVE PROPERTIES CO NIPPON TELEGRAPH &amp;amp; TELEPHONE CORP&lt;/TD&gt;&lt;TD&gt;3M INNOVATIVE PROPERTIES CO&lt;/TD&gt;&lt;TD&gt;NIPPON TELEGRAPH &amp;amp; TELEPHONE CORP&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;A. STUCKI CO A DELAWARE CORP&lt;/TD&gt;&lt;TD&gt;A. STUCKI CO&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;A DELAWARE CORP&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;AMANA CO LP A LTD PARTNERSHIP UNDER THE LAWS OF USA-DELAWARE&lt;/TD&gt;&lt;TD&gt;AMANA CO LP&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;A LTD PARTNERSHIP UNDER THE LAWS OF USA-DELAWARE&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;FEDERAL-HOFFMAN INC D.B.A. FEDERAL CARTRIDGE CO&lt;/TD&gt;&lt;TD&gt;FEDERAL-HOFFMAN INC&lt;/TD&gt;&lt;TD&gt;D.B.A. FEDERAL CARTRIDGE CO&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3M CO (MINNESOTA MINING AND MANUFACTURING COMPANY)&lt;/TD&gt;&lt;TD&gt;3M CO (MINNESOTA MINING AND MANUFACTURING COMPANY)&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3M CO (MINNESOTA MINING AND MANUFACTURING COMPANY&lt;/TD&gt;&lt;TD&gt;3M CO&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;(MINNESOTA MINING AND MANUFACTURING COMPANY&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3M CO (MINNESOTA MINING AND MANUFACTURING COMPANY) A LTD IN UK&lt;/TD&gt;&lt;TD&gt;3M CO (MINNESOTA MINING AND MANUFACTURING COMPANY)&lt;/TD&gt;&lt;TD&gt;A LTD IN UK&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Tue, 04 Jun 2019 08:01:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-target-string-based-on-position-and-length/m-p/563458#M157970</guid>
      <dc:creator>Alexxxxxxx</dc:creator>
      <dc:date>2019-06-04T08:01:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the target string based on position and length?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-target-string-based-on-position-and-length/m-p/563634#M158037</link>
      <description>&lt;P&gt;You have to test for the&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;if find(NAME,' CO LTD ') then do;&lt;/PRE&gt;
&lt;P&gt;block before the&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;if find(NAME,' CO ') then do;&lt;/PRE&gt;
&lt;P&gt;block.&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jun 2019 03:19:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-target-string-based-on-position-and-length/m-p/563634#M158037</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-06-05T03:19:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the target string based on position and length?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-target-string-based-on-position-and-length/m-p/563637#M158038</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your suggestion.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, it is a large and complicated data, for example, the value like,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;BPB CO A UK CO LTD
BROADCOM UK CO LTD A DELAWARE CO&amp;lt;code&amp;gt;&amp;lt;/code&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;So, I am trying to check them one by one.&lt;/P&gt;&lt;P&gt;At the current stage, I can find the first company suffix by the following code,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data HAVE;
input NAME :&amp;amp; $800.;
cards;
A. STUCKI CO A DELAWARE CORP
AMANA CO LP A LTD PARTNERSHIP UNDER THE LAWS OF USA-DELAWARE
run; 


data want;
set HAVE;
patternID = prxparse('/ CO /');
call prxsubstr(patternID,NAME,POS_CO,LEN_CO);
patternID = prxparse('/ LTD /');
call prxsubstr(patternID,NAME,POS_LTD,LEN_LTD);
patternID = prxparse('/ CO LP /');
call prxsubstr(patternID,NAME,POS_CO_LP,LEN_CO_LP);
patternID = prxparse('/ CORP /');
call prxsubstr(patternID,NAME,POS_CORP,LEN_CORP);

array x[*] POS_: ;
do k=1 to patternID;
smallest = smallest(k, of x[*]);
if smallest &amp;gt;0 then output;
end;
drop k patternID;

run;


proc sql;
create table want1 as
select distinct 
*
from want
group by NAME
having smallest=min(smallest)
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;the ‘smallest’ is the position of the first company suffix, however, the position will be the same for 'CO' and 'CO LP' in value '&lt;CODE class=" language-sas"&gt;AMANA CO LP A LTD PARTNERSHIP UNDER THE LAWS OF USA-DELAWARE'&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So I need to 1.select the variable which has the longer length (in this example, it is 'CO LP'). and know which suffix is selected.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;by using the result of the code, Table 2, as an example&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;NAME&lt;/TD&gt;&lt;TD&gt;POS_CO&lt;/TD&gt;&lt;TD&gt;LEN_CO&lt;/TD&gt;&lt;TD&gt;POS_LTD&lt;/TD&gt;&lt;TD&gt;LEN_LTD&lt;/TD&gt;&lt;TD&gt;POS_CO_LP&lt;/TD&gt;&lt;TD&gt;LEN_CO_LP&lt;/TD&gt;&lt;TD&gt;POS_CORP&lt;/TD&gt;&lt;TD&gt;LEN_CORP&lt;/TD&gt;&lt;TD&gt;smallest&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;A. STUCKI CO A DELAWARE CORP&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;10&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;24&lt;/TD&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;10&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;AMANA CO LP A LTD PARTNERSHIP UNDER THE LAWS OF USA-DELAWARE&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;6&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;14&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;6&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;7&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;6&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When POS_CO and POS_CO_LP both equal smallest (which is 6), I expect to select CO LP because the number of&amp;nbsp;LEN_CO (which is 4) is larger than the number of LEN_CO_LP(which is 7).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Finally, I expect to get a result,&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;NAME&lt;/TD&gt;&lt;TD&gt;POS_CO&lt;/TD&gt;&lt;TD&gt;LEN_CO&lt;/TD&gt;&lt;TD&gt;POS_LTD&lt;/TD&gt;&lt;TD&gt;LEN_LTD&lt;/TD&gt;&lt;TD&gt;POS_CO_LP&lt;/TD&gt;&lt;TD&gt;LEN_CO_LP&lt;/TD&gt;&lt;TD&gt;POS_CORP&lt;/TD&gt;&lt;TD&gt;LEN_CORP&lt;/TD&gt;&lt;TD&gt;smallest&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;largest&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;suffix&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;A. STUCKI CO A DELAWARE CORP&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;24&lt;/TD&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;CO&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;AMANA CO LP A LTD PARTNERSHIP UNDER THE LAWS OF USA-DELAWARE&lt;/TD&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;14&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;TD&gt;7&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;TD&gt;7&lt;/TD&gt;&lt;TD&gt;CO LP&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could you please give me some suggestion about this?&lt;/P&gt;&lt;P&gt;thanks in advance.&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jun 2019 04:24:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-target-string-based-on-position-and-length/m-p/563637#M158038</guid>
      <dc:creator>Alexxxxxxx</dc:creator>
      <dc:date>2019-06-05T04:24:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the target string based on position and length?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-target-string-based-on-position-and-length/m-p/563639#M158039</link>
      <description>&lt;P&gt;&lt;SPAN&gt;&amp;gt; &lt;EM&gt;the ‘smallest’ is the position of the first company suffix&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Ok so we are on the same wave length.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Use index() instead of&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;patternID = prxparse('/ CO /')&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;it's much faster.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. You need to keep track of which string got you the smallest position and use it.&lt;/P&gt;
&lt;P&gt;Basic IF THEN tests on the POS variables is the way to do it.&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;if&amp;nbsp;&lt;SPAN&gt;POS_CO = SMALLEST then do; &lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;..&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;else if etc&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jun 2019 05:00:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-target-string-based-on-position-and-length/m-p/563639#M158039</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-06-05T05:00:20Z</dc:date>
    </item>
  </channel>
</rss>

