楼主: victorwxw
3622 13

求助高手指点怎么读取文档下的文件名字 [推广有奖]

  • 19关注
  • 1粉丝

已卖:2份资源

博士生

27%

还不是VIP/贵宾

-

威望
0
论坛币
21 个
通用积分
29.5885
学术水平
1 点
热心指数
1 点
信用等级
1 点
经验
4913 点
帖子
163
精华
0
在线时间
374 小时
注册时间
2007-6-17
最后登录
2024-11-18

楼主
victorwxw 发表于 2013-6-29 13:53:19 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
我的一段SAS程序主要目的是想获取一个文档下的所有excel文件的名字,程序如下:
options noxwait;
%let path=C:\Documents and Settings\ibm\My Documents\My SAS
Files\9.2\li0oi3pj1\;
x "dir &path.*.xls /b>&path.all.txt";

运行结果是:all.txt文件中什么也没有。

后面再加一段程序:
data _null_;
        infile "&path.all.txt";
        input str:$100.;
            put str;
run;

运行结果如下:
ERROR: 物理文件不存在,C:\Documents and Settings\ibm\My Documents\My SAS  Files\9.2\li0oi3pj1\all.txt
请问有高手能帮忙指点下,这是什么原因吗?有谁有能读取文件名字的程序吗?非常谢谢


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:求助高手 高手指点 文件名 documents Document Documents excel ERROR null 程序

本帖被以下文库推荐

沙发
leptonyu 发表于 2013-6-29 14:18:05
使用dopen函数吧

filename dir "C:\Documents and Settings\ibm\My Documents\My SAS
Files\9.2\li0oi3pj1\";
data _null_;
    did=dopen('dir');
if did then do;
   for i=1 to dnum(did);
     file=dread(did,i);
   put file=;
  end; rc=dclose(did);
end;
run;

藤椅
victorwxw 发表于 2013-6-29 15:02:30
非常谢谢您的回复,不过你的程序我在电脑上运行结果出现错误。我把您的程序改动下,如下:
filename dir "C:\Documents and Settings\ibm\My Documents\My SAS
Files\9.2\li0oi3pj1\";
data fname;
length name $50;
did=dopen('dir');       
if did>0 then  do;
num=dnum(did);
do i=1 to num;
     name=dread(did,i);
   *put name;
  end;
  end;
else do;
rc=dclose(did);
end;
run;
运行出来却得一个观测,也即得不到文件名字,就是文件打开失败,我也不知道是什么原因?请教指点下,谢谢!

板凳
leptonyu 发表于 2013-6-29 15:11:48
filename dir "C:\Documents and Settings\ibm\My Documents\My SAS Files\9.2\li0oi3pj1\";
这么写 中间断行了,你先打印到日志看看 别输出到数据集

报纸
victorwxw 发表于 2013-6-29 15:18:00
上面的dopen一段的运行结果是did=0,rc有一个数字,其他的值为空。另外请教下,我想读入的是在该文件夹下的所有excel表格,不是该文件夹下的所有文件。那么该怎么读取呢?请求帮助

地板
leptonyu 发表于 2013-6-29 15:19:27
说明这个路径不对,读取的文件,使用字符串处理函数来进行过滤,后缀为.xls的输出即可

7
victorwxw 发表于 2013-6-29 15:28:05
,非常谢谢!果然是路径名不对,我把filename dir "C:\Documents and Settings\ibm\My Documents\My SAS
Files\9.2\li0oi3pj1\";改成一行后,在日志窗口有显示出文件名字。非常谢谢

8
bobguy 发表于 2013-6-30 10:08:32
Here is a utility program in link https://bbs.pinggu.org/thread-2260545-1-1.html

I modify a little to fit your problem.

options cmplib=sasuser.funcs;
proc fcmp outlib=sasuser.funcs.dir;

function diropen(dir $);
   length dir $ 256 fref $ 8;
   rc = filename(fref, dir);
   if rc = 0 then do;
      did = dopen(fref);
      rc = filename(fref);
   end;
   else do;
      msg = sysmsg();
      put msg '(DIROPEN(' dir= ')';
      did = .;
   end;
   return(did);
endsub;

subroutine dirclose(did);
   outargs did;
   rc = dclose(did);
   did = .;
endsub;

subroutine dir_entries(dir $, files
  • $, n, trunc);
       outargs files, n, trunc;
       length dir entry $ 256;

       if trunc then return;

       did = diropen(dir);
       if did <= 0 then return;

       dnum = dnum(did);
       do i = 1 to dnum;
          entry = dread(did, i);
          /* If this entry is a file, then add to array */
          /* Else entry is a directory, recurse. */
          fid = mopen(did, entry);
          entry = trim(dir) || '\' || entry;
          if fid > 0 then do;
             rc = fclose(fid);
             if n < dim(files) then do;
                trunc = 0;
                n = n + 1;
                files[n] = entry;
             end;
             else do;
                trunc = 1;
                call dirclose(did);
                return;
             end;
          end;
          else
             call dir_entries(entry, files, n, trunc);
       end;

       call dirclose(did);
    endsub;
    run;

    %let dir="c:\temp";

    data _null_;
       array files[1000] $ 256 _temporary_;
       dnum = 0;
       trunc = 0;
       call dir_entries(&dir, files, dnum, trunc);
       if trunc then put 'ERROR: Not enough result array entries. Increase array
    size.';
       do i = 1 to dnum;
          if find(upcase(files),'.XLS') then
          put files;
       end;
    run;
  • 9
    victorwxw 发表于 2013-7-1 16:19:58
    非常谢谢,您是一位SAS高手。我SAS学得不好,你写的程序学习下哈。再次感谢

    10
    小跑朱彼得 发表于 2013-7-1 21:29:35
    victorwxw 发表于 2013-6-29 15:18
    上面的dopen一段的运行结果是did=0,rc有一个数字,其他的值为空。另外请教下,我想读入的是在该文件夹下的 ...
    ?,神马东西

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

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