Question

I've written a macro %xl to print an Excel file from a dataset with specific formatting. It is working fine by itself. However, the macro needs to set the title temporarily. I can clear the title at the end of the macro, but I really want to preserve the pre-macro title and restore it as the last step of my macro.

How can I capture the current value of title into a macro variable? Similar to %sysfunc(GetOption()) with options.

Thanks, pT

Was it helpful?

Solution

Titles and footnotes can be retrieved using the SASHELP.VTITLE view. So I would use that to save the current values, run your program, then run another step to reset the titles using call execute, like this:

data _old_titles;
  set sashelp.vtitle;
run;

/* do something */
footnote;
title;

/* reset titles and footnotes */
data _null_;
   set _old_titles;
   if type = 'T'
      then cmd = catt('title',number," '",trim(text)," ';");
      else cmd = catt('footnote',number," '",trim(text)," ';");
  call execute(cmd);
run;

Notice there is an extra blank used after the text value to prevent two consecutive apostrophes, which would translate into a single quote. Also not the use of single quotes to prevent any unwanted macro expansion.

Not perfect, but remember that you can have many titles and footnotes, so an option or SAS system function would be difficult to create. This has been a "feature request" many times but never implemented.

OTHER TIPS

It seems like instead of first setting the title and then trying to create a macro variable from it, you'd be better off first setting a macro variable (or multiple if there are several different titles you'll want to use repeatedly), then using that in the title statement, like this:

%let title_text = "This is my pre-macro title";

/* Set title first time. */
title "&title_text";

/* Call macro. */
%xl;

/* Restore title. */
title "&title_text";

If you have SAS/AF licensed you can create an SCL program to do what you want. Follow instructions here on creating a SCL program.

in your SCL program (I named mine gettitle.scl) you want

init:                                  
   call symput('TitleOld',gettitle(1));                                  
return;

After you compile this you can call it like this

title "whatever it happens to be";
proc display c=myscl.catalog.gettitle.scl; run;

%xl;

title "&TitleOld";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top