title: Extracting a substring in SAS using regex
body: <p><strong>Problem</strong></p>
  
  <p>I need to extract a specific string from HTML using regex. The name of the string always follows the following pattern:</p>
  
  <p><code>&lt;2 digits&gt;&lt;any number of characters&gt;.zip</code></p>
  
  <p>I would like to do this in one step.</p>
  
  <p><strong>What I have</strong></p>
  
  <pre><code>data have;
      infile datalines truncover;
      input Line $ 1-500;
      datalines;
  "&lt;td&gt;&lt;a href=""Location/01data.zip""&gt;2001&lt;/td&gt;"
  "&lt;td&gt;&lt;a href=""Location/02moarstuff.zip""&gt;2002&lt;/td&gt;"
  ;
  run;
  </code></pre>
  
  <p><strong>What I need</strong></p>
  
  <p>The file's name and extension from the HTML code.</p>
  
  <pre><code>File               Line                                                   
  01data.zip         "&lt;td&gt;&lt;a href=""Location/01data.zip""&gt;2001&lt;/td&gt;"         
  02moarstuff.zip    "&lt;td&gt;&lt;a href=""Location/02moarstuff.zip""&gt;2002&lt;/td&gt;"    
  </code></pre>
  
  <p><strong>What I've tried</strong></p>
  
  <p>I've tried using the following regular expression:</p>
  
  <p><code>/\d+\w+(\.zip)/</code></p>
  
  <p>After testing with <a href="http://regexr.com/" rel="nofollow">http://regexr.com/</a> , the expression does find the right string. I then tried to use a technique found on <a href="http://www.pharmasug.org/proceedings/2013/CC/PharmaSUG-2013-CC35.pdf" rel="nofollow">page 3 of this SAS regex whitepaper</a> to remove everything except for the desired substring by using the <code>prxchange()</code> function:</p>
  
  <pre><code>data want;
      length File $25.;
      set have;
  
      file=prxchange('s/^.*\d+\w+(\.zip).*$/$1/',-1, line);
  run;
  </code></pre>
  
  <p>This will get me:</p>
  
  <pre><code>File    Line                                                   
  .zip    "&lt;td&gt;&lt;a href=""Location/01data.zip""&gt;2001&lt;/td&gt;"         
  .zip    "&lt;td&gt;&lt;a href=""Location/02moarstuff.zip""&gt;2002&lt;/td&gt;" 
  </code></pre>
  
  <p>It ends up replacing the string with <code>.zip</code>, but I am missing the file's name. I've tried different values of <code>$</code> in the replacement, but no success. </p>
  
  <p><strong>Question</strong></p>
  
  <p>What am I doing wrong with this regex replacement?</p>