楼主: dxystata
4256 3

[问答] 如何把数据集中的数值型全部变为字符型 [推广有奖]

版主

大师

34%

还不是VIP/贵宾

-

TA的文库  其他...

Software

中英文Ebook

R学习

威望
2
论坛币
182600 个
通用积分
15207.3903
学术水平
208 点
热心指数
271 点
信用等级
174 点
经验
291628 点
帖子
5382
精华
1
在线时间
13485 小时
注册时间
2006-6-21
最后登录
2024-5-3

初级学术勋章 初级热心勋章 中级热心勋章 初级信用勋章

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
数据集中有字符型 也由数值型
要求:1.变量名不变 2.变量顺序不变。
如何实现,谢谢!


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:数据集 字符型 数值型 如何实现 如何

本帖被以下文库推荐

沙发
yugao1986 发表于 2011-10-11 17:30:58 |只看作者 |坛友微信交流群
sas.com里面有例子,把所有字符型改为数值型,作下对调:
  1. /* How to convert all numeric variables to character and use the same variable names in the output data set
  2. The sample data set TEST contains both character and numeric variables*/
  3. data test;
  4. input id $ b c $ d e $ f;
  5. datalines;
  6. AAA 50 11 1 222 22
  7. BBB 35 12 2 250 25
  8. CCC 75 13 3 990 99
  9. ;
  10. /*PROC CONTENTS is used to create an output data set called VARS to list all */
  11. /*variable names and their type from the TEST data set. */

  12. proc contents data=test out=vars(keep=name type) noprint;

  13. /*A DATA step is used to subset the VARS data set to keep only the numeric */
  14. /*variables and exclude the one ID numeric variable. A new list of character*/
  15. /*variable names is created from the numeric variable name with a "_n" */
  16. /*appended to the end of each name. */

  17. data vars;
  18. set vars;
  19. if type=1 and name ne 'id';
  20. newname=trim(left(name))||"_n";

  21. /*The macro system option SYMBOLGEN is set to be able to see what the macro*/
  22. /*variables resolved to in the SAS log. */

  23. options symbolgen;

  24. /*PROC SQL is used to create three macro variables with the INTO clause. One */
  25. /*macro variable named c_list will contain a list of each character variable */
  26. /*separated by a blank space. The next macro variable named n_list will */
  27. /*contain a list of each new numeric variable separated by a blank space. The */
  28. /*last macro variable named renam_list will contain a list of each new numeric */
  29. /*variable and each character variable separated by an equal sign to be used on*/
  30. /*the RENAME statement. */

  31. proc sql noprint;
  32. select trim(left(name)), trim(left(newname)),
  33. trim(left(newname))||'='||trim(left(name))
  34. into :c_list separated by ' ', :n_list separated by ' ',
  35. :renam_list separated by ' '
  36. from vars;
  37. quit;

  38. /*The DATA step is used to convert the numeric values to character. An ARRAY */
  39. /*statement is used for the list of character variables and another ARRAY for */
  40. /*the list of numeric variables. A DO loop is used to process each variable */
  41. /*to convert the value from character to numeric with the INPUT function. The */
  42. /*DROP statement is used to prevent the character variables from being written */
  43. /*to the output data set, and the RENAME statement is used to rename the new */
  44. /*numeric variable names back to the original character variable names. */

  45. data test2;
  46. set test;
  47. array ch(*) $ &c_list;
  48. array nu(*) &n_list;
  49. do i = 1 to dim(nu);
  50. ch(i)=put(nu(i),8.);
  51. end;
  52. drop i &c_list;
  53. rename &renam_list;
  54. run;
复制代码

已有 2 人评分学术水平 热心指数 收起 理由
Bridgenc + 1 + 1 精彩帖子
dxystata + 1 + 2 好的意见建议

总评分: 学术水平 + 2  热心指数 + 3   查看全部评分

三人行必有我师

使用道具

藤椅
wangyf0218 发表于 2011-10-11 17:50:32 |只看作者 |坛友微信交流群
好吧,我怎么能用那么低效率的方法。。。
继续学习

使用道具

板凳
hs4601 发表于 2013-12-15 17:21:49 |只看作者 |坛友微信交流群

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-5-3 09:39