楼主: clarepan
12150 18

[问答] 用宏批量读入txt,之后如何只生成一个文件? [推广有奖]

11
soporaeternus 发表于 2010-12-3 22:10:12
我不是大神,我也是菜鸟一个......大神以后你自然会看到 *_*!!
我猜测了下楼主代码的想法,添加以下代码,其中宏%a可以根据你的需要来替换
希望对你有帮助
  1. options noxwait;
  2. x "dir d:\*.txt /b > d:\all";
  3. %macro a(n);
  4.         %put Just a Test for &n!;
  5. %mend a;
  6. data _null_;
  7.         infile "d:\all";
  8.         input str:$100.;
  9.         call execute(compress('%a('||scan(str,1,'.')||');'));
  10. run;
  11. x "del d:\all";
复制代码
Let them be hard, but never unjust

12
邢不行 在职认证  发表于 2010-12-6 09:47:11
11# soporaeternus
实在是谢谢大虾,顺利的帮我解决了我的问题!
虽然可以用了,问题也解决了。但是关于call execute语句还是有点不大懂,网上找不到关于他的资料,sas help又看不大懂,但是我一定会继续钻研的~

大虾好谦虚,我看到你帮好多人都解决了问题,每次回答之后还会加上 希望对你有帮助,好感动~~
再次感谢~

13
zkymath 在职认证  发表于 2011-8-12 15:18:57
助人为快乐之本

14
bobguy 发表于 2011-8-13 10:13:03
If all files are under one location, then use filevar option will be much mor efficient than a macro approach.

15
hongxx 发表于 2011-8-15 00:20:48
我不认为把很多“小”的dataset通过append合起来
会比一个set省多少开销
提供一个用infile的filevar选项的方法
很久没用了,貌似可以更简单点的......
options noxwait;

x "dir d:\*.txt /b > d:\filename";

data ccc;
        length file $300;
        infile "d:\filename";
        input file:$300.;
        file=compress("D:\"||file);
        infile aaa filevar=file end=eof;
        do while (not eof);
                input x;
                output;
        end;
run;

x "del d:\filename";
复制代码
      
         我稍微改一下,先把需要文件移到到临时创建的文件夹中,然后再DIR 命令加上选择 /s,这样就能读到文件的全名称(包括路径),避免在data步处理路径问题,而且能适应多个文件夹及其子文件的情况。

  1. options noxwait;
  2. x "md d:\temp";
  3. x "move d:\*.txt  d:\temp";

  4. filename filepath pipe  "dir d:\temp\*.txt /b /s";
  5.         
  6. data ccc;
  7.         infile filepath;
  8.         input file $300.;
  9.         infile aaa filevar=file end=eof;
  10.         do while (not eof);
  11.                 input x;
  12.                 output;
  13.         end;
  14. run;

  15. x "move d:\temp\*.* d:";
  16. x "rmdir d:\temp"
复制代码

16
baojiwolong 发表于 2011-11-30 17:26:03
hongxx 发表于 2011-8-15 00:20
我稍微改一下,先把需要文件移到到临时创建的文件夹中,然后再DIR 命令加上选择 /s,这 ...
really nice!!!!

  1. options noxwait;
  2. x "md d:\temp";
  3. x "move d:\*.txt d:\temp";

  4. filename filepath pipe "dir d:\temp\*.txt /b /s";

  5. data ccc;
  6. infile filepath;
  7. input file $300.;
  8. infile aaa filevar=file end=eof;
  9. do while (not eof);
  10. input x;
  11. output;
  12. end;
  13. run;

  14. x "move d:\temp\*.* d:";
  15. x "rmdir d:\temp"
复制代码

17
SASRETURN 发表于 2011-12-1 18:12:38
直接在DOS里copy:
copy/b file1.txt +file2.txt +file3.txt file4.txt
or:
copy/b *.txt newfile.txt

18
luckychll 发表于 2012-11-1 11:50:34
或者跟平常导入数据一样编写程序,只在导入路径更改将其改成*代替相似地方。例如批量导入E:city文件名为city20120801.txt city20120802.txt……city20121030.txt,这里的city不同、日期不同,年份相同,可将路径写成    INFILE 'E:\city\*2012*.txt'

19
geokaran 发表于 2013-3-14 00:19:00
good

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

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