Help using Base SAS procedures

Next Available Macro Variable

Reply
Contributor
Posts: 42

Next Available Macro Variable

Good Morning All,

I am attempting to dynamically allocate a macro variable based on the availability of the macro variable name.

My problem is that for some reason when 2 or more my batch jobs run at the same time the macro variable I am using ("dog" in the example below) gets overridden by the next job. As a result I would like to read in the macro variable "dog" and change it's name to "dog1". If "dog1" is not available then check the availability of "dog2" and so on until an available name is located then specify the value (by resolving "dog").

The code I have written is as follows:

OPTIONS MPRINT MLOGIC SYMBOLGEN;

%LET DOG = SCOTT;
/*%LET DOG1 = CAT;*/
DATA STUFF;

LOOPEND = 1;

DO I = 1 TO LOOPEND;

TEXT = SYMGET('DOG'||TRIM(LEFT(PUT(I,8.))));

IF TEXT = "" THEN CALL SYMPUT('DOG'||TRIM(LEFT(PUT(I,8.))),SYMGET('DOG'));
ELSE LOOPEND +1;

END;

RUN;

%PUT _GLOBAL_;

/*%SYMDEL DOG DOG1 DOG2 DOG3 DOG4;*/

Any help you could offer would be greatly appreciated. Thank you for your time.

Regards,
Scott
Respected Advisor
Posts: 4,173

Re: Next Available Macro Variable

Posted in reply to Scottcom4
Hi

Every single batch job runs in its own environment so one batch job overwritting a macro variable from another batch job can't happen.

To check for existence of a macro variable in a data step use "symexist()" function. There is also a dictionary table (sashelp.vmacro) which you could query.

But as said: Batch jobs won't overwrite each others' environment. So may be you investigate the cause of the issue first.

HTH
Patrick
Contributor
Posts: 42

Re: Next Available Macro Variable

Hi Patrick,

That was my understanding as well, however my collegues are witnessing the override. I was hoping to create code that would prove this by dymanically creating macro variables. If there is no conflict and I can always use the 'dog1' variable then this means I am correct and the batch jobs are running in their own environment..

Basically we have a weekly.bat which kicks off 10 jobs and a daily.bat which runs 5 jobs. As each job completes it runs an include statement (which is the same for each job regardless of it's freqeuncy portalcopy.sas) kicks off a piece of code which copies the output to a sharepoint location. We use the marcro variable &decisionvar. from the first piece of code to resolve in portalcopy.sas to specify the file to be copied to sharepoint. Somehow, according to my collegues this is being overridden.

Does anyone have any suggestions as to why this is happening or a better way to prove my beliefs?

Thank you for your help.
Contributor
Posts: 42

Re: Next Available Macro Variable

Posted in reply to Scottcom4
Hi Guys,

Thank you for your help, but I finally worked it out. I just needed another approach. The code is attached if you are interested.


OPTIONS MPRINT MLOGIC SYMBOLGEN;

%LET DOG = SCOTT;

Data stuff1;
I = 1;
loop: TEXT = SYMGET('DOG'||TRIM(LEFT(PUT(I,8.))));
if TEXT = "" then goto done;
I+ +1;
goto loop;
done: CALL SYMPUT('DOG'||TRIM(LEFT(PUT(I,8.))),SYMGET('DOG'));
Run;

%put _USER_;
Respected Advisor
Posts: 4,173

Re: Next Available Macro Variable

Posted in reply to Scottcom4
Hi

I assume that you're actually not running several independent jobs but one single job. Using %include means collecting the code from an external .sas file but then executing it in the job which is "including" the code. So if you have several %includes in one job that means all these included jobs run in the same environment - and you can overwrite things.

Better start every single job separately - or use "systask" instead of "%include" as "systask" will create a new environment per call (and you could even run the jobs in parallel).

HTH
Patrick
Contributor
Posts: 42

Re: Next Available Macro Variable

Hi,

We are running several independant jobs but also a number where it is a single job with multiple includes.

I completely agree with what you are saying.

We have a combination of the multiple includes within a job and single includes within multiple jobs. The override is perfectly acceptable for the multiple includes as they are running in sequence and therefore the job that kicks off the include needs to alter the macro variable in order for the data to be exported to the correct location. They seem to believe there are problems when running two individual jobs at the same time.

This makes absolutley no sense to me given my understanding of how macro variables work.

Thank you again for your help.
Valued Guide
Posts: 634

Re: Next Available Macro Variable

Posted in reply to Scottcom4
Here is a little macro function that will return the next available value.
[pre]
%let dog=scott;
%let dog1=bill;
%let dog2=george;
%let dog3=notsue;
%macro nextdog;
%local cnt;
%let cnt=;
%do %while(%symexist(dog&cnt));
%let cnt=%eval(&cnt+1);
%end;
&cnt
%mend nextdog;
%put nextdog is %nextdog;
%let dog%nextdog=Johnny;
%put nextdog is %nextdog;
[/pre]
Contributor
Posts: 42

Re: Next Available Macro Variable

Thank you, that is very helpful.
Ask a Question
Discussion stats
  • 7 replies
  • 150 views
  • 0 likes
  • 3 in conversation