BookmarkSubscribeRSS Feed
Shonesum
Calcite | Level 5

I have a 3 macro files that I call all 3 macro a main sas program. In one of those macro file, I need a variable that I create called "Value1". How can I access that macro variable from another 2 macro file  and/or the main file. 

I tried %global value1; but it can only be called within 1 macro file.

 

 

Thanks in advance. 

 

5 REPLIES 5
Astounding
PROC Star

We're guessing.  We can't see your macros, we can't see the program that calls the macros, we can't see your data.  So here's a guess.

 

It can depend on where this statement goes:

 

%global value1;

 

Place it in the program that calls the macros, before any macros get called.

Shonesum
Calcite | Level 5

Lets say we have 3  files with 1 macro in each:

 

sas1.sas 

%global value1;

 %macro sas1()

%let value1 ="apple";

%mend;

 

sas2.sas 

 

 %macro sas2()

 

%mend;

 

sas3.sas 

 

 %macro sas2()

 

%mend;

 

 

macromain.sas

 

%sas1;

%sas2;

%sas3;

 

How can I access that value1 macro from sas1 file to sas2.sas and sas3.sas? How can I access that value 1 from sas1 in macromain.sas?

 

Tom
Super User Tom
Super User

Just reference it.  So in your main program you could do:

%sas1;
%put Value of Value1 is &value1;
%sas2;
%sas3;

Similarly inside of %SAS2() and %SAS3().

%macro sas2;

%put Value of Value1 is &value1;

%mend sas2;

 

Astounding
PROC Star

You can.

 

You can test that yourself, where you have:

 

%sas1;

%sas2;

%sas3;

 

Add a diagnostic statement:

 

%sas1;

%put &valuie1;

%sas2;

%sas3;

 

Most likely, you are assigning &VALUE1 the incorrect value.  In your statements, you have assigned a 7-character value (quotes included) not a 5-character value.

Reeza
Super User

If you're submitting your programs via batch this may not work because each program could be running in a separate session. 

 

Assuming you're using %INCLUDE, this is how I build my 'control' programs. This allows the macro variable myVar to be available to all programs.

 

%let myVar = TEST;

%let path_master = /folders/myfolders/projectABC

*Import data;
%include "&path_master/programs/P001 Import Data.sas";


*Clean data;
%include "&path_master/programs/P002 Clean Data.sas";


*Generate reports
%include "&path_master/programs/P003 Report Data.sas";

@Shonesum wrote:

Lets say we have 3  files with 1 macro in each:

 

sas1.sas 

%global value1;

 %macro sas1()

%let value1 ="apple";

%mend;

 

sas2.sas 

 

 %macro sas2()

 

%mend;

 

sas3.sas 

 

 %macro sas2()

 

%mend;

 

 

macromain.sas

 

%sas1;

%sas2;

%sas3;

 

How can I access that value1 macro from sas1 file to sas2.sas and sas3.sas? How can I access that value 1 from sas1 in macromain.sas?

 


 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 854 views
  • 0 likes
  • 4 in conversation