楼主: zespri
7210 17

[原创博文] 有没有方法把一个文件夹中的EXCEL都读入SAS [推广有奖]

11
lwien007 发表于 2009-11-25 13:51:13
filename dst pipe'dir e:\mysasfiles\excel/b';
%macro import(filename);
        proc import out=&filename
                datafile="e:\mysasfiles\excel\&filename..xls"
                dbms=excel replace;
        run;
%mend import;
data tmp;
        length fname $ 200;
        infile dst;
        input fname $ ;
        if scan(fname,-1,'.')^='xls' then delete;
        pe=prxparse('s/.xls//');
        call prxchange(pe,-1,fname);
        call execute('%import('||fname||')');
run;
这个也行

12
MyLoveSas 发表于 2009-11-25 15:14:45
回复楼主如下问题

8#

5# MyLoveSas
我也没能成功运行这程序, 加不加空格都一样?



代码有几个问题:
1、filename dirpipe pipe 'dir &dirpath.\*..&extfile./b';语句需要修改为"dir &dirpath*.&extfile./b";,因为只有使用双引号时,sas macro facility 才做宏替换;
2、PROC IMPORT过程中DBMS选项不能直接设置为文件扩展名。另外不同类型的源数据,对应的statement(如getnames、datarow、sheet等)均不相同;

13
zespri 发表于 2009-11-26 06:05:28
太感谢了, 谢谢帮助

14
第六根弦 发表于 2009-11-26 06:23:12

这是高难度啊

15
zespri 发表于 2009-11-26 09:11:09
11# lwien007

请问, 有没有方法, 先定变量的长度和类型吗?
因为Excel中同一变量, 有的是数字, 有的是文本, 比如
变量: 语言 - 有的是代码 19, 7 等, 有的直接 用English
日期 - 10022009 或 10/02/2009

我想把他们都做为文本导入, 再处理. 谢谢

16
MyLoveSas 发表于 2009-11-26 11:25:45
试试proc import的如下选项试试?

MIXED=YES | NO;
     converts numeric data values into character data values for a column that contains mixed data types. This option is valid only while importing data from Excel. The default is NO, which means that numeric data will be imported as missing values in a character column. If MIXED=YES, then the engine will assign a SAS character type for the column and convert all numeric data values to character data values. This option is valid only while reading (importing) data into SAS.

TEXTSIZE=1 to 32767
     specifies the field length that is allowed for importing Microsoft Excel 97, 2000, or 2002 Memo fields.

SCANTEXT=YES | NO;
     scans the length of text data for a data source column and uses the length of the longest string data that it finds as the SAS column width. However, if the maximum length that it finds is greater than what is specified in the TEXTSIZE= option, then the smaller value that is specified in TEXTSIZE= will be applied as the SAS variable width.
已有 1 人评分经验 论坛币 收起 理由
bakoll + 3 + 3 精彩帖子

总评分: 经验 + 3  论坛币 + 3   查看全部评分

17
zespri 发表于 2009-11-26 11:40:48
16# MyLoveSas


谢谢, 非常好

18
海底浮云 发表于 2013-10-19 21:53:36
若用infile的方法,效率将大大提升。我用import的方法,100多个文件读了一个晚上。后改成infile的方法,3分钟内搞定。
“独立精神、自由思想、求是态度”

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

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