楼主: dxystata
4020 14

如何把数据集中的值替换并输入到txt文件中 [推广有奖]

11
dxystata 发表于 2014-1-24 00:19:16
123.txt (13 Bytes)

还是有bug,结果中abc还存在!

12
dxystata 发表于 2014-1-24 00:20:33
yongyitian 发表于 2014-1-23 23:43
要把 long_string1.txt 中的每一行联在一起,再替换 'abc' 是这样的。
不一定是你想要的结果。
麻烦用我刚上传的123.txt试试,谢谢!

13
yongyitian 发表于 2014-1-24 09:52:48
dxystata 发表于 2014-1-24 00:20
麻烦用我刚上传的123.txt试试,谢谢!
p = find(x, 'abc');  语句将找到的第一个abc的位置赋值给p。
下面的条件语句包括了p=1的情况。但不能替换同一个字符串中的多个abc。
如果需要替换一个字符串中的多个abc,可以将替换过的 123.txt 作为原123.txt重新运行这段程序。
  1. data aaa;
  2.      infile  "F:\MySAS\Temp\output\123.txt" LRECL=32767;
  3.         length x $11000.;
  4.      input x :&$;
  5. run;
  6. proc print data=aaa; title 'aaa'; run; title;

  7. data _null_;
  8.     infile  "F:\MySAS\Temp\output\long_string1.txt" LRECL=32767  end=last;
  9.       file  "F:\MySAS\Temp\output\1234567.txt"      LRECL=32767;
  10.     length x y temp $22000.;
  11.         retain temp;
  12.     do until(last);
  13.         input;
  14.         call cats(temp, _infile_, '___');   
  15.         end;
  16.     do until(last1);
  17.         set aaa end=last1;
  18.                   p = find(x, 'abc');
  19.                if p = 1 then y= cats(temp, substr(x, p+3));
  20.           else if p > 1 then y= cats(substr(x, 1, p-1), temp, substr(x, p+3));
  21.              else y = x;
  22.     put @1 y;
  23.     end;
  24. run;
复制代码

14
dxystata 发表于 2014-1-24 10:41:40
yongyitian 发表于 2014-1-24 09:52
p = find(x, 'abc');  语句将找到的第一个abc的位置赋值给p。
下面的条件语句包括了p=1的情况。但不能 ...
我上传的新123.txt文件中就含一个abc啊

15
farmman60 发表于 2014-1-25 11:15:36
dxystata 发表于 2014-1-23 22:40
如果是long_string1.txt,如何把abc替换为long_string1.txt的内容呢?谢谢!
options noquotelenmax;
data _null_;
   file "c:\temp.txt" ;
   infile "c:\longstring.txt"  truncover firstobs=1 end=last1;
   length temp$11000.;
   retain temp;
   do until (last1);
     input x $11000.;
         temp=cats(temp,_infile_);
   if last1 then call symput("string",temp);
   end;
   infile "c:\123.txt" truncover firstobs=1 end=last2;
   length _xyz $11000.;
     do until (last2);
           input xyz $11000.;
           if index(_infile_,'abc')^=0 then do;
             if findc(_infile_,'x','b')>3 then _xyz=cats(scan(_infile_,1,'abc'),resolve('&string'),scan(_infile_,-1,'abc'));
                 else _xyz=cats(scan(_infile_,1,'abc'),resolve('&string'));
           end;
           else _xyz=xyz;            
           put _xyz $;
         end;
run;
已有 1 人评分经验 学术水平 热心指数 收起 理由
dxystata + 100 + 1 + 1 鼓励积极发帖讨论

总评分: 经验 + 100  学术水平 + 1  热心指数 + 1   查看全部评分

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-31 08:27