<?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 Regular Expression in SAS Question in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Regular-Expression-in-SAS-Question/m-p/802873#M316102</link>
    <description>&lt;P&gt;Good Afternoon,&lt;/P&gt;&lt;P&gt;I am trying to pull specific information based on a pattern shown below.&amp;nbsp; The code is shown below.&amp;nbsp; I have tested my regular expressions in other environments and they worked without issue.&amp;nbsp; I believe the issue has something to do with my lack of understanding in SAS.&amp;nbsp; My idea was to pull the required information from the end of each line (that is the reason I am using an end of line marker in RegEx)&amp;nbsp; for each applicable observation and then concatenate as needed.&amp;nbsp; However, when I tried to use that method outside of my loops it would always pull the first observation and then continue adding data onto the end of the string.&amp;nbsp; When I used this RegEx inside the loops it pulled the very last piece of data that I wanted, but it wouldn't pull earlier iterations (In the example data I posted at the bottom of this message it would pull the 2nd highlighted portion of line 3, but nothing from line 2).&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;data test;
	length text $32767;
	retain text '';
	infile msghtml flowover dlmstr='//' end=last;
	input;
	text=cats(text,'~',_infile_);
	patternID1 = prxparse("/,Trans,.*?,Trans,/s");
	patternID2 = prxparse('/^(~)([0-9]*\.[0-9]+),(Trans),(\w+),(\w+)/');
	patternID3 = prxparse('/(?&amp;lt;=\d{2}\.\d{4},Rec,DATATYPE,(\w){8}\s(\w){2})(\s\w\w)+\s+$/');

	if prxmatch(patternID1, text) then do;
			text = substr(text,1,length(text)-length(_infile_)-1);
			/* Tried placing RegEx here with end of line anchors, did not give desired result */

			if prxmatch(patternID2, text) then do;
					Match_2=prxmatch(patternID2, text);
					Match_3=prxmatch(patternID3, text);
					Buffer_Msg_Trans = prxposn(patternID2, 0, text);
					Buffer_Msg_Trans1 = prxposn(patternID2, 2, text);
					Buffer_Msg_Trans2 = prxposn(patternID2, 3, text);
					Buffer_Msg_Trans3 = prxposn(patternID2, 4, text);
					Buffer_Msg_Trans4 = prxposn(patternID2, 5, text);
					Buffer_Msg_3 = prxposn(patternID3, 0, text);
					
			end;
			text=cats('~',_infile_);
	end;
	/* Tried placing code here with end of line anchors, did not give desired results */
run;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like the following string to be the end result:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;37 46 01 80 11 00&amp;nbsp;01 02 7A 00 4F FF&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;OR&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;1)&amp;nbsp;37 46 01 80 11 00&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;2) 01 02 7A 00 4F FF&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;(So then I can easily concatenate afterwards)&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;String that is currently in my 'text' column (bold portions contain the data that I want to keep:&lt;/P&gt;&lt;P&gt;1)&amp;nbsp; ~68.1626,Trans,DATATYPE,0601&lt;BR /&gt;2)&amp;nbsp; ~68.1626,Trans,DATATYPE,0601~68.1809,Rec,DATATYPE,29EEF98A 10 &lt;STRONG&gt;37 46 01 80 11 00&lt;/STRONG&gt; 6A&lt;BR /&gt;3)&amp;nbsp; ~68.1626,Trans,DATATYPE,0601~68.1809,Rec,DATATYPE,29EEF98A 10 &lt;STRONG&gt;37 46 01 80 11 00&amp;nbsp;&lt;/STRONG&gt;6A~68.1855,Rec,DATATYPE,29EEF98A 27 &lt;STRONG&gt;01 02 7A 00 4F FF&lt;/STRONG&gt; FF&lt;BR /&gt;4)&amp;nbsp; ~69.2385,Trans,DATATYPE,0602&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help or advice would be greatly appreciated.&amp;nbsp; Thank you for your time.&lt;/P&gt;</description>
    <pubDate>Fri, 18 Mar 2022 21:46:42 GMT</pubDate>
    <dc:creator>Anthony_eng</dc:creator>
    <dc:date>2022-03-18T21:46:42Z</dc:date>
    <item>
      <title>Regular Expression in SAS Question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Regular-Expression-in-SAS-Question/m-p/802873#M316102</link>
      <description>&lt;P&gt;Good Afternoon,&lt;/P&gt;&lt;P&gt;I am trying to pull specific information based on a pattern shown below.&amp;nbsp; The code is shown below.&amp;nbsp; I have tested my regular expressions in other environments and they worked without issue.&amp;nbsp; I believe the issue has something to do with my lack of understanding in SAS.&amp;nbsp; My idea was to pull the required information from the end of each line (that is the reason I am using an end of line marker in RegEx)&amp;nbsp; for each applicable observation and then concatenate as needed.&amp;nbsp; However, when I tried to use that method outside of my loops it would always pull the first observation and then continue adding data onto the end of the string.&amp;nbsp; When I used this RegEx inside the loops it pulled the very last piece of data that I wanted, but it wouldn't pull earlier iterations (In the example data I posted at the bottom of this message it would pull the 2nd highlighted portion of line 3, but nothing from line 2).&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;data test;
	length text $32767;
	retain text '';
	infile msghtml flowover dlmstr='//' end=last;
	input;
	text=cats(text,'~',_infile_);
	patternID1 = prxparse("/,Trans,.*?,Trans,/s");
	patternID2 = prxparse('/^(~)([0-9]*\.[0-9]+),(Trans),(\w+),(\w+)/');
	patternID3 = prxparse('/(?&amp;lt;=\d{2}\.\d{4},Rec,DATATYPE,(\w){8}\s(\w){2})(\s\w\w)+\s+$/');

	if prxmatch(patternID1, text) then do;
			text = substr(text,1,length(text)-length(_infile_)-1);
			/* Tried placing RegEx here with end of line anchors, did not give desired result */

			if prxmatch(patternID2, text) then do;
					Match_2=prxmatch(patternID2, text);
					Match_3=prxmatch(patternID3, text);
					Buffer_Msg_Trans = prxposn(patternID2, 0, text);
					Buffer_Msg_Trans1 = prxposn(patternID2, 2, text);
					Buffer_Msg_Trans2 = prxposn(patternID2, 3, text);
					Buffer_Msg_Trans3 = prxposn(patternID2, 4, text);
					Buffer_Msg_Trans4 = prxposn(patternID2, 5, text);
					Buffer_Msg_3 = prxposn(patternID3, 0, text);
					
			end;
			text=cats('~',_infile_);
	end;
	/* Tried placing code here with end of line anchors, did not give desired results */
