楼主: priss111
3082 15

[原创博文] 请问如何将字符之间的非连续的‘||’去掉?谢谢! [推广有奖]

11
shenliang_111 发表于 2011-9-7 13:14:42
  1. /*分列*/
  2. data aa;
  3. input @;
  4. _infile_=tranwrd(_infile_,"||"," ");
  5. infile datalines truncover;
  6. input x y z zz zzz;
  7. datalines;
  8. 2||3||4
  9. 2||3
  10. 2||3
  11. 2||3||4||5||7
  12. 2||3||4||5
  13. 2||3
  14. 2||3||4
  15. 2||3
  16. 2||3||4||5
  17. 2||3||4
  18. ;
  19. run;
  20. /*一个变量*/
  21. data aaa;
  22. input @;
  23. _infile_=tranwrd(_infile_,"||"," ");
  24. input x $ 1-20;
  25. datalines;
  26. 2||3||4
  27. 2||3
  28. 2||3
  29. 2||3||4||5||7
  30. 2||3||4||5
  31. 2||3
  32. 2||3||4
  33. 2||3
  34. 2||3||4||5
  35. 2||3||4
  36. ;
  37. run;
复制代码

12
priss111 发表于 2011-9-7 14:20:39
shenliang_111 发表于 2011-9-7 13:14
谢谢!!

再请教一下,
对于‘分列’,
如果原始数据中已经生成了数据库,
不需要重新cards了,
那么如何直接从原始库将该变量生成‘分列的’数据集?


data a;
set yuanshi;
a=tranwrd(b,'||',' ')
...

13
priss111 发表于 2011-9-7 14:20:50
shenliang_111 发表于 2011-9-7 13:14
谢谢!!

再请教一下,
对于‘分列’,
如果原始数据中已经生成了数据库,
不需要重新cards了,
那么如何直接从原始库将该变量生成‘分列的’数据集?


data a;
set yuanshi;
a=tranwrd(b,'||',' ')
...

14
shenliang_111 发表于 2011-9-7 15:14:04
  1. /*导入数据*/
  2. data neww;
  3. input ss $ 1-20;
  4. cards;
  5. 2||3||4
  6. 2||3
  7. 2||3
  8. 2||3||4||5||7
  9. 2||3||4||5
  10. 2||3
  11. 2||3||4
  12. 2||3
  13. 2||3||4||5
  14. 2||3||4
  15. ;
  16. run;
  17. /*实行分列*/
  18. data _null_;
  19. retain max_cnt 0;
  20. set neww end=last;
  21. cnt=count(ss,'||');
  22. max_cnt=max(max_cnt,cnt);
  23. if last then call symputx('nn',put(max_cnt,best.-l));
  24. run;
  25. %put &nn;
  26. /*确定分列后的变量个数*/
  27. %let cnt=%eval(&nn+1);
  28. /*开始分列*/
  29. data result;
  30. set neww;
  31. array x(&cnt.);
  32. do i=1 to &cnt.;
  33.   x(i)=scan(ss,i,'||');
  34. end;
  35. drop i;
  36. run;
复制代码

15
priss111 发表于 2011-9-7 15:21:07
shenliang_111 发表于 2011-9-7 15:14
谢谢!!

学习中...

16
priss111 发表于 2011-9-7 15:31:04
shenliang_111 发表于 2011-9-7 15:14
谢谢!

针对该程序,
请教2个问题:
1、retain 后面的0表示什么意思?
2、symputx括号里的’best.-1'是不是可以省略?
这样,
%let cnt=%eval(&nn)了.

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-20 02:23