Posted By

webonomic on 03/31/08


Tagged

parse String regular expressions prxmatch


Versions (?)

String Matching


 / Published in: SAS
 

URL: http://jaredprins.squarespace.com/blog/2008/3/31/string-matching.html

  1. /*pull out all comments with specific words*/
  2.  
  3. data commentsdata;set surveydata;
  4.  
  5. if _n_=1 then do;
  6. retain re;
  7. re = prxparse(’/words|to|match/i’); /* the /i here means Case insensitive match */
  8. if missing(re) then do;
  9. putlog ‘ERROR: regex is malformed’;
  10. stop;
  11. end;
  12. end;
  13. if prxmatch(re,Comments); /* Comments is the name of the variable with the comments text */
  14. run;
  15.  
  16.  
  17. /*Regular Expressions*/
  18.  
  19. /*
  20. ^ start of field
  21. s* (maybe with whitespace at the front)
  22. [A-Z] a letter from A to Z
  23. d a digit
  24. [A-Z] A to Z again
  25. a space
  26. d a digit
  27. [A-Z] A to Z again
  28. d a digit
  29. s* possible whitespace
  30. $ end of field
  31. */
  32.  
  33. data new;
  34. set YourData;
  35. if _n_=1 then do;
  36. re = prxparse(’/^s*[A-Z]d[A-Z] d[A-Z]ds*$/’);
  37. if missing (re) then stop;
  38. end;
  39.  
  40. if prxmatch(re,YourNameField);
  41. run;

Report this snippet  

You need to login to post a comment.