<?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 Convert long string into variable EDIFACT in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Convert-long-string-into-variable-EDIFACT/m-p/402096#M97609</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I am dealing with a dataset which has a field containing a so-called EDIFACT message. It menas a long streng containing different inormation. I want to parse and divide the long text string into sas variables. This should be based on char string characters - se example below.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any ideas or code to a nice and easy solution from SAS???&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;STRONG&gt;Input data: one long string&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;NAD+PERSONnumber+3456789010'DTM+091:170126:101'NAD+ENTITY+027'NAD+KLA+11'
NAD+YNR+'NAD+MOK+405'TAF+55:45:53:52:51:61:62:63:64:65:85:84:83:82:81:71:72:73:74:75'
TOB+85:OKK:0'TOB+75:OKK:0’TOB+63:MES:4'QTY+DYB:X'&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;I want to create these new variables:&lt;/STRONG&gt;&lt;/P&gt;
&lt;PRE&gt;PERSONnumber=3456789010
DTM=091:170126
ENTITY=027
KLA=11
YNR=+
MOK=405
TAF=55:45:53:52:51:61:62:63:64:65:85:84:83:82:81:71:72:73:74:75'
TOB85=OKK:0
TOB75=OKK:0
TOB63= MES:4'QTY+DYB:X'&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 07 Oct 2017 16:46:30 GMT</pubDate>
    <dc:creator>ANLYNG</dc:creator>
    <dc:date>2017-10-07T16:46:30Z</dc:date>
    <item>
      <title>Convert long string into variable EDIFACT</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-long-string-into-variable-EDIFACT/m-p/402096#M97609</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I am dealing with a dataset which has a field containing a so-called EDIFACT message. It menas a long streng containing different inormation. I want to parse and divide the long text string into sas variables. This should be based on char string characters - se example below.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any ideas or code to a nice and easy solution from SAS???&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;STRONG&gt;Input data: one long string&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;NAD+PERSONnumber+3456789010'DTM+091:170126:101'NAD+ENTITY+027'NAD+KLA+11'
NAD+YNR+'NAD+MOK+405'TAF+55:45:53:52:51:61:62:63:64:65:85:84:83:82:81:71:72:73:74:75'
TOB+85:OKK:0'TOB+75:OKK:0’TOB+63:MES:4'QTY+DYB:X'&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;I want to create these new variables:&lt;/STRONG&gt;&lt;/P&gt;
&lt;PRE&gt;PERSONnumber=3456789010
DTM=091:170126
ENTITY=027
KLA=11
YNR=+
MOK=405
TAF=55:45:53:52:51:61:62:63:64:65:85:84:83:82:81:71:72:73:74:75'
TOB85=OKK:0
TOB75=OKK:0
TOB63= MES:4'QTY+DYB:X'&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 07 Oct 2017 16:46:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-long-string-into-variable-EDIFACT/m-p/402096#M97609</guid>
      <dc:creator>ANLYNG</dc:creator>
      <dc:date>2017-10-07T16:46:30Z</dc:date>
    </item>
    <item>
      <title>Re: Convert long string into variable EDIFACT</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-long-string-into-variable-EDIFACT/m-p/402104#M97613</link>
      <description>&lt;P&gt;Try SCAN() with the + and ' as your delimiters.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 07 Oct 2017 17:47:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-long-string-into-variable-EDIFACT/m-p/402104#M97613</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-10-07T17:47:42Z</dc:date>
    </item>
    <item>
      <title>Re: Convert long string into variable EDIFACT</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-long-string-into-variable-EDIFACT/m-p/402106#M97614</link>
      <description>&lt;P&gt;Looks to me like you can get very close by&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Removing the NAD+ from the input string.&lt;/LI&gt;
&lt;LI&gt;Splitting the string on single quote and period&lt;/LI&gt;
&lt;/OL&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test ;
  str=
   "NAD+PERSONnumber+3456789010'DTM+091:170126:101'"
