楼主: dxystata
2712 10

[问答] 50论坛币币求助多个SAS程序文件中的内容替换 [推广有奖]

版主

已卖: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 发表于 2022-3-25 10:50:08 |AI写论文
50论坛币
我想将多个SAS程序文件中的mdy(12,31,2021) 替换成 mdy(06,30,2022),如何编程实现?谢谢!

关键词:sas程序 50论坛币 0论坛币 论坛币

沙发
whymath 发表于 2022-3-25 10:50:09
  1. /*
  2. 简介:当前宏将类文本文件读取为SAS数据集,然后对其执行任意合法的SAS操作. 宏共包含5个参数:
  3. 参数说明:
  4.   filein        指定输入文件, 允许使用SAS fileref
  5.   fileout       指定输出文件, 允许使用SAS fileref, 默认为[&filein.]
  6.   inencoding    输入文件的编码值, 默认为[%sysfunc(compress(&sysencoding.,-))]
  7.   outencoding   输出文件的编码值, 默认为[&inencoding.]
  8.   process       要对输入文件执行的合法SAS操作
  9. 其它说明:
  10.   宏内部对输入文件转换的数据集命名为_file_, 输入文件的内容存储在变量text中.
  11. 用法示例:
  12.   %*对输入文件进行编码转换;
  13.   %file_anyprocess(filein=D:\Download\没离开过_林志炫.lrc,inencoding=utf8,outencoding=euccn);

  14.   %*对输入文件进行文本替换;
  15.   %file_anyprocess(filein=D:\Download\没离开过_林志炫.lrc,process=%str(
  16.     text=tranwrd(text,'没离开','没换气')));

  17.   %*对输入文件增加行号;
  18.   %file_anyprocess(filein=D:\Download\没离开过_林志炫.lrc,process=%str(
  19.     text=put(_n_,z3.)||' '||text));

  20.   %*完全替换输入文件的内容;
  21.   %file_anyprocess(filein=D:\Download\没离开过_林志炫.lrc,process=%str(
  22.     data _file_;set sashelp.class;text=cats(name,',',sex);run;));
  23. */

  24. %macro file_anyprocess(filein=,fileout=&filein.,inencoding=%sysfunc(compress(&sysencoding.,-)),outencoding=&inencoding.,process=);
  25. data _file_;
  26.   infile %sysfunc(quote(%superq(filein))) lrecl=32767 truncover encoding=&inencoding.;
  27.   input text $char32767.;
  28.   %unquote(&process.);
  29. run;

  30. data _null_;
  31.   file %sysfunc(quote(%superq(fileout))) encoding=&outencoding.;
  32.   set _file_;
  33.   _len_=lengthn(text);
  34.   put text $varying. _len_;
  35. run;
  36. %mend;
复制代码

已有 1 人评分经验 学术水平 热心指数 信用等级 收起 理由
dxystata + 100 + 1 + 1 + 1 热心帮助其他会员

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

藤椅
乐天天12300 发表于 2022-3-25 18:57:59
data test;
    /*读取程序*/
    infile "C:\Users\EDY\Desktop\tab9_3_1_5 to 9_3_1_6.sas" lrecl=32767 recfm=f truncover;
    input a $32767.;
    /*修改程序,如将a全部替换为b*/
    a=tranwrd(a,"a","b");
    /*输出修改后的文件*/
    file "C:\Users\EDY\Desktop\test.sas";
    put a;
run;
这是修改原理。批量的话,可以遍历一个文件下的所有SAS程序文件,然后用循环
已有 1 人评分经验 学术水平 热心指数 信用等级 收起 理由
dxystata + 100 + 1 + 1 + 1 热心帮助其他会员

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

板凳
whymath 发表于 2022-3-25 19:10:38
标题是50币你就发1个?太不厚道啦

报纸
dxystata 发表于 2022-3-26 14:04:30
whymath 发表于 2022-3-25 19:10
标题是50币你就发1个?太不厚道啦
随便上传个附件,我来购买呢,谢谢!

地板
dxystata 发表于 2022-3-26 14:05:16
乐天天12300 发表于 2022-3-25 18:57
data test;
    /*读取程序*/
    infile "C:\Users\EDY\Desktop\tab9_3_1_5 to 9_3_1_6.sas" lrecl=327 ...
随便上传个附件,我来购买呢,谢谢!

7
dxystata 发表于 2022-3-26 14:05:42
whymath 发表于 2022-3-25 19:10
标题是50币你就发1个?太不厚道啦
我修改啦,抱歉!

8
whymath 发表于 2022-3-26 15:31:13
dxystata 发表于 2022-3-26 14:05
我修改啦,抱歉!
好的,下次记得多给点啊,你可是有17W币的大财主

9
whymath 发表于 2022-3-26 15:33:05
遍历文件列表执行这个宏就可以批量完成了
已有 1 人评分经验 学术水平 热心指数 信用等级 收起 理由
dxystata + 200 + 2 + 2 + 2 热心帮助其他会员

总评分: 经验 + 200  学术水平 + 2  热心指数 + 2  信用等级 + 2   查看全部评分

10
dxystata 发表于 2022-3-28 09:43:52
@乐天天12300  @whymath
请以出售的形式随便上传个附件,我来购买,谢谢!

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

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