DATA Step, Macro, Functions and more

Read and append files in different folders

Reply
Occasional Contributor
Posts: 8

Read and append files in different folders

Hello, I'm a beginner on SAS and I have a question of writing a macro. Basically I want to write a macro to read several files with the same name in different folders. For example, folder a and b both have a file name x. What I want to do is to read append the two files and create another observation which indicates which folder the part of new combined file comes from. 

 

 

Thank you so much 

 

XB

Super User
Posts: 5,081

Re: Read and append files in different folders

Since you're a beginner in SAS, there are a couple of requirements before you even think about writing a macro.

 

First, write an example of the program.  Hard-code everything, and make sure that the program works.

 

Second, consider what will happen when turning the program into a macro.  Which parts of the program will never change, and which parts will change each time you call the macro.

 

Only then can you even consider writing a macro to accomplish the task.

Super User
Posts: 10,483

Re: Read and append files in different folders

In addition to @Astounding's good advice the content of the files makes a little difference.

It may well be possible to read all of the files using a single file reference such as :

Filename MyInput ("C:\path\folder1\myfile.text" "C:\path\folder2\myfile.text" "C:\path\folder3\myfile.text");

and use that type reference in the INFILE statement along with the the FILENAME option to capture the name of the specific file.

Trusted Advisor
Posts: 1,115

Re: Read and append files in different folders

Hello @xiaobing,

 

Here is an example of what @ballardw has outlined:

 

filename all ('C:\Test\abc.txt', 'C:\Temp\Data\abc.txt');

data want;
length path $260; /* adapt the length if necessary */
infile all filename=path; /* add options as needed */
input x y z; /* adapt as appropriate */
folder=substr(path,1,max(find(path,'\',-999)-1,3));
run;

This assumes that the two raw data files (abc.txt) not only have the same name, but also the same structure. Also, it creates a new variable (named FOLDER), not an observation, which indicates the source folder of each observation.

 

Super User
Posts: 17,784

Re: Read and append files in different folders

Are these files SAS datasets or text files? Is this related to your latest question?

Occasional Contributor
Posts: 8

Re: Read and append files in different folders

Hello, Reeza.Thanks for the reply.

It's SAS dataset. So Basically, I have 10 folders with different names. Within each folder, there were many sas datasets. What I basically need is to create a new variable that's equal to its belonging folder name for each dataset. 

 

Cheers,

 

XiaoBing 

Ask a Question
Discussion stats
  • 5 replies
  • 318 views
  • 3 likes
  • 5 in conversation