<?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: Extract conditional information from text in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/Extract-conditional-information-from-text/m-p/301743#M8546</link>
    <description>&lt;P&gt;&lt;FONT face="times new roman,times" size="3"&gt;Dear&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9﻿&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser﻿&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick﻿&lt;/a&gt;.&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="times new roman,times" size="3"&gt;Thank you very much for your help. &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="times new roman,times" size="3"&gt;Yes, Patrick. PERL is indeed completely new to me but I will definately try to self-learn now.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="times new roman,times" size="3"&gt;Have an awesome weekend!&lt;/FONT&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 30 Sep 2016 11:53:01 GMT</pubDate>
    <dc:creator>Miracle</dc:creator>
    <dc:date>2016-09-30T11:53:01Z</dc:date>
    <item>
      <title>Extract conditional information from text</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Extract-conditional-information-from-text/m-p/301715#M8542</link>
      <description>&lt;P&gt;&lt;FONT face="times new roman,times" size="3"&gt;Hi everyone.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="times new roman,times" size="3"&gt;Can I please ask your help on this because I have racked my brain for hours to solve this but sadly I still can't figure out how&amp;nbsp;&lt;img id="smileysad" class="emoticon emoticon-smileysad" src="https://communities.sas.com/i/smilies/16x16_smiley-sad.png" alt="Smiley Sad" title="Smiley Sad" /&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="times new roman,times" size="3"&gt;Your help would&amp;nbsp;be very much appreciated&amp;nbsp;&lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://communities.sas.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;I wish to produce data 'Want' based on variable 'Comment' and 'ID' from data 'Have'.

&lt;U&gt;Data 'Have'&lt;/U&gt;
ID=1, Comment='qwertyuiopQ2asdfgh.Q35 jkl Q88zxcvbnm Q151: plmkjhq66 rfevs'
ID=2, Comment='Q98ertyu Q77: asdfgh.Q88zvbq q111: mjqq42 rvsq1plo'

&lt;U&gt;Data 'Want'&lt;/U&gt;
ID Question   Comment
1             qwertyuiop
1  Q2         asdfgh.
1  Q35        jkl 
1  Q88        zxcvbnm
1  Q151       plmkjh
1  Q66	      rfevs	
2  Q98        ertyu
2  Q77        asdfgh. 
2  Q88        zvbq
2  Q111       mjq
2  Q42	      rvs 
2  Q1	      plo&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 30 Sep 2016 08:00:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Extract-conditional-information-from-text/m-p/301715#M8542</guid>
      <dc:creator>Miracle</dc:creator>
      <dc:date>2016-09-30T08:00:44Z</dc:date>
    </item>
    <item>
      <title>Re: Extract conditional information from text</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Extract-conditional-information-from-text/m-p/301717#M8543</link>
      <description>&lt;P&gt;Well, the problem you are facing is that there doesn't appear to be any logical pattern to the comment. &amp;nbsp;For instance, you can get some way there by using scan, see example below, however Q isn't constant, it is sometimes, and that could also be elsewhere in the text. &amp;nbsp;Othertimes its not even present. &amp;nbsp;This is why most programmers will refuse to process comments data, you may be quicker just manually going through the data yourself.&lt;/P&gt;
&lt;PRE&gt;Data Have;
ID=1; Comment='qwertyuiopQ2asdfgh.Q35 jkl Q88zxcvbnm Q151: plmkjhq66 rfevs'; output;
ID=2; Comment='Q98ertyu Q77: asdfgh.Q88zvbq q111: mjqq42 rvsq1plo'; output;
run;

data want;
  set have;
  do i=1 to countw(comment,"Q");
    section=scan(comment,i,"Q");
    output;
  end;
run;
&lt;/PRE&gt;</description>
      <pubDate>Fri, 30 Sep 2016 08:38:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Extract-conditional-information-from-text/m-p/301717#M8543</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-09-30T08:38:33Z</dc:date>
    </item>
    <item>
      <title>Re: Extract conditional information from text</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Extract-conditional-information-from-text/m-p/301718#M8544</link>
      <description>&lt;P&gt;So you can use a capital Q as the separator?&lt;/P&gt;
