03-17-2016 03:51 PM
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
03-17-2016 04:21 PM
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.
03-17-2016 04:28 PM
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.
03-17-2016 07:49 PM
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.
03-19-2016 09:14 PM
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.