BookmarkSubscribeRSS Feed
jjames1
Fluorite | Level 6

I have a question that I suspect has an answer, but I have seached online for quite a long time and have yet to find one solution.

 

Suppose I have a folder called "MySasFolder" and inside MySasFolder I have N number of  files: "a.xml", "b.xml", "c.xml","d.xml" etc. and so on.


 I have to read this data  into a SAS dataset.

 

Is there a way that SAS can loop through all files in a folder and input them into one SAS dataset without me having to tell SAS the names of each file ?

 

 When I input each file into the SAS dataset I also need to keep the name of each file as a variable as well.

 

Any help would be much appreciated!!!

 

Thanks,

 

James

3 REPLIES 3
Reeza
Super User

Are all the XMLs formatted the same way? They have the same structure?

 

Do you have a process built to import one file to start with, thats what you need first. Then you can create a list of files and call the macro using CALL EXECUTE.

 

I have an example here, but uses XLSX files:

https://github.com/statgeek/SAS-Tutorials/blob/master/Import_all_files_one_type

 

You can replace my PROC IMPORT with your import procedure.

 

 

jjames1
Fluorite | Level 6

No the xmls are of different structure.

But my SAS code is able to handle all different types of XMLs that I have.

 

But all these cases I manually input my xml filename into a macro and then do my process.

 

For example if I need to read ABC.xml I do the following 

%let variable= ABC

filename output temp;
libname output xmlv2 "/home/test/&variable..xml" automap=replace xmlmap=output;
proc copy in=output out=work noclone;

In my case I want to read from a folder that has thousands of xml files

 

So I know that this approach is not ideal.

 

 

Reeza
Super User

The idea is still the same. Wrap your current code into a macro, in my example it's called IMPORT_FILE. The other macro LIST_FILES (first one) creates a list of files. The last data step then uses the file paths to import the files.

 

I updated some comments in the program that may help you work through it on your side.

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
  • 3 replies
  • 759 views
  • 0 likes
  • 2 in conversation