将值从字符转换为数字或从数字转换为字符

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将值从字符转换为数字或从数字转换为字符相关的知识,希望对你有一定的参考价值。

Original Source from http://support.sas.com/kb/24/590.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;

以上是关于将值从字符转换为数字或从数字转换为字符的主要内容,如果未能解决你的问题,请参考以下文章

无法将值从因子转换为仅数字

MongoDb:$elemMatch,将字符串转换为数字

在字典中,将值从字符串转换为整数

停止杰克逊读数字作为字符串

将值从字符串转换为流时出错

Vue 将 input[type=number] 转换为字符串值