1
votes

Say that in a folder I have over 100+ .dat files with no common generic file names. For instance, the files are not named f001, f002, f003 (you get the pattern). The names are random. I need to parse these .dat files into SAS files. Each files have the same column/attributes. I use the following code to parse one of the .dat file:

data have;
infile 'C:\SAS\have.dat' dsd dlm='|';
input var1 var2 var3$;run;

The code is the same for each .dat files. Is there a way in SAS to simply parse all the files in a folder and name these SAS files the same as their original .dat. I want all the files to be separated and not under one SAS file.

[UPDATE] I first start by reading all the filenames in my folder using the following SAS command:

data yfiles;
keep filename;
length fref $
8 filename $ 80;
rc = filename(fref,
'Y:\Files\Exchanges');
if rc = 0 then do ;
did = dopen(fref);
rc = filename(fref); end;
else do; length msg $200.;
msg = sysmsg();
put msg=; did =.;
end;
if did <=0 then putlog
'ERR' 'OR: Unable to open directory.';
dnum = dnum(did);
do i =1 to dnum;
filename = dread(did, i);
/* If this entry is a file, then output. */
fid = mopen(did, filename);
if fid >0 then output;
end;
rc = dclose(did);
run;

In yfiles I have all the names of my .dat datasets.

Now, how can I loop through each .dat files names of my yfiles dataset to apply the above parsing code?

2
do you want these in different data sets or 1 large data set?DomPazz
in different data setsPlug4

2 Answers

1
votes

Use CALL EXECUTE and a Data Step to loop through the file names. You use the Data Step to build and execute the SAS statements.

data _null_;
set yfiles;
format outStr $200.;

outStr = 'data have' || strip(put(_N_,best.)) || ';';
call execute(outStr);

outStr = "infile 'C:\SAS\" || strip(filename) || "' dsd dlm='|';";
call execute(outStr);

call execute("input var1 var2 var3$;run;");
run;
0
votes

Try using a pipe file ref with the command doing a directory listing. Parse the output and loop over the directory contents.