BookmarkSubscribeRSS Feed
JamesNewcombe
Calcite | Level 5

macroHello

I have written a SAS macro to open an excel spreadsheet using dde. I've also got one to do a "save as"

%macro excelsaveas(ifile, ilength);

filename cmds dde 'excel|system';

data _null_;

file cmds;

   put '[error(false)]'; *prevent excel asking "are you sure?";

   put &ifile;

   x=sleep(&ilength.);

   put "[QUIT()]";

   x=sleep(&ilength.);

run;

%mend;

I can call the macro like this

%excelsaveas(%str('[SAVE.AS("C:\TEMP\test2.xls")]'), 5);

The first parameter is the save-as name and location. The second parameter is how many seconds SAS should sleep for before continuing, because SAS doesn't know when the execution of the save-as command has been completed.

This works fine, however what I want to do is include a macro variable in the save-as name of the spreadsheet. This will allow me to save the spreadsheet with the date in the name, which would be really useful! However, I can't find any combination of double/single quotes or quoting functions that work. I've also tried rewriting the macro in various ways, but SAS always comes back with an error.

Has anyone done something like this before, is it possible, and if so how?

Many thanks

1 REPLY 1
ballardw
Super User

I think you would be much better off trying to build the save as part in the macro instead of as a parameter. The various quoting and unquoting functions in SAS will be a nightmare with that whole statement.

I solved a similar bit with following: (xlsx not xls but logic is the same)

 

put %unquote(%str(%'[SAVE.AS("&name..xlsx",51)]%'));

Your macro call using just the path and file name

%excelsave(%str(c:\temp\test2.xls),5);

with line instead fo put &ifile

put %unquote(%str(%'[SAVE.AS("&ifile",51)]%'));

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 1 reply
  • 1510 views
  • 0 likes
  • 2 in conversation