楼主: LinYouthen
2432 8

[问答] 合并大量相同格式的txt文件并导入sas [推广有奖]

  • 1关注
  • 0粉丝

已卖:8份资源

副教授

27%

还不是VIP/贵宾

-

威望
0
论坛币
1678 个
通用积分
0
学术水平
0 点
热心指数
2 点
信用等级
1 点
经验
53994 点
帖子
58
精华
0
在线时间
1517 小时
注册时间
2010-8-29
最后登录
2024-8-27

楼主
LinYouthen 发表于 2013-10-14 01:07:03 |AI写论文
10论坛币
各位大神大牛,我有5个字段的学生数据,包括:学生姓名,学号,性别,入学日期,毕业日期。

可是有100个班的TXT文件,文件名分别是:1班,2班,3班...100班,


想要将他们合并,同时导入sas进行分析,求助各位大侠写个宏程序一次性批量导入,感激不尽啊

最佳答案

yongyitian 查看完整内容

The following code requires that the filenames has the form: class1.txt class2.txt ... class100.txt, and the variables are in the order: Name ID Gender Edate Bdate. If there is no variable names in the text file, the firstobs=1 should be used.
关键词:txt 各位大侠 SOSO 批量导入 感激不尽 程序 姓名
无忧无虑无烦恼

沙发
yongyitian 发表于 2013-10-14 01:07:04
The following code requires that the filenames has the form: class1.txt class2.txt ... class100.txt, and the variables are in the order: Name ID Gender Edate Bdate.
If there is no variable names in the text file, the firstobs=1 should be used.
  1. %macro InputMultiFiles(num);
  2.   %do i=1 %to #
  3.      data class;
  4.        infile "F:\MySAS\Temp\class&i..txt" firstobs=2;
  5.        input name $  ID Gender Edate : yymmdd10. Bdate : yymmdd10.;
  6.      run;
  7.     proc append base=ALL_Class data=class;
  8.     run;
  9.   %end;
  10. %mend InputMultiFiles;
  11. %InputMultiFiles(10);
复制代码
已有 2 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
LinYouthen + 1 + 1 + 1 好的意见建议
Imasasor + 60 + 80 + 4 + 3 鼓励积极发帖讨论

总评分: 经验 + 60  论坛币 + 80  学术水平 + 5  热心指数 + 4  信用等级 + 1   查看全部评分

藤椅
farmman60 发表于 2013-10-14 10:04:06
%macro merge(num);
  proc printto log=_null_;
  %do i=1 %to #
    proc import out=class&i datafile="c:\docs\class&i.txt" dbms=dlm replace;
    %if i=1 %then %do;
           data class_all;
            set class&i(obs=0);
           run;
        %end;
        proc append base=class_all data=class&i;
        proc datasets lib=work nolist;
    delete class&i;
        run;
  %end;
  proc printto;
  run;
%mend;

%merge(100)
已有 1 人评分学术水平 热心指数 收起 理由
LinYouthen + 1 + 1 观点有启发

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

板凳
妖帝东皇 发表于 2013-10-14 11:38:05
farmman60 发表于 2013-10-14 10:04
%macro merge(num);
  proc printto log=_null_;
  %do i=1 %to #
  1. %if i=1 %then
  2. %do;
  3. data class_all;
  4. set class&i(obs=0);
  5. run;
  6. %end;
复制代码
这句是干什么?还有obs=0起什么作用?
┏━━━━━━━━━━━━━━┓
   ☞❤学而无友必然孤陋寡闻!❤
┗━━━━━━━━━━━━━━┛

报纸
_i_ 发表于 2013-10-14 13:22:25
不同表内的数据需要flag吗?

地板
scarfacetony 发表于 2013-10-14 15:28:58
  1. data a;
  2.   infile 'C:\temp\*.txt';
复制代码
假设文件都在TEMP文件夹下
看了这个视频,我于是有了进股市的信心!
https://bbs.pinggu.org/thread-2787427-1-1.html

7
yongyitian 发表于 2013-10-14 23:31:36
_i_ 发表于 2013-10-14 13:22
不同表内的数据需要flag吗?
Good Question. The flag as a variable is necessary in most cases for analytical purpose and grouping. add
    class = "class&i";
before input statement should be ok.
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
LinYouthen + 1 + 1 + 1 分析的有道理

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

8
双修阁主の 发表于 2013-10-16 08:50:27
妖帝东皇 发表于 2013-10-14 11:38
这句是干什么?还有obs=0起什么作用?
同问~

9
believe448 在职认证  发表于 2013-10-16 10:44:29
围观学习~

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

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