呼び出し環境からマクロ変数をインポートし、マクロ変数をエクスポートして元の呼び出し環境へ戻します。
| カテゴリ: | マクロ |
/* The %TRYIT macro is created. %TRYIT sets the global macro */
/* variable MYMAC from the macro code. */
%macro tryit(x);
%global mymac;
%if &x=1 %then %let mymac=2;
%else %let mymac=3;
%mend;
/* MYMAC is defined as a global variable, and its value is set to 4. */
%global mymac;
%let mymac=4;
/* The %TRYIT macro is invoked by the CALL EXECUTE routine. The macro code */
/* is executed immediately, and MYMAC has a value of 2. The value of the */
/* MYMAC variable is retrieved by using the SYMGET function. */
data _null_;
call execute('%tryit(1)');
value=symget('mymac');
put value= '(should be 2)';
run;
/* The value of MYMAC is set to 4. */
%let mymac=4;
/* This DATA step uses the DOSUBL function to achieve the same result. */
data _null_;
rc=dosubl('%tryit(1)');
value=symget('mymac');
put value= '(should be 2)';
run;
/* The definition of %TRYIT is changed so that a DATA step is */
/* invoked to set the value of MYMAC. */
%macro tryit(x);
%global mymac;
data _null_;
if &x=1 then mymac=2;
else mymac=3;
call symputx('mymac', mymac);
run;
%mend;
/* The value of MYMAC is set to 4 by the %LET macro. CALL EXECUTE */
/* calls the %TRYIT macro to be invoked immediately, but the macro */
/* contains a DATA step. The DATA step code executes after the */
/* current DATA step completes. Therefore, the value of MYMAC remains */
/* 4. The call to the SYMGET function does not retrieve the proper */
/* value of MYMAC, which is 2. */
%let mymac=4;
data _null_;
call execute('%tryit(1)');
value=symget('mymac');
put value= '(should be 2)';
run;
/* If you use the DOSUBL function, the code in the %TRYIT macro is */
/* executed immediately, and the SYMGET function returns a value of 2. */
%let mymac=4;
data _null_;
rc=dosubl('%tryit(1)');
value=symget('mymac');
put value= '(should be 2)';
run;