<?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 extract sequence from string and save as separate value in another column with counter in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-sequence-from-string-and-save-as-separate-value/m-p/853811#M337455</link>
    <description>&lt;P&gt;There is probably a slick solution with some PRXCHANGE rules but I'm not very good with those.&lt;/P&gt;
&lt;P&gt;You could take a separate pass through the value with the TRANWRD function to replace "pn " with "pn" before the code you currently use.&lt;/P&gt;
&lt;P&gt;I would make sure that the case is set before this step if you think you may have Pn pN or PN values.&lt;/P&gt;
&lt;PRE&gt;result = tranwrd(codes,"pn ","pn");
&amp;lt;next code&amp;gt;&lt;/PRE&gt;
&lt;P&gt;Caution with the Tranwrd function. Your need here should not be a problem because you are removing characters. If you use it to replace a string with a longer value value you run into the possibility of creating a resulting string that will not fit into the current defined length of the existing variable. So when adding characters you may need to define a length for new variable long enough to hold the changes and assign the result to that new variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You may also want to examine the modifiers available for the Compress function.&lt;/P&gt;
&lt;PRE&gt;data example;
   x='1!2@3#4$ abc ?&amp;gt;&amp;lt;":pn123-pn456';
   y=compress(x,' -','kad');
run;&lt;/PRE&gt;
&lt;P&gt;the 'kad' uses the k to keep the characters in the list, the a adds all alphabetic characters and d adds digits. So this keeps just letters, digits, spaces and the - character (if interested).&lt;/P&gt;
&lt;P&gt;Lots shorter typing than that long list you use in Translate.&lt;/P&gt;</description>
    <pubDate>Fri, 13 Jan 2023 20:47:55 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2023-01-13T20:47:55Z</dc:date>
    <item>
      <title>How to extract sequence from string and save as separate value in another column with counter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-sequence-from-string-and-save-as-separate-value/m-p/853801#M337449</link>
      <description>&lt;P&gt;I have data entered freestyle as a comment that looks like this:&lt;/P&gt;
&lt;P&gt;pn1111-pn2222-pn3333-pn4444-1icc1111-1icc2222-1icc3333-2icc1111&lt;/P&gt;
&lt;P&gt;I am using code like this to separate them out into pn1111, pn2222, etc.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want; set codes;
y=compbl(translate(codes,' ',"||:,*~’°-!';()®""@#$%^&amp;amp;©+=\/|[]}{]{?&amp;gt;&amp;lt;ÉÑËÁ’ÍÓÄö‘—È…...")); run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want2; set want;
y2=lowcase(y); run;

data want3; set want2;
y3=compress(y2); run;

data try; set want3;
y2=tranwrd(trim(y2), " ", ",");
call symput('y', y2);
run;
data middle2;
set try;
length term $15;
do i=1 by 1 until (term=' ');
term=strip(scan(y2,i,','));
if term ne ' ' then output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The issue is that if someone pus a space like pn 1111 in the original comment, this code creates two separate codes in the term variable, pn, and 1111.&amp;nbsp; Is there a way to format the comment somehow allowing nospace between the prefixes 'pn, 1icc, 2icc' and 4 digits, but retaining the natural space between each separate code?&amp;nbsp; Or if there's another method to do this?&lt;/P&gt;</description>
      <pubDate>Fri, 13 Jan 2023 19:48:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-sequence-from-string-and-save-as-separate-value/m-p/853801#M337449</guid>
      <dc:creator>appleorange</dc:creator>
      <dc:date>2023-01-13T19:48:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract sequence from string and save as separate value in another column with counter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-sequence-from-string-and-save-as-separate-value/m-p/853811#M337455</link>
      <description>&lt;P&gt;There is probably a slick solution with some PRXCHANGE rules but I'm not very good with those.&lt;/P&gt;
&lt;P&gt;You could take a separate pass through the value with the TRANWRD function to replace "pn " with "pn" before the code you currently use.&lt;/P&gt;
&lt;P&gt;I would make sure that the case is set before this step if you think you may have Pn pN or PN values.&lt;/P&gt;
&lt;PRE&gt;result = tranwrd(codes,"pn ","pn");
&amp;lt;next code&amp;gt;&lt;/PRE&gt;
&lt;P&gt;Caution with the Tranwrd function. Your need here should not be a problem because you are removing characters. If you use it to replace a string with a longer value value you run into the possibility of creating a resulting string that will not fit into the current defined length of the existing variable. So when adding characters you may need to define a length for new variable long enough to hold the changes and assign the result to that new variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You may also want to examine the modifiers available for the Compress function.&lt;/P&gt;
&lt;PRE&gt;data example;
   x='1!2@3#4$ abc ?&amp;gt;&amp;lt;":pn123-pn456';
   y=compress(x,' -','kad');
run;&lt;/PRE&gt;
&lt;P&gt;the 'kad' uses the k to keep the characters in the list, the a adds all alphabetic characters and d adds digits. So this keeps just letters, digits, spaces and the - character (if interested).&lt;/P&gt;
&lt;P&gt;Lots shorter typing than that long list you use in Translate.&lt;/P&gt;</description>
      <pubDate>Fri, 13 Jan 2023 20:47:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-sequence-from-string-and-save-as-separate-value/m-p/853811#M337455</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-01-13T20:47:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract sequence from string and save as separate value in another column with counter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-sequence-from-string-and-save-as-separate-value/m-p/853826#M337462</link>
      <description>&lt;P&gt;Thank you for the suggestion with the separate pass through of removing the spaces first.&amp;nbsp; That seemed to solve the issue.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have been looking at PRXCHANGE too but haven't found that slick solution yet!&lt;/P&gt;</description>
      <pubDate>Fri, 13 Jan 2023 22:25:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-sequence-from-string-and-save-as-separate-value/m-p/853826#M337462</guid>
      <dc:creator>appleorange</dc:creator>
      <dc:date>2023-01-13T22:25:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract sequence from string and save as separate value in another column with counter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-sequence-from-string-and-save-as-separate-value/m-p/853836#M337469</link>
      <description>&lt;P&gt;Scan() should do the job nicely.&lt;/P&gt;
&lt;P&gt;If there would be a need for RegEx then &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.2/lefunctionsref/n1obc9u7z3225mn1npwnassehff0.htm" target="_self"&gt;CALL PRXNEXT()&lt;/A&gt; and the sample code provided in the docu would already get you very close to the solution.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sample;
  have='pn1111-pn2222-pn3333-pn4444-1icc1111-1icc2222-1icc3333-2icc1111';
  length term $10;
  term=scan(have,1,'-');
  _i=1;
  do while(not missing(term));
    output;
    term=scan(have,_i,'-');
    _i+1;
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Jan 2023 23:45:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-sequence-from-string-and-save-as-separate-value/m-p/853836#M337469</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-01-13T23:45:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract sequence from string and save as separate value in another column with counter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-extract-sequence-from-string-and-save-as-separate-value/m-p/853956#M337525</link>
      <description>&lt;P&gt;I don't see the need for fancy regex, the duo countw + scan seem capable of solving the problem:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   have='pn1111-pn2222-pn3333-pn4444-1icc1111-1icc2222-1icc3333-2icc1111';
   length term $10;

   do i = 1 to countw(have, '-');
      term = scan(have, i, '-');
      output;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 16 Jan 2023 08:05:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-extract-sequence-from-string-and-save-as-separate-value/m-p/853956#M337525</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2023-01-16T08:05:33Z</dc:date>
    </item>
  </channel>
</rss>