&lt;P&gt;In the set statement of the data step, rename comment to oldcomment.&lt;/P&gt;
&lt;P&gt;Define a suitable length for comment&lt;/P&gt;
&lt;P&gt;For the first record, I'd set question to blank, and comment to scan(oldcomment,1,'Q')&lt;/P&gt;
&lt;P&gt;Then remove the first part by setting oldcomment to substr(oldcomment,index(oldcomment,'Q')+1)&lt;/P&gt;
&lt;P&gt;Then in do a while loop (check for the presence of Q in the condition):&lt;/P&gt;
&lt;P&gt;- scan for no-number characters at the beginning of the string with notdigit(); this lets you extract the question number&lt;/P&gt;
&lt;P&gt;- then, substr() everything after the number up to the next Q&lt;/P&gt;
&lt;P&gt;- output&lt;/P&gt;
&lt;P&gt;- do substr(oldcomment,index(oldcomment,'Q')+1) once again&lt;/P&gt;
&lt;P&gt;- after the loop has finished, process the remainder of oldcomment just like in the loop&lt;/P&gt;
&lt;P&gt;- keep only id, question and comment&lt;/P&gt;</description>
      <pubDate>Fri, 30 Sep 2016 11:56:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Extract-conditional-information-from-text/m-p/301718#M8544</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-09-30T11:56:18Z</dc:date>
    </item>
    <item>
      <title>Re: Extract conditional information from text</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Extract-conditional-information-from-text/m-p/301726#M8545</link>
      <description>&lt;P&gt;I couldn't come up with something simpler than in below code and it will eventually be a bit hard to digest in case you're not familiar with Regular Expressions (&amp;nbsp;&lt;A href="http://support.sas.com/resources/papers/proceedings16/2480-2016.pdf" target="_blank"&gt;http://support.sas.com/resources/papers/proceedings16/2480-2016.pdf&lt;/A&gt; )&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Have;
  ID=1;
  text='qwertyuiopQ2asdfgh.Q35 jkl Q88zxcvbnm Q151: plmkjhq66 rfevs';
  output;
  ID=2;
  text='Q98ertyu Q77: asdfgh.Q88zvbq q111: mjqq42 rvsq1plo';
  output;
  text='  ';
  output;
run;

data want(drop=_:);
  if _n_=1 then
    do;
      retain _prxid_q;
      _prxid_q=prxparse('/q\d+/oi');
    end;

  set have;

  if prxmatch('/^\s*q\d/i',text) ne 1 and lengthn(text) &amp;gt; 0 then _text='Q9999'||text;
  else _text=text;

  _start=1;
  _stop=length(_text);
  length question $5 answer $20;

  call prxnext(_prxid_q, _start, _stop, _text, _position, _length);
  do while (_position &amp;gt; 0);
    question = substr(_text, _position, _length);
    _start_answer=sum(_position,lengthn(question));

    call prxnext(_prxid_q, _start, _stop, _text, _position, _length);
    if _position&amp;gt;0 then
      do;
        answer=substrn(_text,_start_answer,_position-_start_answer);
      end;
      else
      do;
        answer=substrn(_text,_start_answer);
      end;
    output;
  end;

run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 30 Sep 2016 10:05:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Extract-conditional-information-from-text/m-p/301726#M8545</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2016-09-30T10:05:13Z</dc:date>
    </item>
    <item>
      <title>Re: Extract conditional information from text</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Extract-conditional-information-from-text/m-p/301743#M8546</link>
      <description>&lt;P&gt;&lt;FONT face="times new roman,times" size="3"&gt;Dear&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9﻿&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser﻿&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick﻿&lt;/a&gt;.&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="times new roman,times" size="3"&gt;Thank you very much for your help. &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="times new roman,times" size="3"&gt;Yes, Patrick. PERL is indeed completely new to me but I will definately try to self-learn now.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="times new roman,times" size="3"&gt;Have an awesome weekend!&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 30 Sep 2016 11:53:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Extract-conditional-information-from-text/m-p/301743#M8546</guid>
      <dc:creator>Miracle</dc:creator>
      <dc:date>2016-09-30T11:53:01Z</dc:date>
    </item>
    <item>
      <title>Re: Extract conditional information from text</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Extract-conditional-information-from-text/m-p/301746#M8547</link>
      <description>&lt;PRE&gt;
I love this question.


data Have;
  ID=1;
  text='qwertyuiopQ2asdfgh.Q35 jkl Q88zxcvbnm Q151: plmkjhq66 rfevs';
  output;
  ID=2;
  text='Q98ertyu Q77: asdfgh.Q88zvbq q111: mjqq42 rvsq1plo';
  output;
run;

data temp;
 set have;
 length temp $ 400;
 n=0;group=0;
 pid=prxparse('/q\d+:?/io');
 s=1;
 e=length(text);
 call prxnext(pid,s,e,text,p,l);
 do while(p gt 0);
 n+1;
  if n=1 then do;
    if p ne 1 then do;group+1;temp=' ';output;temp=substr(text,1,p-1);output;end;
    group+1;temp=compress(substr(text,p,l),':');output;
  end;
  else do;
   temp=substr(text,lag_p+lag_l,p-lag_p-lag_l);output;
   group+1;temp=compress(substr(text,p,l),':');output;
  end;
  
  lag_p=p;lag_l=l;
  call prxnext(pid,s,e,text,p,l);
 
  if p le 0 then do;temp=substr(text,lag_p+lag_l);output;end;
 
 end;
keep id temp group;
run;
proc transpose data=temp out=want;
 by id group;
 var temp;
run;
  

&lt;/PRE&gt;</description>
      <pubDate>Fri, 30 Sep 2016 12:16:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Extract-conditional-information-from-text/m-p/301746#M8547</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-09-30T12:16:34Z</dc:date>
    </item>
    <item>
      <title>Re: Extract conditional information from text</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Extract-conditional-information-from-text/m-p/301798#M8548</link>
      <description>&lt;P&gt;And a completely different approach:&lt;/P&gt;
&lt;P&gt;Go to who ever created that mess a see what options for other data formats there may have been. This appears to be exported from some sort of data entry or survey software and suspect some one was lazy or did not pay attention to options when extracting data.&lt;/P&gt;</description>
      <pubDate>Fri, 30 Sep 2016 16:18:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Extract-conditional-information-from-text/m-p/301798#M8548</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-09-30T16:18:39Z</dc:date>
    </item>
    <item>
      <title>Re: Extract conditional information from text</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Extract-conditional-information-from-text/m-p/301883#M8553</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw﻿&lt;/a&gt;, best advice of the thread. &amp;nbsp;In a proper process you could go back to the agreed upon Data Transfer Document, but it seems like nobody bothers with silliness such as documented, validated, repeatable procedures anymore. &amp;nbsp;Just dump any old garbage into Excel and jobs a good one.&lt;/P&gt;</description>
      <pubDate>Sat, 01 Oct 2016 10:37:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Extract-conditional-information-from-text/m-p/301883#M8553</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-10-01T10:37:42Z</dc:date>
    </item>
  </channel>
</rss>

