We Recommend

Mastering Regular Expressions Mastering Regular Expressions
As this book shows, a command of regular expressions is an invaluable skill. Regular expressions allow you to code complex and subtle text processing that you never imagined could be automated. Regular expressions can save you time and aggravation. They can be used to craft elegant solutions to a wide range of problems. Once you've mastered regular expressions, they'll become an invaluable part of your toolkit. You will wonder how you ever got by without them.


Posted By

webonomic on 01/27/09


Tagged

delete character numeric keep


Versions (?)


Delete observations containing characters and keep numeric values only


Published in: SAS 


URL: http://jaredprins.squarespace.com/blog/2009/1/27/how-to-delete-observations-containing-characters.html

How to delete observations containing characters (e.g. 92z89 or abcd) and only keep the ones with numeric values.

  1. data mydata;
  2. input value $;
  3. cards;
  4. 1023442
  5. 92z89
  6. abcd
  7. 5231295
  8. 09CX42
  9. 9e122
  10. 12E3
  11. 98722
  12. ;
  13. run;
  14.  
  15. *Suggestion #1 - simple deleting;
  16.  
  17. *Keep only numeric values;
  18. data want1; set mydata;
  19. if anyalpha(value)>0 then delete;
  20. run;
  21.  
  22.  
  23. *Suggestion #2 - using PRXMATCH function;
  24. data want2; set mydata;
  25. if prxmatch("/[a-zA-Z]/",value)=0;
  26. /* Searches for a pattern match and returns the position at which the pattern is found */
  27. run;
  28.  
  29.  
  30. *Suggestion #3;
  31.  
  32. /*
  33. Do you want to delete the letters?
  34.  
  35. Also, it's not a good idea to just simply delete data without knowing the value. You may find you want to keep
  36. those observations. So why not try using the New Compress function in SAS9 or above and create a new variable?
  37. */
  38.  
  39. data want3a; set mydata;
  40. var = Compress (value, ,"Kd") ;
  41. *Kd is keep digits;
  42. run;
  43.  
  44. *SAS 8;
  45. data want3b; set mydata;
  46. var = Compress (value, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
  47. run;
  48.  
  49. *Suggestion #4;
  50. /* How about 12e3? It contains an alpha character but is nevertheless valid as
  51. the external representation of a numeric value?
  52. If so, you need to keep numbers stated in
  53. scientific notation, then try the following method:*/
  54.  
  55. %LET N_Errors = %SYSFUNC(GETOPTION(Errors));
  56. OPTIONS ERRORS=0;
  57. data want4; set mydata;
  58. if missing(input(value,best12.)) then delete;
  59. OPTIONS ERRORS=&N_Errors;
  60. run;

Report this snippet 

You need to login to post a comment.