BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Is there a way to display the value of a macro variable in a table analysis or summary table?
1 REPLY 1
Cynthia_sas
SAS Super FREQ
Hi:
It really depends on what you mean by "displaying the value of a macro variable in a table analysis or summary table".

If you want to know whether EG will let you select a macro variable for a Task Role or in any of its interactive tasks, the answer is NO. There is a way to customize your EG code, to use macro variables, and there is a parameter manager in EG which allows you to link macro variables to tasks, but EG will not automatically allow you to use macro variables in an interactive task or wizard.

In your previous posting you wanted a way to:

. . . create a variable (say number of records) from one table to be used in a calculation in another table (without linking the tables).


This is what my code example accomplished -- Using a macro variable, &CNTOBS, I got the number of records in SASHELP.CLASS and used that macro variable in a calculation in another table (SASHELP.SHOES) . If you cut and paste the code from this forum posting
http://support.sas.com/forums/thread.jspa?threadID=2091&tstart=0
in an EG code node and run it, you should see that the number of observations in SASHELP.CLASS is put into a macro variable by the proc sql code.
Then, that macro variable value is used to create a permanent variable in the data set WORK.NEW, as shown in this output:
[pre]
Using the Number of Obs ( 19 ) from CLASS to divide by in SHOES

Obs Region Product Sales divideby newnum
1 Africa Boot $29,761 19 1566.37
2 Africa Men's Casual $67,242 19 3539.05
3 Africa Men's Dress $76,793 19 4041.74
4 Africa Sandal $62,819 19 3306.26
5 Africa Slipper $68,641 19 3612.68

[/pre]

In my program, I used &CNTOBS to assign a value to DIVIDEBY, I can use that new variable, DIVIDEBY, in a task for table analysis or summary table.
Of course, since DIVIDEBY is the same value on every observation, it may not be very useful in a table analysis. In my program, I could have coded something like this:
[pre]
newnum = sales / &cntobs;
[/pre]
But, &CNTOBS is just the number, 19 . While I can use the constant number 19 in a division; there are not many places in a summary table task or a table analysis where I can use the number 19 in a meaningful way.

It seems that you have something very specific in mind when you ask your question and it's hard to envision what it is you want: do you want EG to let you use a macro variable in a task or wizard; or want the macro variable value in a title or used as a label; or want the macro variable to appear as an informational note before or after some table analysis. In the above output, the macro variable &CNTOBS is used in a SAS title statement and also used in a DATA step RETAIN statement.

Generally, a TABLE ANALYSIS task in EG produces PROC FREQ code and a SUMMARY TABLE task in EG produces PROC TABULATE code. I'm having a hard time figuring where you would want to use a macro variable value within either of those tasks -- inside an EG task.

A SAS Macro variable is a way to use a placeholder -- in the code that you type or submit for execution. So, for example, let's imagine that I have some REALLY long variable names, like:
[pre]
TABLE1_PURCHASE_PRICE_NEW
TABLE1_PURCHASE_PRICE_OLD
[/pre]

and, let's imagine, that I don't type very well, so I don't want to keep typing these long names over and over in my code. I can do this:
[pre]
%let tpp_new = TABLE1_PURCHASE_PRICE_NEW;
%let tpp_old = TABLE1_PURCHASE_PRICE_OLD;
[/pre]

Basically, the %LET is one way to create macro variables and assign them a value. So now that I've created those 2 macro variables to hold the values of the long names, I can do something like this in code (but NOT within an EG interactive task):
[pre]
proc print data=purchase;
var product type &tpp_new &tpp_old;
format &tpp_new &tpp_old dollar12.2;
run;

proc tabulate data=purchase;
class product;
var &tpp_new &tpp_old;
table product all,
sum* (&tpp_new &tpp_old);
run;
[/pre]

What I send to the compiler has the "symbolic" macro variable references, but then internally, the SAS Macro processor takes the code after it has been submitted and resolves the macro variable references into this code (for the proc print, to keep the posting short):
[pre]

proc print data=purchase;
var product type TABLE1_PURCHASE_PRICE_NEW TABLE1_PURCHASE_PRICE_OLD;
format TABLE1_PURCHASE_PRICE_NEW TABLE1_PURCHASE_PRICE_OLD dollar12.2;
run;

[/pre]

So at the simplest usage, the SAS Macro facility "types" (or substitutes) the long variable names for me and then sends the resolved code to the compiler (and then the compiled code is executed). The PROC SQL shown in the previous forum posting is a DIFFERENT way to create a macro variable (&CNTOBS) and assign it a value (the number of observations in SASHELP.CLASS).

You may find better help for your question from Tech Support, as they can look at your data and you can tell them what kind of output you want and they can help you figure out the best way to accomplish that task. Macro variables may NOT be the way to go. On the other hand, macro variables may be your solution.

cynthia

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 1 reply
  • 700 views
  • 0 likes
  • 2 in conversation