BookmarkSubscribeRSS Feed
FP12
Obsidian | Level 7

Hi all,

I have a directory &dir, with several files in txt in it. Some files begin by "FILE_" others don't.
The files beginning by "FILE_" have the same format, "hopefully"
A $6
B $12

 

I just need to create a table Z, as an append of all files beginning by "FILE_" whatever is the number of files in the directory dir.

I am a bit lost on how to do that.
Can someone help me on 'easy' problem (I am a SAS almost beginner)?

 

Thanks

2 REPLIES 2
mkeintz
PROC Star

You could use sas functions to open a directory and get a list of file names in that directory, but this problem is also amenable to using an operating system command to trivially list the desired files names.  That list can be put in an %include file, as in:

 

filename mypipe pipe "dir/b c:\temp\FILE_*.txt";
filename tmp  temp ;

data _null_;
  infile mypipe truncover end=eod;
  input filnam $50.;
  filnam=cats('c:\temp\',filnam);

  file tmp;
  if _n_=1 then put 'infile "(' @;
  else          put ',' @ ;
  put filnam @;
  if eod then put ')";';
run;

options source2;
data want;
  %include tmp ;
  *input x y;
  input (a b) ($6. $12.);
run;

 

Notes:

  1. This program assumes a windows environment (hence the use of the "dir/b" command -- i.e. a "bare" directory list of all txt files that have names beginning with "FILE_".
  2. That dir command is embedded within a filename statement of type "pipe" - i.e. pipe the output of the dir command when the filename reference MYPIPE is used.
  3. The "filename tmp temp" established a temporary external file - i.e. it will be deleted when the sas session is finished.  You don't need to know where the file is located.  You're just going to write stuff to it, and read it back when needed during the session.
  4. The first data step reads in the piped list of file names, prepends the directory name and writes an INFILE statement with a comma-separated list of full names to TMP.  The result will look like this:
       infile "(c:\temp\FILE_001.txt ,c:\temp\FILE_002.txt)" ;
  5. The second data step uses "%include tmp" to read in TMP as one or more SAS statements.  Note the "option source2" which tells sas to print to log the content of the %include files .
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
FP12
Obsidian | Level 7

Hello mkeintz,

 

Thanks for the reply.

 

I got an issue when I try to "include tmp":

ERROR: Physical file does not exist, /apps/sas94/sasconfig/Lev1/SASMain/c:\temp/usr/bin/ksh:.

I am indeed working on Windows.

 

I think it means the file filenam wasn't created as planned in the first datastep?

I don't really understand the path file because you are mixing a SAS path with a Windows path. Is it normal?

Or maybe I am not granted to write (I see a Lev1 in the path that's why I think this)?

 

Thanks

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 803 views
  • 0 likes
  • 2 in conversation