楼主: dxystata
4018 14

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

版主

已卖:302份资源

大师

37%

还不是VIP/贵宾

-

TA的文库  其他...

Software

中英文Ebook

R学习

威望
2
论坛币
183395 个
通用积分
15333.1475
学术水平
208 点
热心指数
271 点
信用等级
174 点
经验
298627 点
帖子
5586
精华
1
在线时间
13632 小时
注册时间
2006-6-21
最后登录
2025-12-22

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

楼主
dxystata 发表于 2014-1-22 09:55:11 |AI写论文
200论坛币

  1. data aaa;
  2. input x$ y$ z$;
  3. cards;
  4. a b abc
  5. d abc f
  6. ;
  7. run;
复制代码
2014-01-22_093601.png

我想把数据集输出到txt文件中,但我想把abc替换成10000个字符长度的值输出到txt文件中,有什么好方法实现吗?谢谢!


最佳答案

yongyitian 查看完整内容

the macro variable temp created by call symput('temp', _infile_); can not be used (by &temp) in the same data step. It can be referenced after the data step have finished. Don't know how to remove the warning. But the file 1234.txt was created correctly with maximum length of 10004. There is not warning this way.
关键词:txt 数据集 cards 数据集输出 Input 如何

本帖被以下文库推荐

沙发
yongyitian 发表于 2014-1-22 09:55:12
dxystata 发表于 2014-1-23 11:54
data aaa;
infile  "F:\123.txt" LRECL=32767;
length x $11000.;
the macro variable temp created by call symput('temp', _infile_);
can not be used (by &temp) in the same data step.

It can be referenced after the data step have finished.

Don't know how to remove the warning. But the file 1234.txt was created correctly with maximum length of 10004.

There is not warning this way.
  1. data _null_;
  2. infile "F:\MySAS\Temp\output\long_string.txt" LRECL=32767;
  3. file "F:\MySAS\Temp\output\12345.txt" LRECL=32767;
  4. length x y temp $11000.;
  5. retain temp;
  6. if _n_ = 1 then do;
  7. input;
  8. temp = _infile_;
  9. end;
  10. set aaa;
  11. p = find(x, 'abc');
  12. if find(x, 'abc') then y= cats(substr(x, 1, p-1), temp, substr(x, p+3));
  13. else y = x;
  14. put @1 y;
  15. run;
复制代码

已有 2 人评分经验 学术水平 热心指数 收起 理由
dxystata + 100 + 2 + 5 好的意见建议
jingju11 + 1 写得真好。登山生层云。。。

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

藤椅
dxystata 发表于 2014-1-22 11:13:04
先put到txt文件中,然后再对abc替换!这样在SAS中能实现吗?

板凳
yongyitian 发表于 2014-1-23 09:34:47
  1. /* create a text file for the dataset */
  2. data aaa;
  3. file  "F:\MySAS\Temp\Short_AAA.txt"; /* create a text file for dataset aaa */
  4. input x$ y$ z$;
  5. put @1 x  @10 y @20 z;  
  6. cards;
  7. a b abc
  8. d abc f
  9. ;
  10. run;

  11. /* create a long_string text file and a macro for replacing 'abc' */
  12. data _null_;   
  13.    file  "F:\MySAS\Temp\long_string.txt" LRECL=32767; /* create a text file */
  14.    length temp $11000.;                           
  15.     do i= 1 to 1000;
  16.       temp = cats(temp, 'A', '123456789');
  17.     end;
  18.    call symput('temp', temp);     /* create a macro variable */
  19.    put @1 temp;
  20. run;

  21. /* method 1: using two infiles statement for input
  22.                and one    file statement for output */

  23. data _null_;
  24.   infile  "F:\MySAS\Temp\long_string.txt" LRECL=32767;
  25.     file  "F:\MySAS\Temp\long_aaa_1.txt"  LRECL=32767;
  26.    length x y z $11000.;
  27.    array xyz  $ x y z;
  28.     retain temp;
  29.     if _n_ = 1 then do;
  30.         input;
  31.         temp = _infile_;
  32.     end;
  33.     do until(last);
  34.        infile  "F:\MySAS\Temp\short_aaa.txt"  LRECL=32767 end=last;
  35.          input x : $ y :$ z :$;
  36.       do over xyz;
  37.         if xyz = 'abc' then xyz = temp;
  38.       end;
  39.       put @1 x=  +5 y=  +5 z=;  /* = sign is just for illustrating, and can be removed */
  40.     end;
  41. run;

  42. /* method 2: using one infile statement and a dataset for input
  43.                       and one file statement for output */
  44. data _null_;
  45.   infile  "F:\MySAS\Temp\long_string.txt" LRECL=32767;
  46.     file  "F:\MySAS\Temp\Long_aaa_2.txt" LRECL=32767;
  47.     length x y z $11000.;
  48.     array xyz  $ x y z;
  49.     retain temp;
  50.     if _n_ = 1 then do;
  51.         input;
  52.         temp = _infile_;
  53.     end;
  54.     set aaa;
  55.        do over xyz;
  56.           if xyz = 'abc' then xyz = temp;
  57.        end;
  58.     put @1 x=  +5 y=  +5 z=;    /* = sign just for illustrating, and can be removed */
  59. run;
