Date: Wed, 15 Feb 2006 09:40:05 -0500
Reply-To: Peter Crawford <peter.crawford@BLUEYONDER.CO.UK>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Peter Crawford <peter.crawford@BLUEYONDER.CO.UK>
Subject: Re: Global or AUTOMATIC, should I use automatic?
looking at it a different way :
how would it be valid to call %little_macro() twice ?
(if it needs to instantiate an array, even once )
Since the problem is only that an array can be defined only once,
why use a global value to indicate that it has been done?
Why not just do it outside %little_macro() in something
invoked only once?
Peter
On Wed, 15 Feb 2006 09:23:11 -0500, data _null_; <datanull@GMAIL.COM>
wrote:
>Say I write a little macro that can be called more than once within a
>user written data step. The first call to little macro needs(wants)
>to create a temporary array and give it some initial values.
>
>Therefore "I think" the little macro needs to know if it has been
>called so it can emit the ARRAY statement or not.
>
>I create a global macro variable named &sysMacroName and give it the
>value &sysMacroName and check it. But then I see there are a number
>of seemingly unused AUTOMATIC macro variables that would be just as
>useful and then my little macro doesn't need to create a global
>variable.
>
>Below is a test using both methods, a) creating a global variable b)
>using AFSTR1.
>
>I probably should just redesign little macro and then I wont have to
>worry about it, but I thought I would like some global input.
>
>%macro littleTest(var=,newvar=);
> %global &sysMacroName;
> %if %quote(&&&sysMacroName) ne %quote(&sysMacroName) %then %do;
> ** this code only one time;
> array &sysMacroName._T[3] $1 _temporary_ ('~' '!','1');
> %end;
> &newvar = log(&var);
> if &sysMacroName._T[3] = '1' then do;
> call symput("&sysMacroName",' ');
> &sysMacroName._T[3] = '0';
> end;
> %let &sysMacroName = &sysMacroName;
> %mend littleTest;
>
>data work.test;
> x = 1;
> %littleTest(var=x,newvar=lx);
> %littleTest(var=x,newvar=lx2);
> run;
>%put _all_;
>
>
>%macro littleTest(var=,newvar=);
> %if %quote(&afstr1) ne %quote(&sysMacroName) %then %do;
> ** this code only one time;
> array &sysMacroName._T[3] $1 _temporary_ ('~' '!','1');
> if &sysMacroName._T[3] = '1' then do;
> call symput('afstr1',' ');
> &sysMacroName._T[3] = '0';
> end;
> %end;
> &newvar = log(&var);
> %let afstr1 = &sysMacroName;
> %mend littleTest;
>
>data work.test;
> x = 1;
> %littleTest(var=x,newvar=lx);
> %littleTest(var=x,newvar=lx2);
> run;
>%put _all_;
|