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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 5 replies
  • 1738 views
  • 3 likes
  • 5 in conversation