BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hello,
I would like to know on how to store and reuse macro variables.Thanks for your help in advance.

I have created a dataset named mylib.demog.And I have created 2 macro variables name1, name2 and stored in an SAS macro library.

libname mylib 'C:\Documents and Settings\Desktop' ;
data mylib.demog;
input name $ marks;
datalines;
tom 20
sam 40
pete 45
kat 55
bat 32
dell 67
;
run;

options mstored sasmstore=mylib;
%macro freq/store;
%let name1=%bquote('tom');
%let name2=%bquote('dell');
%put &name1.;
%put &name2.;
%mend freq;


Now when I run this below code, %freq prints 2 values - 'tom,'dell' in the log file as expected.

options mstored sasmstore=mylib;
%freq;


Now if I want to use these 2 values , how do I use them ?
For example , if want to print the data from mylib.demog datset where name in ('tom,'dell') using the macro %freq., How do i do that?

Thanks
6 REPLIES 6
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
I expect you would want to declare the SAS macro variable names you are using as %GLOBAL variables - then you can use these macro variables in your code, but only after executing the OPTIONS statement as you identified and then by also invoking %FREQ; -- after the %GLOBAL references (or add the %GLOBAL to your FREQ macro code).

Suggest you use this Google advanced search argument to find technical discussion on the topic as SAS support http://support.sas.com/ website.

using macro store facility site:sas.com

Scott Barry
SBBWorks, Inc.


SAS Macro Language: Reference, Scopes of Macro Variables
http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/a002047080.htm
Cynthia_sas
SAS Super FREQ
Hi:
There is a difference between storing the macro PROGRAM and referencing macro VARIABLES. The macro PROGRAM can be created and used within a session (where it would live in the WORK.SASMACR catalog) or it can be stored and referenced from an AUTOCALL library or it can be stored and referenced from a stored, compiled macro location.

Macro VARIABLES, on the other hand, are somewhat ephemeral. They are not "stored" from session to session. They either live, within one session, in a GLOBAL symbol table or a LOCAL symbol table. However, if you store your macro PROGRAM, it is the creation method, the creation logic which you are storing -- NOT the macro VARIABLES themselves. Macro VARIABLES can be created in a variety of different ways, but they only "live" in memory for the duration of your session. One way to create macro variables is with %LET, another way is with CALL SYMPUT. Either of those methods work outside and inside a macro PROGRAM. So you have several different combinations of creation possibilities.

It seems like you are confusing the storage of macro PROGRAMS with how to reference macro VARIABLES. Every time you run your %FREQ macro PROGRAM, it will, indeed, create 2 macro VARIABLES called &NAME1 and &NAME2 -- they will however, be LOCAL in scope -- that is, they will only live in the memory of the LOCAL symbol table for macro variables unless you explicitly make them GLOBAL. Consider this program:

[pre]
%let name1 = Mary;
%let name2 = William;

ods listing;
title "List for: &name1, &name2";
proc print data=sashelp.class;
where name in ("&name1", "&name2");
run;
[/pre]

You do not need a macro PROGRAM to create or use two macro variables. A good rule of thumb when designing a macro PROGRAM or process is to start with a WORKING program. In your case, you should start from the program that gives you the desired output. For example, to generate the above program, I started with this:
[pre]

ods listing;
title "List for selected students";
proc print data=sashelp.class;
where name in ("Mary", "William");
run;
[/pre]

Then I figure out where I could use my macro VARIABLES, decided on the names of those macro VARIABLES and then tested them out in the program by assigning values to &NAME1 and &NAME2 and then using those macro VARIABLES to see whether they'd work in the program as it was written. As you can see, I decided to change the title to show the names that I was selecting.

Some information on the scope of macro VARIABLES can be found here:
http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/a002293860.htm
http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/a001072159.htm
http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/a001072103.htm

A good introduction to the difference between macro VARIABLES and a macro PROGRAM is here:
Doc on Using macro VARIABLES:
http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/a002047074.htm

Intro to Macro Facility (user group paper):
http://www2.sas.com/proceedings/sugi28/056-28.pdf

cynthia
deleted_user
Not applicable
Thanks Cynthia..that really helped me.
"It seems like you are confusing the storage of macro PROGRAMS with how to reference macro VARIABLES." - This line itself answered my question.
Here is what understood. running %freq will actually call my MACRO program which will create 2 macro variables &name1. &name2. .
Am I right??

Thanks
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Yes, but you still have the issue with a GLOBAL versus a LOCAL macro, as previously mentioned. You can see what macro variables are accessible using these commands:

%PUT _LOCAL_;

%PUT _GLOBAL_;

%PUT _AUTOMATIC_;

..or..

%PUT _ALL_;


Scott Barry
SBBWorks, Inc.
Cynthia_sas
SAS Super FREQ
Hi:
Scott is correct in his recommendation. As I said in the rest of the paragraph:

It seems like you are confusing the storage of macro PROGRAMS with how to reference macro VARIABLES. Every time you run your %FREQ macro PROGRAM, it will, indeed, create 2 macro VARIABLES called &NAME1 and &NAME2 -- they will however, be LOCAL in scope -- that is, they will only live in the memory of the LOCAL symbol table for macro variables unless you explicitly make them GLOBAL.


The documentation links that I posted have many good explanations of local versus global scope of macro variables. How to reference the macro variables created by your %FREQ program will be complicated even more if you do not understand the difference between LOCAL and GLOBAL macro variables.

cynthia
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
To tgt_sas
Yes, time for some reading, testing and learning with the SAS macro variable environment processing. The questions are somewhat fundamental and can be revealed with individual experience and testing.

Scott Barry
SBBWorks, Inc.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 6 replies
  • 842 views
  • 0 likes
  • 3 in conversation