<?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: Remove leading and trailing delimiters (~) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Remove-leading-and-trailing-delimiters/m-p/603941#M175018</link>
    <description>&lt;P&gt;Try next code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do i=1 to length(var);
    if substr(var,i,1) ne '~' then do;
       var = substr(var,i);
       leave;
    end;
end;
do j=length(var) to 1 by -1;
    if substr(var,i,1) ne '~' then do;
       var = substr(var,1,j);
       leave;
    end;
end; 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;check the code what happens, and what do you want to do, in case var = '~~~~~~~' (only delimiters: put i= j=).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 13 Nov 2019 19:19:56 GMT</pubDate>
    <dc:creator>Shmuel</dc:creator>
    <dc:date>2019-11-13T19:19:56Z</dc:date>
    <item>
      <title>Remove leading and trailing delimiters (~)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-leading-and-trailing-delimiters/m-p/603924#M175011</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset with the following data&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;id&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;comment&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;123&amp;nbsp; &amp;nbsp; &amp;nbsp; ~~%abc~asd~&lt;/P&gt;&lt;P&gt;234&amp;nbsp; &amp;nbsp; &amp;nbsp; abc~def~&lt;/P&gt;&lt;P&gt;456&amp;nbsp; &amp;nbsp; &amp;nbsp;~~&amp;nbsp; .vbr~123~567&lt;/P&gt;&lt;P&gt;678&amp;nbsp; &amp;nbsp; ~*bvs~&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The Required result I need is to remove leading and trailing ~ but need the ~ in between.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;id&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;comment&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;123&amp;nbsp; &amp;nbsp; &amp;nbsp; %abc~asd&lt;/P&gt;&lt;P&gt;234&amp;nbsp; &amp;nbsp; &amp;nbsp; abc~def&lt;/P&gt;&lt;P&gt;456&amp;nbsp; &amp;nbsp; &amp;nbsp; .vbr~123~567&lt;/P&gt;&lt;P&gt;678&amp;nbsp; &amp;nbsp; &amp;nbsp;*bvs&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am guessing I should use regex here, but not sure how, can some one help thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Nov 2019 18:27:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-leading-and-trailing-delimiters/m-p/603924#M175011</guid>
      <dc:creator>shanmukh2</dc:creator>
      <dc:date>2019-11-13T18:27:09Z</dc:date>
    </item>
    <item>
      <title>Re: Remove leading and trailing delimiters (~)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-leading-and-trailing-delimiters/m-p/603930#M175012</link>
      <description>&lt;P&gt;If there are no spaces in the text then:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;var = tran(compbl(tran(var,' ','~')),'~',' ');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;that will replace multi '~' into one only.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Nov 2019 18:44:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-leading-and-trailing-delimiters/m-p/603930#M175012</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2019-11-13T18:44:34Z</dc:date>
    </item>
    <item>
      <title>Re: Remove leading and trailing delimiters (~)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-leading-and-trailing-delimiters/m-p/603936#M175016</link>
      <description>&lt;P&gt;It's not working , there are spaces in the&amp;nbsp; comments&lt;/P&gt;</description>
      <pubDate>Wed, 13 Nov 2019 18:56:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-leading-and-trailing-delimiters/m-p/603936#M175016</guid>
      <dc:creator>shanmukh2</dc:creator>
      <dc:date>2019-11-13T18:56:40Z</dc:date>
    </item>
    <item>
      <title>Re: Remove leading and trailing delimiters (~)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-leading-and-trailing-delimiters/m-p/603941#M175018</link>
      <description>&lt;P&gt;Try next code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do i=1 to length(var);
    if substr(var,i,1) ne '~' then do;
       var = substr(var,i);
       leave;
    end;
end;
do j=length(var) to 1 by -1;
    if substr(var,i,1) ne '~' then do;
       var = substr(var,1,j);
       leave;
    end;
end; 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;check the code what happens, and what do you want to do, in case var = '~~~~~~~' (only delimiters: put i= j=).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Nov 2019 19:19:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-leading-and-trailing-delimiters/m-p/603941#M175018</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2019-11-13T19:19:56Z</dc:date>
    </item>
    <item>
      <title>Re: Remove leading and trailing delimiters (~)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-leading-and-trailing-delimiters/m-p/603942#M175019</link>
      <description>&lt;P&gt;You could probably figure out how to write a regular expression pretty easily.&lt;/P&gt;
&lt;P&gt;But you could also just figure out how to find the part of the string you want by calculating some pointers into the string.&lt;/P&gt;
&lt;PRE&gt;----+---10----+---20
~~  .vbr~123~567
  ^------- first_nondlm = 3
            ^------- last_dlm = 13
               ^----- str_length = 16
  ^---------^   substr_length = 10&lt;/PRE&gt;
&lt;P&gt;Here is some code. You might need a little more logic to handle cases without trailing delimiters.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
 input id comment $40.;
cards;
123 ~~%abc~asd~
234 abc~def~
456 ~~  .vbr~123~567
678 ~*bvs~
999
;

data want;
  set have;
  first_nondlm=verify(comment,'~');
  last_dlm=findc(comment,'~','b');
  str_length=lengthn(comment);
  substr_length=min(str_length,last_dlm) - first_nondlm;
  want=substrn(comment,first_nondlm,substr_length);