复制代码

报纸
dxystata 发表于 2014-1-23 10:21:35
yongyitian 发表于 2014-1-23 09:34
123.txt (20 Bytes) long_string.txt (9.77 KB)


先把123.txt读入SAS
data aaa;
infile  "F:\123.txt" LRECL=32767;
length x $11000.;
input x:$;
run;


然后把数据集aaa中变量x中含有abc 替换成long_string.txt中内容?



最后得到123.txt(文件名不变)

地板
yongyitian 发表于 2014-1-23 11:12:35
dxystata 发表于 2014-1-23 10:21
先把123.txt读入SAS
data aaa;
infile  "F:\123.txt" LRECL=32767;
try this
  1. data _null_;
  2.   infile  "F:\MySAS\Temp\output\long_string.txt" LRECL=32767;
  3.   input;
  4.   call symput('temp', _infile_);
  5. run;
  6. %put &temp;

  7. data aaa;
  8.      infile  "F:\MySAS\Temp\output\123.txt" LRECL=32767;
  9.      length x $11000.;
  10.      input x :&$;
  11. run;

  12. data bbb;
  13.    file  "F:\MySAS\Temp\output\1234.txt" LRECL=32767;
  14.    length x $11000.;
  15.    set aaa;
  16.    if find(x, 'abc') then x = tranwrd(x, 'abc', "&temp");
  17.    put @1 x;
  18. run;
复制代码
已有 1 人评分学术水平 热心指数 收起 理由
wdxiaochun + 1 + 1 精彩帖子

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

7
dxystata 发表于 2014-1-23 11:46:02
yongyitian 发表于 2014-1-23 11:12
try this
程序出现WARNING: The quoted string currently being processed has become more than 262 bytes long.  You
         might have unbalanced quotation marks.

8
dxystata 发表于 2014-1-23 11:54:23
yongyitian 发表于 2014-1-23 11:12
try this
data aaa;
infile  "F:\123.txt" LRECL=32767;
length x $11000.;
input x:$;
run;

data _null_;
        infile  "F:\long_string.txt" LRECL=32767;
    file  "F:\124.txt" LRECL=32767;
        length x $11000.;
        input;
        call symput('temp', _infile_);
        %put &temp;
    set aaa;
    x=tranwrd(x,'abc',"&temp");
        put x;
run;
这样运行为什么会得不到结果呢!

9
dxystata 发表于 2014-1-23 22:40:45
yongyitian 发表于 2014-1-22 09:55
the macro variable temp created by call symput('temp', _infile_);
can not be used (by &temp) in ...
long_string1.txt (10.3 KB)

如果是long_string1.txt,如何把abc替换为long_string1.txt的内容呢?谢谢!

10
yongyitian 发表于 2014-1-23 23:43:38
dxystata 发表于 2014-1-23 22:40
如果是long_string1.txt,如何把abc替换为long_string1.txt的内容呢?谢谢!
要把 long_string1.txt 中的每一行联在一起,再替换 'abc' 是这样的。
不一定是你想要的结果。
  1. data aaa;
  2.      infile  "F:\MySAS\Temp\output\123.txt" LRECL=32767;
  3.          length x $11000.;
  4.      input x :&$;
  5. run;

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

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

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