redaring 发表于 2009-12-8 13:48
数据文件是txt格式的,第一行的分隔符为tab,后面的分隔符都为逗号,在导入时想把第一行作为变量名,其他所有行为数据,可是分隔符不知道该怎么设置。我用了proc import 导入,可是delimiter只能指定一个。由于我有好几百个数据文件,不能一个去把第一行的tab替换成逗号。
刚刚开始学习sas几天,很多基本的问题都不懂,希望各位前辈指点一下。谢谢了!
以下是我自己写的,可是没能实现我想要的效果:
proc import datafile="d:\testfile.txt" out=testfile;
delimiter=',';
getnames=yes;
run;
If all files have the same "formats" in the following sense,
file1
**************************
var1 var2 var3 var4
1,2,c,5
**************************
file2
*************************
var1 var2 var3 var4
1,2,c,5
36,4,d,6
**************************
The you may think about to use filevar option in infile stetement.
Here is a sample program.
*************************;
data _null_;
infile 'c:\downloads\test*.txt' ;
input ;
put _infile_;
run;
%let loc= c:\downloads\;
filename fnlist pipe "dir &loc.test*.txt /w";
data test;
length fn $128 x1 x2 x4 8 x3 $1;;
infile fnlist;
input fn @@;
if upcase(substr(fn,1,4))='TEST' then do;
filen="&loc"||fn;
infile dummy filevar=filen firstobs=2 dsd end=eof truncover;
do while(not eof);
input x1 x2 x3 x4;
output;
end;
end;
*drop fn;
run;
proc print; run;
*********************
Here is the log;
251 data _null_;
252 infile 'c:\downloads\test*.txt' ;
253 input ;
254 put _infile_;
255 run;
NOTE: The infile 'c:\downloads\test*.txt' is:
File Name=c:\downloads\test.txt,
File List=c:\downloads\test*.txt,RECFM=V,
LRECL=256
var1 var2 var3 var4
1,2,c,5
NOTE: The infile 'c:\downloads\test*.txt' is:
File Name=c:\downloads\test2.txt,
File List=c:\downloads\test*.txt,RECFM=V,
LRECL=256
var1 var2 var3 var4
1,2,c,5
NOTE: The infile 'c:\downloads\test*.txt' is:
File Name=c:\downloads\test3.txt,
File List=c:\downloads\test*.txt,RECFM=V,
LRECL=256
var1 var2 var3 var4
1,2,c,5
36,4,d,6
NOTE: 2 records were read from the infile 'c:\downloads\test*.txt'.
The minimum record length was 7.
The maximum record length was 19.
NOTE: 2 records were read from the infile 'c:\downloads\test*.txt'.
The minimum record length was 7.
The maximum record length was 19.
NOTE: 3 records were read from the infile 'c:\downloads\test*.txt'.
The minimum record length was 7.
The maximum record length was 19.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
256
257 %let loc= c:\downloads\;
258
259 filename fnlist pipe "dir &loc.test*.txt /w";
260
261 data test;
262 length fn $128 x1 x2 x4 8 x3 $1;;
263 infile fnlist;
264 input fn @@;
265 if upcase(substr(fn,1,4))='TEST' then do;
266 filen="&loc"||fn;
267 infile dummy filevar=filen firstobs=2 dsd end=eof truncover;
268 do while(not eof);
269 input x1 x2 x3 x4;
270 output;
271 end;
272 end;
273 *drop fn;
274 run;
NOTE: The infile FNLIST is:
Unnamed Pipe Access Device,
PROCESS=dir c:\downloads\test*.txt /w,RECFM=V,
LRECL=256
NOTE: The infile DUMMY is:
File Name=c:\downloads\test.txt,
RECFM=V,LRECL=256
NOTE: The infile DUMMY is:
File Name=c:\downloads\test2.txt,
RECFM=V,LRECL=256
NOTE: The infile DUMMY is:
File Name=c:\downloads\test3.txt,
RECFM=V,LRECL=256
NOTE: 8 records were read from the infile FNLIST.
The minimum record length was 0.
The maximum record length was 50.
NOTE: 1 record was read from the infile DUMMY.
The minimum record length was 7.
The maximum record length was 7.
NOTE: 1 record was read from the infile DUMMY.
The minimum record length was 7.
The maximum record length was 7.
NOTE: 2 records were read from the infile DUMMY.
The minimum record length was 7.
The maximum record length was 8.
NOTE: SAS went to a new line when INPUT statement reached past the end of a line.
NOTE: The data set WORK.TEST has 4 observations and 5 variables.
NOTE: DATA statement used (Total process time):
real time 0.07 seconds
cpu time 0.03 seconds
275
276 proc print; run;
NOTE: There were 4 observations read from the data set WORK.TEST.
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
*******************************
Here is the listing;
Obs fn x1 x2 x4 x3
1 test.txt 1 2 5 c
2 test2.txt 1 2 5 c
3 test3.txt 1 2 5 c
4 test3.txt 36 4 6 d


雷达卡

京公网安备 11010802022788号







