BookmarkSubscribeRSS Feed
xiaobing
Calcite | Level 5

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

5 REPLIES 5
Astounding
PROC Star

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.

ballardw
Super User

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.

FreelanceReinh
Jade | Level 19

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.

 

Reeza
Super User

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

xiaobing
Calcite | Level 5

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 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 2136 views
  • 3 likes
  • 5 in conversation