BookmarkSubscribeRSS Feed
SachinRuk
Calcite | Level 5
array time_interval{4}$ 10 ('month','month3','annual','ytd');
do i=1 to 4;
%get_calcs(time_interval{i});
end;

Hi all,

In the abover code the macro get_calcs appends variables with month or whichever name you parse onto it (and it works when i send it when individually typed as month, month3 etc.). However, when I use the array it takes it to be the literal array name instead of value. How do I get arround this?

I do know that this has a lot to do with the fact that macros are invoked before the rest of the code. I just want a work around preferably using arrays.

I've used call execute but without success since it executes AFTER the data step where the variables are out of scope. does retain help the situation?
Thanks,
Sachin
2 REPLIES 2
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
You really need to share all of your SAS executed coded - consider this SAS statement to generate the most output that will be useful for your own desk-checking and also for the forum.

OPTIONS SOURCE SOURCE2 MGEN SGEN MPRINT /* MLOGIC */ ;

Scott Barry
SBBWorks, Inc.
Cynthia_sas
SAS Super FREQ
Hi:
This latest question seems to be related to your other postings:
http://support.sas.com/forums/message.jspa?messageID=44850#44850
http://support.sas.com/forums/message.jspa?messageID=44847#44847

It's not fair to folks to post bits and pieces of the code in other forum postings, instead of sticking with one post and providing complete information there. For example, the code for %get_calc is needed; the LOG messages are needed; some idea of the data is needed; and some idea of the big picture of what you're trying to achieve will all help other folks come up with constructive and meaningful suggestions.

I've been reading your posts and it's still not clear to me what you are trying to achieve.

However, consider this "fake data" and a macro program that uses a provided suffix in order to create different statements for the compiler, as shown in the SAS log. To simplify things, I removed the array reference and the DO loop -- as they are just an added layer of distraction from the basic point -- that the macro facility is only resolving code and sending the resolved code (without macro variable references) to the compiler. Assume the dataset created from this data is called WORK.FAKE_DATA:

FAKE DATA:
[pre]
ID Country Type Date actual_east predict_east sales_product discount_product
001 CANADA C 01Jan10 7585 7312 15553 .12
002 CANADA C 01Feb10 9581 9401 22485 .11
003 GERMANY C 01Jan10 9785 8771 21283 .11
004 GERMANY C 01Feb10 6302 6226 12333 .17
005 USA C 01Jan10 7916 7788 14926 .18
006 USA C 01Feb10 9615 8547 21433 .17
[/pre]

MACRO PROGRAM to generate 1 assignment statement for each suffix:
[pre]
%macro makestmt(suff=);
%if %upcase(&suff) = EAST %then %do;
newvar_&suff = (actual_&suff - predict_&suff) / predict_&suff;
%end;
%else %if %upcase(&suff) = PRODUCT %then %do;
profit_&suff = sales_&suff * (1 - discount_&suff);
%end;
%mend makestmt;
[/pre]

INVOKE the Macro Program in a DATA step program:
[pre]
options mprint=1;
data newvar;
set fake_data;
%makestmt(suff=EAST)
%makestmt(suff=PRODUCT)
run;

proc print data=newvar;
title 'After using Macro to make new variables';
run;
[/pre]

SAS Log from running the above program:
[pre]
1048 options mprint=1;
1049 data newvar;
1050 set fake_data;
1051 %makestmt(suff=EAST)
MPRINT(MAKESTMT): newvar_EAST = (actual_EAST - predict_EAST) / predict_EAST;
1052 %makestmt(suff=PRODUCT)
MPRINT(MAKESTMT): profit_PRODUCT = sales_PRODUCT * (1 - discount_PRODUCT);
1053 run;

NOTE: There were 6 observations read from the data set WORK.FAKE_DATA.
NOTE: The data set WORK.NEWVAR has 6 observations and 10 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.00 seconds

1054
1055 proc print data=newvar;
1056 title 'After using Macro to make new variables';
1057 run;

NOTE: There were 6 observations read from the data set WORK.NEWVAR.
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
[/pre]

Notice the MPRINT results. This %IF in the macro program
[pre]
%if %upcase(&suff) = EAST %then %do;
newvar_&suff = (actual_&suff - predict_&suff) / predict_&suff;
%end;
[/pre]

resulted in this resolved statement
being sent to the compiler when the value of the parameter &SUFF was set to EAST.
[pre]
newvar_EAST = (actual_EAST - predict_EAST) / predict_EAST;
[/pre]

Here are some macro processing fundamentals that will bear more study and reading.
1) You cannot use a macro variable created with CALL SYMPUT in the same program in which the macro variable is created. A step boundary must occur before you reference the macro variable.
http://support.sas.com/kb/23/182.html
http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a000210266.htm
http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#tw3514-symput.htm

2) Also, when you use CALL EXECUTE, any statements that you generate will be submitted AFTER the step boundary for the program in which the CALL EXECUTE string or macro call is created. Therefore, any code you submit or macro program you invoke, must generate syntactically correct SAS code. An assignment statement cannot "live" outside of a DATA step program -- so therefore, your macro would need to generate a syntactically correct SAS program.
http://support.sas.com/kb/24/634.html
http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a000543697.htm
http://support.sas.com/documentation/cdl/en/lrdict/63026/HTML/default/viewer.htm#a000127810.htm

In comparison, my macro program creates 1 assignment statement WITHIN a single DATA step program when the passed parameter &SUFF is EAST and a different assignment statement when the passed parameter &SUFF is PRODUCT. Those assignment statements result in the creation of NEWVAR_EAST and PROFIT_PRODUCT variables in the new dataset.

Studying a bit more on how the SAS Macro Facility operates may help you redesign your process. However, to get the most help from the forum, you can explain a bit more about what you want to achieve and keep all your posts in one place so folks don't have to bounce around to follow the thread.

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!

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