<?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: SUB STING Problem in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SUB-STING-Problem/m-p/628542#M185759</link>
    <description>&lt;P&gt;compress() strips all leading and trailing blanks, so the resulting string is only 8 or 9 bytes long. This lets the checks for 40 (in all cases) and 9 (in most other cases) fail. The substr() function issues a NOTE (Maxim 2: Read the Log) and returns blank values, which match in the check for 40.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You are better off using a do loop to determine the match length:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data testing2;
input name $40.;
infile datalines dlm=',';
datalines;
checking
checking
checking
checking
checking1
checking2
checking2
;
run;
 
data testing4;
set testing2;
name2=compress(name);
lag_name=compress(lag(name));
do i = 1 to length(name2);
  if substr(name2,1,i) = substr(lag_name,1,i) then Length_match_Lag = i;
end;
drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I added a second "checking2" value to get one result of 9.&lt;/P&gt;</description>
    <pubDate>Sun, 01 Mar 2020 10:33:27 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2020-03-01T10:33:27Z</dc:date>
    <item>
      <title>SUB STING Problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SUB-STING-Problem/m-p/628528#M185750</link>
      <description>&lt;P&gt;good day,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;here is my program.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;just wondering why if the up and down row are exact the same. the result always = 40?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;is it something wrong with my program? word "checking" only 8 character.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data testing2;&lt;BR /&gt;input name $40.;&lt;BR /&gt;infile datalines dlm=',';&lt;BR /&gt;datalines;&lt;BR /&gt;checking&lt;BR /&gt;checking&lt;BR /&gt;checking&lt;BR /&gt;checking&lt;BR /&gt;checking1&lt;BR /&gt;checking2&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data testing3;&lt;BR /&gt;set testing2;&lt;BR /&gt;name2=compress(name);&lt;BR /&gt;lag_name=compress(lag(name));&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data testing4;&lt;BR /&gt;set testing3;&lt;/P&gt;
&lt;P&gt;if substr(compress(name2),1,40) = substr(compress(lag_name),1,40) then Length_match_Lag ="40";&lt;BR /&gt;else if substr(compress(name2),1,9) = substr(compress(lag_name),1,9) then Length_match_Lag ="9";&lt;BR /&gt;else if substr(compress(name2),1,8) = substr(compress(lag_name),1,8) then Length_match_Lag ="8";&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;thanks in advance&lt;/P&gt;
&lt;P&gt;harry&lt;/P&gt;</description>
      <pubDate>Sun, 01 Mar 2020 08:20:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SUB-STING-Problem/m-p/628528#M185750</guid>
      <dc:creator>harrylui</dc:creator>
      <dc:date>2020-03-01T08:20:48Z</dc:date>
    </item>
    <item>
      <title>Re: SUB STING Problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SUB-STING-Problem/m-p/628542#M185759</link>
      <description>&lt;P&gt;compress() strips all leading and trailing blanks, so the resulting string is only 8 or 9 bytes long. This lets the checks for 40 (in all cases) and 9 (in most other cases) fail. The substr() function issues a NOTE (Maxim 2: Read the Log) and returns blank values, which match in the check for 40.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You are better off using a do loop to determine the match length:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data testing2;
input name $40.;
infile datalines dlm=',';
datalines;
checking
checking
checking
checking
checking1
checking2
checking2
;
run;
 
data testing4;
set testing2;
name2=compress(name);
lag_name=compress(lag(name));
do i = 1 to length(name2);
  if substr(name2,1,i) = substr(lag_name,1,i) then Length_match_Lag = i;
end;
drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I added a second "checking2" value to get one result of 9.&lt;/P&gt;</description>
      <pubDate>Sun, 01 Mar 2020 10:33:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SUB-STING-Problem/m-p/628542#M185759</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-03-01T10:33:27Z</dc:date>
    </item>
    <item>
      <title>Re: SUB STING Problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SUB-STING-Problem/m-p/628555#M185765</link>
      <description>&lt;P&gt;What is the test you are tying to do?&amp;nbsp; Why are you using COMPRESS()?&amp;nbsp; Are you trying to remove the embedded spaces from the values?&amp;nbsp; Why do you keep calling it after you already removed them?&amp;nbsp; Did you expect them re-appear somehow?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  input name $40.;
  lag_name = lag(name);
  match40 = name = lag_name;
  match9 = substr(name,1,9) = substr(lag_name,1,9);
  match8 = substr(name,1,8) = substr(lag_name,1,8);
datalines;
checking
checking
checking
checking
checking1
checking2
checking2
;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;Obs      name       lag_name     match40    match9    match8

 1     checking                     0          0         0
 2     checking     checking        1          1         1
 3     checking     checking        1          1         1
 4     checking     checking        1          1         1
 5     checking1    checking        0          0         1
 6     checking2    checking1       0          0         1
 7     checking2    checking2       1          1         1&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 01 Mar 2020 15:12:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SUB-STING-Problem/m-p/628555#M185765</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-03-01T15:12:11Z</dc:date>
    </item>
    <item>
      <title>Re: SUB STING Problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SUB-STING-Problem/m-p/629003#M185944</link>
      <description>&lt;P&gt;thanks you all&lt;/P&gt;</description>
      <pubDate>Tue, 03 Mar 2020 02:29:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SUB-STING-Problem/m-p/629003#M185944</guid>
      <dc:creator>harrylui</dc:creator>
      <dc:date>2020-03-03T02:29:20Z</dc:date>
    </item>
  </channel>
</rss>

