BookmarkSubscribeRSS Feed
hdg
Obsidian | Level 7 hdg
Obsidian | Level 7

Hello, I have a set of datasets which I would like to output in an Excel File. Is there a way to do this quickly rather than calling proc export each time for each dataset

%let MyDS = ('out.Ids', 'out.Vars', 'out.Places')

%let MyDSname = (Ids, Vars, Places)

I would like to create a macro that check if each dataset exists and then output to an Excel spreadsheet with the Tab name as specified in the corresponding MyDSname ...

Something like... %macro Out(MySpreadsheetName, MyDS, MyDSname);

Thanks very much for your help

6 REPLIES 6
LinusH
Tourmaline | Level 20

Shouldn't be a problem.

Use open() function to check the existence of a data set, then conditionally execute a Proc Export.

Data never sleeps
Barnipaz
Obsidian | Level 7

Or the exist function to check the the existence.

%sysfunc(exist(<data_set>));

If you use the open(), as suggested by LinusH, it's better to use the close after the check, otherwise the DataSet could be locked.

hdg
Obsidian | Level 7 hdg
Obsidian | Level 7

Thank you both. but I am also looking how to Parse my macro variables so as to use the proc Export command I'm not sure how to use the loop

LinusH
Tourmaline | Level 20

You just use the macro variables in the code. Perhaps you need to learn a little about macro programming before getting started with this task.

Loop, what loop?

Data never sleeps
hdg
Obsidian | Level 7 hdg
Obsidian | Level 7

Thanks Linus, but the macro variable MyDS refers to the list of datasets 3 in this case. The macro variable MyDSname corresponds to the name I want to call the tab in the spreadsheet which they are store.

I dont think I can just use a proc export directly passing MyDS and MyDSname

I would have to parse MyDS to extract the first value same way MyDSname and then use proc export.

. I wanst sure how to do this.

%let MyDS = ('out.Ids', 'out.Vars', 'out.Places') %let MyDSname = (Ids, Vars, Places)

ballardw
Super User

Effective macro coding always starts with the same place: A base SAS section of code that works.

Provide an example of ONE of the proc exports you are attempting that works and then indicate which pieces change.

And you almost never need to include quotes in the parameters as they will be treated by the macro processor as text.

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 6 replies
  • 1326 views
  • 0 likes
  • 4 in conversation