Using GETOPTION to Save and Restore Options
The GETOPTION function can be used to determine the current value of an option.
%let optionValue = %sysfunc(getoption(CENTER)); %put NOTE: The value of the CENTER option is &optionValue;
produces the following in the SAS LOG:
1 %let optionValue = %sysfunc(getoption(CENTER)); 2 %put NOTE: The value of the CENTER option is &optionValue; NOTE: The value of the CENTER option is CENTER
Use in Utility Programs and Macros
This technique can be of particular value for general purpose macros which rely on being able to reset the values of certain options. Such macros should reset any options that it changed back to their original value. Consider, for example, a macro that should produce output that is:
- left-aligned (i.e., not CENTERed)
- does not include the automatic page number produced by SAS
- does not include the date/time that the SAS job started
Before the code that produces the output, the macro could get the current values and save them in a local macro variable:
%local saveOptions; %let saveOptions = %sysfunc(getoption(CENTER)) %sysfunc(getoption(NUMBER)) %sysfunc(getoption(DATE)); options nocenter nonumber nodate;
It could then generate whatever code is necessary to produce the desired output. Before ending the macro could reset the CENTER, NUMBER and DATE options to their original values as follows:
The logic to get and restore the values must be cognizant to generate the correct syntax for each option and it's value. For example, suppose in the example above, it was also desired to reset and then restore the options that controls whether the output is PORTRAIT or LANDSCAPE. Running the following simple %put statement shows the value for the ORIENTATION option.
3 %put NOTE: Orientation is %sysfunc(getoption(ORIENTATION)); NOTE: Orientation is PORTRAIT
and resetting that requires an OPTIONS statement of the form:
OPTIONS ORIENTATION = XXXX;
where XXXX is the original value. For such cases, it is probably best to create a separate macro variable for each option and use the values to generate the appropriate OPTIONS statement. Thus, the example above, with the addition of the ORIENTATION option, could be done as follows:
%let center = %sysfunc(getoption(CENTER)); %let number = %sysfunc(getoption(NUMBER)); %let date = %sysfunc(getoption(DATE)); %let orientation = %sysfunc(getoption(ORIENTATION));
with the values reset at the conclusion of the macro using:
options ¢er &number &date orientation = &orientation;
The KEYWORD option
By using the KEYWORD reporting option as the second argument of the GETOPTION function, you can, in some cases, provide even further simplification. The code to save the ORIENTATION system option becomes:
%let orientation = %sysfunc(getoption(ORIENTATION,keyword));
Now the OPTION statement becomes:
options ¢er &number &date &orientation;
The macro variable &ORIENTATION now includes all the text that is needed to reset the option, and the ORIENTATION= is no longer needed.
Note that for options that require the value to be quoted the KEYWORD option will not generate text that can be used in an OPTION statment. You could use this type of syntax for those cases:
%let formchar = FORMCHAR="%qsysfunc(getoption(FORMCHAR))";
Saving and Restoring all OPTIONS values
If your program changes more than a few options, you might want to consider PROC OPTSAVE and PROC OPTLOAD, used when you want to save and then restore ALL of your settings.