||"NAD+ENTITY+027'NAD+KLA+11'"
||"NAD+YNR+'NAD+MOK+405'"
||"TAF+55:45:53:52:51:61:62:63:64:65:85:84:83:82:81:71:72:73:74:75'"
||"TOB+85:OKK:0'TOB+75:OKK:0.TOB+63:MES:4'QTY+DYB:X'"
  ;
  length i 8 term $100 ;
 * Remove NAD+ and then Split on ticks and periods ;
  str=compress(tranwrd(str,'NAD+',' '),' ');
  do i=1 by 1 until (term=' ');
    term=scan(str,i,"'.");
    output;
    put i= term= ;
  end;
  drop str;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;i=1 term=PERSONnumber+3456789010
i=2 term=DTM+091:170126:101
i=3 term=ENTITY+027
i=4 term=KLA+11
i=5 term=YNR+
i=6 term=MOK+405
i=7 term=TAF+55:45:53:52:51:61:62:63:64:65:85:84:83:82:81:71:72:73:74:75
i=8 term=TOB+85:OKK:0
i=9 term=TOB+75:OKK:0
i=10 term=TOB+63:MES:4
i=11 term=QTY+DYB:X
i=12 term=&lt;/PRE&gt;</description>
      <pubDate>Sat, 07 Oct 2017 18:15:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-long-string-into-variable-EDIFACT/m-p/402106#M97614</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-10-07T18:15:49Z</dc:date>
    </item>
    <item>
      <title>Re: Convert long string into variable EDIFACT</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-long-string-into-variable-EDIFACT/m-p/402114#M97617</link>
      <description>&lt;P&gt;if you values are pretty fixed here is the another approach. In first step &amp;nbsp;where we use prxchange what we do is&lt;/P&gt;
&lt;P&gt;we pick the pattern and extract what we want&lt;/P&gt;
&lt;P&gt;for example for variable person number as shown below code snippet&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PERSONnumber =prxchange('s/.+?PERSONnumber(.+?)''.+/$1/i', -1, str);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;for example for variable person number as&amp;nbsp;code shown above&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;(.+?) is the value we capture and contains anything after&amp;nbsp;personnumber &amp;nbsp;and&amp;nbsp;before first single quote.&lt;/P&gt;
&lt;P&gt;This captured value is&amp;nbsp;&amp;nbsp;$1 and replaces everything else. Same approach for every other variable. In the next step + is not replaced, when you have only +, otherwise + is removed. Hope I am clear in my explanation&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;PRE&gt;&lt;CODE class=" language-sas"&gt;data test ;
  str=
