SAS从文件名设置一个变量 [英] SAS set a variable from a filename

查看:425
本文介绍了SAS从文件名设置一个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个文件夹中有一些csv文件。它们都具有相同的结构(3列)。下面的SAS代码将所有这些导入到一个数据集中。它包括3列及其文件名。

I have a number of csv files in a folder. They all have the same structure (3 columns). The SAS code below imports all of them into one dataset. It includes the 3 columns plus their file name.

我的挑战是文件名变量包含目录和驱动器号(例如Z:\DIRECTORYA\DIRECTORYB\file1.csv)。我如何只列出文件名而不是路径(例如file1.csv)?谢谢

My challenge is that the filename variable includes the directories and drive letter (e.g. 'Z:\DIRECTORYA\DIRECTORYB\file1.csv'). How can I just list the file name and not the path (e.g. file1.csv)? Thank you

data WORK.mydata;
%let _EFIERR_ = 0; /* set the ERROR detection macro variable */
length FNAME $80.; 
infile 'Z:\DIRECTORYA\DIRECTORYB\*2012*.csv' delimiter = ',' MISSOVER DSD lrecl=32767 filename=fname firstobs=2;
informat A $26. ;
informat B $6. ;
informat C 8. ;
format A $26. ;
format B $6. ;
format C 8. ;
input
    A $
    B $
    C;
if _ERROR_ then call symputx('_EFIERR_',1);
filename=fname;
run;


推荐答案

我认为最好的办法是使用正则表达式。添加到您的DATA标签:

I think, your best bet is to use regular expressions. Add to your DATA stel:

reg1=prxparse("/\\(\w+\.csv)/");
if prxmatch(reg1, filename) then
                          filename=prxposn(reg1,1,filename);

这篇关于SAS从文件名设置一个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