Convert values from character to numeric or from numeric to character


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

Original Source from http://support.sas.com/kb/24/590.html


Copy this code and paste it in your HTML
  1. /* Convert a character value to a numeric value by using the INPUT */
  2. /* function. Specify a numeric informat that best describes how to */
  3. /* read the data value into the numeric variable. */
  4. /* */
  5. /* When changing types a new variable name is required. If you need */
  6. /* to keep the original variable name for the new type, use the */
  7. /* RENAME= option as illustrated in Sample 2. */
  8.  
  9.  
  10. data char;
  11. input string :$8. date :$6.;
  12. numeric=input(string,8.);
  13. sasdate=input(date,mmddyy6.);
  14. format sasdate mmddyy10.;
  15. datalines;
  16. 1234.56 031704
  17. 3920 123104
  18. ;
  19.  
  20. proc print;
  21. run;
  22.  
  23.  
  24. /* Convert a numeric value to a character value by using the PUT */
  25. /* function. Specify a numeric format that describes how to write */
  26. /* the numeric value to the character variable. To left align */
  27. /* the resulting character value, specify -L after the format */
  28. /* specification. */
  29. /* */
  30. /* When changing types a new variable name is required. If you need */
  31. /* to keep the original variable name, use the RENAME= option on */
  32. /* the SET statement to rename the variable as it comes into the PDV. */
  33. /* This allows the original variable name to be reused when you */
  34. /* change type. */
  35.  
  36.  
  37.  
  38. data now_num;
  39. input num date: mmddyy6.;
  40. datalines;
  41. 123456 110204
  42. 1000 120504
  43. ;
  44.  
  45. data now_char;
  46. set now_num (rename=(num=oldnum date=olddate));
  47. num=put(oldnum,6. -L);
  48. date=put(olddate,date9.);
  49. run;
  50.  
  51. proc print;
  52. run;

URL: http://jaredprins.squarespace.com/blog/2008/12/12/convert-from-character-to-numberic-and-back-using-sas.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.