run;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like the following string to be the end result:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;37 46 01 80 11 00&amp;nbsp;01 02 7A 00 4F FF&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;OR&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;1)&amp;nbsp;37 46 01 80 11 00&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;2) 01 02 7A 00 4F FF&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;(So then I can easily concatenate afterwards)&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;String that is currently in my 'text' column (bold portions contain the data that I want to keep:&lt;/P&gt;&lt;P&gt;1)&amp;nbsp; ~68.1626,Trans,DATATYPE,0601&lt;BR /&gt;2)&amp;nbsp; ~68.1626,Trans,DATATYPE,0601~68.1809,Rec,DATATYPE,29EEF98A 10 &lt;STRONG&gt;37 46 01 80 11 00&lt;/STRONG&gt; 6A&lt;BR /&gt;3)&amp;nbsp; ~68.1626,Trans,DATATYPE,0601~68.1809,Rec,DATATYPE,29EEF98A 10 &lt;STRONG&gt;37 46 01 80 11 00&amp;nbsp;&lt;/STRONG&gt;6A~68.1855,Rec,DATATYPE,29EEF98A 27 &lt;STRONG&gt;01 02 7A 00 4F FF&lt;/STRONG&gt; FF&lt;BR /&gt;4)&amp;nbsp; ~69.2385,Trans,DATATYPE,0602&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help or advice would be greatly appreciated.&amp;nbsp; Thank you for your time.&lt;/P&gt;</description>
      <pubDate>Fri, 18 Mar 2022 21:46:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Regular-Expression-in-SAS-Question/m-p/802873#M316102</guid>
      <dc:creator>Anthony_eng</dc:creator>
      <dc:date>2022-03-18T21:46:42Z</dc:date>
    </item>
    <item>
      <title>Re: Regular Expression in SAS Question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Regular-Expression-in-SAS-Question/m-p/802949#M316149</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/408097"&gt;@Anthony_eng&lt;/a&gt;&amp;nbsp;If the sample data you've posted is sufficiently representative of what you've got then below should work.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines truncover;
  input have_string $200.;
  datalines;
~68.1626,Trans,DATATYPE,0601
~68.1626,Trans,DATATYPE,0601~68.1809,Rec,DATATYPE,29EEF98A 10 37 46 01 80 11 00 6A
~68.1626,Trans,DATATYPE,0601~68.1809,Rec,DATATYPE,29EEF98A 10 37 46 01 80 11 00 6A~68.1855,Rec,DATATYPE,29EEF98A 27 01 02 7A 00 4F FF FF
~69.2385,Trans,DATATYPE,0602
;


data want(drop=_:);
  row_id=_n_;
  set have;
  length want_string $17;
  _prxid=prxparse('/\bRec,DATATYPE,\w+\s\w{2}\s(([0-9a-f]{2}\s){6})/oi');
  _start=1;
  _stop=length(have_string);
  call prxnext(_prxid,_start,_stop,strip(have_string),_pos,_len);
  do while(_pos&amp;gt;0);
    want_string=prxposn(_prxid, 1, strip(have_string));
    output;
    call prxnext(_prxid,_start,_stop,strip(have_string),_pos,_len);
  end;
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1647734830790.png" style="width: 1405px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/69589i55431FEE4011960F/image-dimensions/1405x130?v=v2" width="1405" height="130" role="button" title="Patrick_0-1647734830790.png" alt="Patrick_0-1647734830790.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 20 Mar 2022 00:07:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Regular-Expression-in-SAS-Question/m-p/802949#M316149</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2022-03-20T00:07:29Z</dc:date>
    </item>
    <item>
      <title>Re: Regular Expression in SAS Question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Regular-Expression-in-SAS-Question/m-p/803103#M316230</link>
      <description>&lt;P&gt;Good Morning Patrick,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you so much for your help!&amp;nbsp; Your code is wonderful, I had hit a wall trying to figure out how to compile this last group of data.&amp;nbsp; Thank you again, I truly appreciate it.&lt;/P&gt;</description>
      <pubDate>Mon, 21 Mar 2022 15:17:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Regular-Expression-in-SAS-Question/m-p/803103#M316230</guid>
      <dc:creator>Anthony_eng</dc:creator>
      <dc:date>2022-03-21T15:17:19Z</dc:date>
    </item>
  </channel>
</rss>