run;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;                                  first_                 str_     substr_
Obs     id    comment             nondlm    last_dlm    length     length    want

 1     123    ~~%abc~asd~            3         11         11          8      %abc~asd
 2     234    abc~def~               1          8          8          7      abc~def
 3     456    ~~  .vbr~123~567       3         13         16         10        .vbr~123
 4     678    ~*bvs~                 2          6          6          4      *bvs
 5     999                           1          0          0         -1
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Nov 2019 19:26:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-leading-and-trailing-delimiters/m-p/603942#M175019</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-11-13T19:26:29Z</dc:date>
    </item>
    <item>
      <title>Re: Remove leading and trailing delimiters (~)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-leading-and-trailing-delimiters/m-p/603951#M175023</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want;
	IF _N_ = 1 THEN
		
			PATTERN = prxparse('/[^~+].+[^~+]/');

		

	RETAIN PATTERN;
	LENGTH comment_ $20 ;
	INPUT Id comment $5-20;
	CALL PRXSUBSTR(PATTERN,trim(comment),START,LENGTH);


			comment_ = SUBSTR(comment,START,LENGTH);

	KEEP Id comment comment_;
	DATALINES;
123 ~~%abc~asd~
234 abc~def~
456 ~~  .vbr~123~567
678 ~*bvs~
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 Nov 2019 19:52:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-leading-and-trailing-delimiters/m-p/603951#M175023</guid>
      <dc:creator>r_behata</dc:creator>
      <dc:date>2019-11-13T19:52:41Z</dc:date>
    </item>
    <item>
      <title>Re: Remove leading and trailing delimiters (~)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-leading-and-trailing-delimiters/m-p/603985#M175045</link>
      <description>Thank you, this solution works too.</description>
      <pubDate>Wed, 13 Nov 2019 21:40:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-leading-and-trailing-delimiters/m-p/603985#M175045</guid>
      <dc:creator>shanmukh2</dc:creator>
      <dc:date>2019-11-13T21:40:02Z</dc:date>
    </item>
    <item>
      <title>Re: Remove leading and trailing delimiters (~)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-leading-and-trailing-delimiters/m-p/603988#M175048</link>
      <description>&lt;P&gt;I wouldn't bother with regex, which I believe can be slow.&amp;nbsp; Instead, consider taking advantage of the CHAR, LENGTH, and SUBSTR functions:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input id comment $20.;
  put (_all_) (=);
datalines;
123 ~~%abc~asd~
234 abc~def~
456 ~~  .vbr~123~567
678 ~*bvs~
run;


data want (drop=_:);
  set have;
  original=comment;
  do _c=1 by 1 until (char(comment,_c)^='~');;
  end;
  comment=substr(comment,_c);
  do _c=length(comment) by -1 until(char(comment,_c)^='~');
  end;
  comment=substr(comment,1,_c);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This leads leading blanks in the ID=456 comment.&amp;nbsp; If you want to remove all leading and trailing blanks and ~'s, then consider NOT IN instead of ^=, as in:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:);
  set have;
  original=comment;
  do _c=1 by 1 until (char(comment,_c) not in ('~',' '));
  end;
  comment=substr(comment,_c);
  do _c=length(comment) by -1 until(char(comment,_c) not in ('~',' '));
  end;
  comment=substr(comment,1,_c);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 Nov 2019 21:53:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-leading-and-trailing-delimiters/m-p/603988#M175048</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-11-13T21:53:43Z</dc:date>
    </item>
    <item>
      <title>Re: Remove leading and trailing delimiters (~)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-leading-and-trailing-delimiters/m-p/603997#M175051</link>
      <description>&lt;P&gt;SCAN might be very fast IMHO&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
 input id comment $40.;
cards;
123 ~~%abc~asd~
234 abc~def~
456 ~~  .vbr~123~567
678 ~*bvs~
;

data want;
set have;
length want $50;
do _n_=1 to countw(strip(comment),'~');
 want=catx('~',want,scan(strip(comment),_n_,'~'));
end;
run;

proc print noobs;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="center"&gt;
&lt;TABLE class="table" summary="Procedure Print: Data Set WORK.WANT" frame="box" rules="all" cellspacing="0" cellpadding="5"&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="r header" scope="col"&gt;id&lt;/TH&gt;
&lt;TH class="l header" scope="col"&gt;comment&lt;/TH&gt;
&lt;TH class="l header" scope="col"&gt;want&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;123&lt;/TD&gt;
&lt;TD class="l data"&gt;~~%abc~asd~&lt;/TD&gt;
&lt;TD class="l data"&gt;%abc~asd&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;234&lt;/TD&gt;
&lt;TD class="l data"&gt;abc~def~&lt;/TD&gt;
&lt;TD class="l data"&gt;abc~def&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;456&lt;/TD&gt;
&lt;TD class="l data"&gt;~~ .vbr~123~567&lt;/TD&gt;
&lt;TD class="l data"&gt;.vbr~123~567&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;678&lt;/TD&gt;
&lt;TD class="l data"&gt;~*bvs~&lt;/TD&gt;
&lt;TD class="l data"&gt;*bvs&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;</description>
      <pubDate>Wed, 13 Nov 2019 23:55:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-leading-and-trailing-delimiters/m-p/603997#M175051</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-11-13T23:55:54Z</dc:date>
    </item>
    <item>
      <title>Re: Remove leading and trailing delimiters (~)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-leading-and-trailing-delimiters/m-p/604066#M175082</link>
      <description>&lt;PRE&gt;data have;
 input id comment $40.;
cards;
123 ~~%abc~asd~
234 abc~def~
456 ~~  .vbr~123~567
678 ~*bvs~
999
;
data want;
 set have;
 want=prxchange('s/^~+|~+$//',-1,strip(comment));
run;&lt;/PRE&gt;</description>
      <pubDate>Thu, 14 Nov 2019 12:23:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-leading-and-trailing-delimiters/m-p/604066#M175082</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-11-14T12:23:59Z</dc:date>
    </item>
  </channel>
</rss>

