Delete observations containing characters and keep numeric values only


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

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


Copy this code and paste it in your HTML
  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;

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.