DATA Step, Macro, Functions and more

Method to Temporarily set SAS option and then revert to original value

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 97
Accepted Solution

Method to Temporarily set SAS option and then revert to original value

Does anyone know of a simple method of temporarily setting a SAS option and then reverting it back to it's previous value?  I maintain a large set of macro functions that are frequently used by my team.  Some of these functions need to have specific SAS options set (such as DLCREATEDIR or SASTRACE), but since these macros are generally just small parts of larger programs I don't want to unilaterally change a system option someone may be relying on later on in their program or which may cause unexpected behavior. 

 

I've tried some stuff where I use GETOPTION to save the original value of the option I need to change so I can change it back when my function has done it's piece, but this seems kind of hit and miss (for SASTRACE at least) and it seems like there should be a better, simpler way, although perhaps not. 

 

Does anyone have some tips on handling situations like this?


Accepted Solutions
Solution
‎02-06-2018 12:30 PM
PROC Star
Posts: 1,430

Re: Method to Temporarily set SAS option and then revert to original value

Probably easiest to just use double quotes, like:

options sastrace="&SAStrace_Opt" ;

 

I think below is an example of saving, changing, and restoring the SASTRACE option:

 

*set option ;
options sastrace=',,t,dbs' ;
%put >>%sysfunc(getoption(sastrace))<< ;

*store the option in a macro var ;
%let SASTrace_Opt = %SYSFUNC(GETOPTION(SASTRACE));

*change the option ;
options sastrace='' ;
%put >>%sysfunc(getoption(sastrace))<< ;

*restore the option ;
options sastrace="&SAStrace_Opt" ;
%put >>%sysfunc(getoption(sastrace))<< ;

View solution in original post


All Replies
Super User
Posts: 13,016

Re: Method to Temporarily set SAS option and then revert to original value

Two procedures OPTSAVE and OPTLOAD.

 

Proc OPTSAVE will save current settings to a data set and then Proc opload will read a data set to set options:

 

Example from the online help that displays a current option, saves options, changes it , shows the changed value, reloads the saved and the shows that the saved version has been restored. The example only shows one option but all options in effect are saved/ restored.

 

libname mysas "c:\mysas";

proc options option=yearcutoff;
run;
proc optsave out=mysas.options;
run;
options yearcutoff=2000;
proc options option=yearcutoff;
run;
proc optload data=mysas.options;
run;


proc options option=yearcutoff;
run;
Frequent Contributor
Posts: 97

Re: Method to Temporarily set SAS option and then revert to original value

That would be perfect, except the option I'm really having trouble with is SASTRACE, and that's one of the few options that cannot be saved or loaded with OPTSAVE/OPTLOAD unfortunately (according to PROC OPTIONS LISTOPTSAVE).

 

Basically it's any option where I need to set it with quotes like

OPTION SASTRACE=',,t,dbs';

I can pull the original value with:

%LET SASTrace_Opt = %SYSFUNC(GETOPTION(SASTRACE));

But for some reason the normal quoting mechanisms aren't working when I try to use that variable later to set it back like this:

OPTIONS SASTRACE=&SASTrace_Opt;

I've tried manually putting quotes around the value in many different ways, but it won't take it.  Does the macro processor get partially disabled or something on OPTIONS statements?

 

 

Super User
Posts: 13,016

Re: Method to Temporarily set SAS option and then revert to original value

I don't use Information Maps so don't think I can go much further.

 

But what does %put SAStrace_opt ; generate after running %LET SASTrace_Opt = %SYSFUNC(GETOPTION(SASTRACE));?

Frequent Contributor
Posts: 97

Re: Method to Temporarily set SAS option and then revert to original value

I'm not sure what Information Maps are, the SASTRACE option affects SAS/ACCESS to relational DB products, or at least that's what I'm using it for. 

 

Thanks for your help thus far though.  I really wish that OPTLOAD/OPTSAVE would've worked here.  I hadn't heard of those previously so it's something to add to my toolkit in any case.

 

Here's the code output you requested:

119 17         %LET SASTrace_Opt = %SYSFUNC(GETOPTION(SASTRACE));
120 18         %PUT &=SASTrace_Opt;
121 SASTRACE_OPT=,,t,dbs
122 19         OPTIONS SASTRACE=&SASTrace_Opt;
123 NOTE: LINE GENERATED BY THE MACRO VARIABLE "SASTRACE_OPT".
124 19         ,,t,dbs
125             _
126             13
127 ERROR 13-12: UNRECOGNIZED SAS OPTION NAME ,.
128
129               _
130               13
131 19       ! ,,t,dbs
132              _
133              13
134 ERROR 13-12: UNRECOGNIZED SAS OPTION NAME T.
135
136 19       ! ,,t,dbs
137                ___
138                13
139 ERROR 13-12: UNRECOGNIZED SAS OPTION NAME DBS.

I've also tried the following (and many other permutations, dealing with quoting gives me a headache, so I generally just go for a brute-force approach to get it to work right), all of which result in the same error:

%LET SASTrace_Opt = %STR(%')%SYSFUNC(GETOPTION(SASTRACE))%STR(%');
OPTIONS SASTRACE=&SASTrace_Opt

%LET SASTrace_Opt = %SYSFUNC(GETOPTION(SASTRACE));
OPTIONS SASTRACE=%STR(%')&SASTrace_Opt%STR(%');

%LET SASTrace_Opt = %SYSFUNC(GETOPTION(SASTRACE));
OPTIONS SASTRACE=%BQUOTE(&SASTrace_Opt);

%LET SASTrace_Opt = %STR(%')%SYSFUNC(GETOPTION(SASTRACE))%STR(%');
OPTIONS SASTRACE=%BQUOTE(&SASTrace_Opt);

 

 

 

Solution
‎02-06-2018 12:30 PM
PROC Star
Posts: 1,430

Re: Method to Temporarily set SAS option and then revert to original value

Probably easiest to just use double quotes, like:

options sastrace="&SAStrace_Opt" ;

 

I think below is an example of saving, changing, and restoring the SASTRACE option:

 

*set option ;
options sastrace=',,t,dbs' ;
%put >>%sysfunc(getoption(sastrace))<< ;

*store the option in a macro var ;
%let SASTrace_Opt = %SYSFUNC(GETOPTION(SASTRACE));

*change the option ;
options sastrace='' ;
%put >>%sysfunc(getoption(sastrace))<< ;

*restore the option ;
options sastrace="&SAStrace_Opt" ;
%put >>%sysfunc(getoption(sastrace))<< ;
Frequent Contributor
Posts: 97

Re: Method to Temporarily set SAS option and then revert to original value

That seems to be working, although I'd swear I'd tried that before and it wasn't, I guess my memory isn't what it used to be.  I still really wish that OPTSAVE/OPTLOAD worked for all of the options since that'd be a cleaner solution overall.  I wonder why it doesn't.

Super User
Super User
Posts: 7,847

Re: Method to Temporarily set SAS option and then revert to original value

Don't understand why you are having trouble.  You do need the quotes. You either add them back when you revert.

%LET optsave = %SYSFUNC(GETOPTION(SASTRACE));
options SASTRACE='something else';
options sastrace="&optsave";

Or you could add the quotes when you create the macro variable. I would suggest storing the syntax needed to reset the option.

%LET optsave = sastrace="%SYSFUNC(GETOPTION(SASTRACE))";
options SASTRACE='something else';
options &optsave ;

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 131 views
  • 6 likes
  • 4 in conversation