"NAD+PERSONnumber+3456789010'DTM+091:170126:101'NAD+ENTITY+027'NAD+KLA+11'
NAD+YNR+'NAD+MOK+405'TAF+55:45:53:52:51:61:62:63:64:65:85:84:83:82:81:71:72:73:74:75'
TOB+85:OKK:0'TOB+75:OKK:0'TOB+63:MES:4'QTY+DYB:X'"
  ;
  
  
  data test2(drop =str);
  set test;
  PERSONnumber =prxchange('s/.+?PERSONnumber(.+?)''.+/$1/i', -1, str);
  DTM =prxchange('s/.+?DTM(.+?)''.+/$1/i', -1, str);
  ENTITY=prxchange('s/.+?ENTITY(.+?)''.+/$1/i', -1, str);
  KLA=prxchange('s/.+?KLA(.+?)''.+/$1/i', -1, str);
  YNR=prxchange('s/.+?YNR(.+?)''.+/$1/i', -1, str);
  MOK=prxchange('s/.+?MOK(.+?)''.+/$1/i', -1, str);
  TAF=prxchange('s/.+?TAF(.+?)''.+/$1/i', -1, str);
  TOB85=prxchange('s/.+?TOB\+85\:(.+?)''.+/$1/i', -1, str);
  TOB75=prxchange('s/.+?TOB\+75\:(.+?)''.+/$1/i', -1, str);
  TOB63=prxchange('s/.+?TOB\+63\:(.+)/$1/i', -1, str);
  run;
  
  data test3;
  set test2;
 array vars{*} _character_;
 
   do i=1 to dim(vars);
     if vars{i} = '+' then vars{i} = vars{i};
     	else vars{i}  = substr(vars{i},2);
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 07 Oct 2017 20:23:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-long-string-into-variable-EDIFACT/m-p/402114#M97617</guid>
      <dc:creator>kiranv_</dc:creator>
      <dc:date>2017-10-07T20:23:49Z</dc:date>
    </item>
    <item>
      <title>Re: Convert long string into variable EDIFACT</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-long-string-into-variable-EDIFACT/m-p/402170#M97639</link>
      <description>&lt;P&gt;Ny values are NOT fixed. The styring and fontene is variable. So somehow i ned to find out hos many variables there is.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any smart Way of doping that?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I Will try the solutins latter. Looks vers interesting.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 08 Oct 2017 08:16:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-long-string-into-variable-EDIFACT/m-p/402170#M97639</guid>
      <dc:creator>ANLYNG</dc:creator>
      <dc:date>2017-10-08T08:16:48Z</dc:date>
    </item>
    <item>
      <title>Re: Convert long string into variable EDIFACT</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-long-string-into-variable-EDIFACT/m-p/402171#M97640</link>
      <description>&lt;P&gt;The fontene and nummer and nane of variables Will be different. So somehow i ned to use the test separators in the original styring to James the outputtet variables in ny final dataset.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 08 Oct 2017 08:20:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-long-string-into-variable-EDIFACT/m-p/402171#M97640</guid>
      <dc:creator>ANLYNG</dc:creator>
      <dc:date>2017-10-08T08:20:18Z</dc:date>
    </item>
    <item>
      <title>Re: Convert long string into variable EDIFACT</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-long-string-into-variable-EDIFACT/m-p/402243#M97660</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/26102"&gt;@ANLYNG&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Instead of trying to write your own parser you could also investigate the Internet if there is already something out there doing this job for you - and then spend the time to figure out how to interface with such a parser.&lt;/P&gt;
&lt;P&gt;Doing a quick Internet search it appears such parsers exists - ie. converting EDIFACT to XML. If so then you could use the SAS XMLV2 engine together with &lt;EM&gt;automap&amp;nbsp;&lt;/EM&gt;to then read such an XML file.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Oct 2017 01:07:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-long-string-into-variable-EDIFACT/m-p/402243#M97660</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-10-09T01:07:28Z</dc:date>
    </item>
    <item>
      <title>Re: Convert long string into variable EDIFACT</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-long-string-into-variable-EDIFACT/m-p/403310#M97999</link>
      <description>&lt;P&gt;I used this solution. This works very well. Thanks a lot for your input.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Oct 2017 18:49:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-long-string-into-variable-EDIFACT/m-p/403310#M97999</guid>
      <dc:creator>ANLYNG</dc:creator>
      <dc:date>2017-10-11T18:49:19Z</dc:date>
    </item>
    <item>
      <title>Re: Convert long string into variable EDIFACT</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-long-string-into-variable-EDIFACT/m-p/403313#M98000</link>
      <description>&lt;P&gt;I used this solution. This works very well. Thanks a lot for your input.&lt;/P&gt;</description>
      <pubDate>Wed, 11 Oct 2017 18:52:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-long-string-into-variable-EDIFACT/m-p/403313#M98000</guid>
      <dc:creator>ANLYNG</dc:creator>
      <dc:date>2017-10-11T18:52:17Z</dc:date>
    </item>
    <item>
      <title>Re: Convert long string into variable EDIFACT</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-long-string-into-variable-EDIFACT/m-p/403317#M98002</link>
      <description>I used this solution. This works very well. Thanks a lot for your input.</description>
      <pubDate>Wed, 11 Oct 2017 18:54:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-long-string-into-variable-EDIFACT/m-p/403317#M98002</guid>
      <dc:creator>ANLYNG</dc:creator>
      <dc:date>2017-10-11T18:54:56Z</dc:date>
    </item>
  </channel>
</rss>

