Use Regular Expressions in Proc SQL


/ Published in: SAS
Save to your folder(s)

You can use regular expressions within SQL. This can be quite powerful in selecting data that matches certain conditions. The following example shows a simple regular expression which selects only quarterly periods from a table containing years, quarters & months.


Copy this code and paste it in your HTML
  1. data test ;
  2.  
  3. length period $ 7 ;
  4.  
  5. input period ;
  6.  
  7. cards ;
  8.  
  9. 2005
  10.  
  11. 2005Q1
  12.  
  13. 2005JAN
  14.  
  15. ;;
  16.  
  17. run ;
  18.  
  19. proc sql;
  20.  
  21. create table qtrs as
  22.  
  23. select *
  24.  
  25.   from test
  26.  
  27.   where prxmatch("/\d\d\d\d[qQ][1-4]/",period) ;
  28.  
  29. quit;
  30.  
  31. proc print data=qtrs ;
  32.  
  33. run ;

URL: http://jaredprins.squarespace.com/blog/2008/10/7/some-sas-code-snippets.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.