<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic referencing macro variables from within macros in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/referencing-macro-variables-from-within-macros/m-p/24248#M4054</link>
    <description>array time_interval{4}$ 10 ('month','month3','annual','ytd');&lt;BR /&gt;
do i=1 to 4;&lt;BR /&gt;
%get_calcs(time_interval{i});&lt;BR /&gt;
end;&lt;BR /&gt;
&lt;BR /&gt;
Hi all,&lt;BR /&gt;
&lt;BR /&gt;
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? &lt;BR /&gt;
&lt;BR /&gt;
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. &lt;BR /&gt;
&lt;BR /&gt;
&lt;B&gt;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?&lt;/B&gt;&lt;BR /&gt;
Thanks,&lt;BR /&gt;
Sachin</description>
    <pubDate>Wed, 27 Oct 2010 23:13:33 GMT</pubDate>
    <dc:creator>SachinRuk</dc:creator>
    <dc:date>2010-10-27T23:13:33Z</dc:date>
    <item>
      <title>referencing macro variables from within macros</title>
      <link>https://communities.sas.com/t5/SAS-Programming/referencing-macro-variables-from-within-macros/m-p/24248#M4054</link>
      <description>array time_interval{4}$ 10 ('month','month3','annual','ytd');&lt;BR /&gt;
do i=1 to 4;&lt;BR /&gt;
%get_calcs(time_interval{i});&lt;BR /&gt;
end;&lt;BR /&gt;
&lt;BR /&gt;
Hi all,&lt;BR /&gt;
&lt;BR /&gt;
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? &lt;BR /&gt;
&lt;BR /&gt;
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. &lt;BR /&gt;
&lt;BR /&gt;
&lt;B&gt;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?&lt;/B&gt;&lt;BR /&gt;
Thanks,&lt;BR /&gt;
Sachin</description>
      <pubDate>Wed, 27 Oct 2010 23:13:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/referencing-macro-variables-from-within-macros/m-p/24248#M4054</guid>
      <dc:creator>SachinRuk</dc:creator>
      <dc:date>2010-10-27T23:13:33Z</dc:date>
    </item>
    <item>
      <title>Re: referencing macro variables from within macros</title>
      <link>https://communities.sas.com/t5/SAS-Programming/referencing-macro-variables-from-within-macros/m-p/24249#M4055</link>
      <description>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.&lt;BR /&gt;
&lt;BR /&gt;
OPTIONS SOURCE SOURCE2 MGEN SGEN MPRINT /* MLOGIC */ ;&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Thu, 28 Oct 2010 02:22:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/referencing-macro-variables-from-within-macros/m-p/24249#M4055</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2010-10-28T02:22:25Z</dc:date>
    </item>
    <item>
      <title>Re: referencing macro variables from within macros</title>
      <link>https://communities.sas.com/t5/SAS-Programming/referencing-macro-variables-from-within-macros/m-p/24250#M4056</link>
      <description>Hi:&lt;BR /&gt;
  This latest question seems to be related to your other postings:&lt;BR /&gt;
&lt;A href="http://support.sas.com/forums/message.jspa?messageID=44850#44850" target="_blank"&gt;http://support.sas.com/forums/message.jspa?messageID=44850#44850&lt;/A&gt;&lt;BR /&gt;
&lt;A href="http://support.sas.com/forums/message.jspa?messageID=44847#44847" target="_blank"&gt;http://support.sas.com/forums/message.jspa?messageID=44847#44847&lt;/A&gt;&lt;BR /&gt;
 &lt;BR /&gt;
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.&lt;BR /&gt;
&lt;BR /&gt;
I've been reading your posts and it's still not clear to me what you are trying to achieve.&lt;BR /&gt;
            &lt;BR /&gt;
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:&lt;BR /&gt;
                           &lt;BR /&gt;
FAKE DATA:&lt;BR /&gt;
[pre]&lt;BR /&gt;
ID  Country Type  Date    actual_east predict_east sales_product discount_product&lt;BR /&gt;
001 CANADA   C   01Jan10       7585         7312        15553           .12&lt;BR /&gt;
002 CANADA   C   01Feb10       9581         9401        22485           .11&lt;BR /&gt;
003 GERMANY  C   01Jan10       9785         8771        21283           .11&lt;BR /&gt;
004 GERMANY  C   01Feb10       6302         6226        12333           .17&lt;BR /&gt;
005 USA      C   01Jan10       7916         7788        14926           .18&lt;BR /&gt;
006 USA      C   01Feb10       9615         8547        21433           .17&lt;BR /&gt;
[/pre]&lt;BR /&gt;
              &lt;BR /&gt;
MACRO PROGRAM to generate 1 assignment statement for each suffix:&lt;BR /&gt;
[pre]&lt;BR /&gt;
%macro makestmt(suff=);&lt;BR /&gt;
  %if %upcase(&amp;amp;suff) = EAST %then %do;&lt;BR /&gt;
    newvar_&amp;amp;suff = (actual_&amp;amp;suff - predict_&amp;amp;suff) / predict_&amp;amp;suff;&lt;BR /&gt;
  %end;&lt;BR /&gt;
  %else %if %upcase(&amp;amp;suff) = PRODUCT %then %do;&lt;BR /&gt;
    profit_&amp;amp;suff = sales_&amp;amp;suff * (1 - discount_&amp;amp;suff);&lt;BR /&gt;
  %end;&lt;BR /&gt;
%mend makestmt;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
                                        &lt;BR /&gt;
INVOKE the Macro Program in a DATA step program:&lt;BR /&gt;
[pre]&lt;BR /&gt;
options mprint=1;&lt;BR /&gt;
data newvar; &lt;BR /&gt;
 set fake_data;&lt;BR /&gt;
 %makestmt(suff=EAST) &lt;BR /&gt;
 %makestmt(suff=PRODUCT) &lt;BR /&gt;
run;&lt;BR /&gt;
                           &lt;BR /&gt;
proc print data=newvar;&lt;BR /&gt;
  title 'After using Macro to make new variables';&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
                                &lt;BR /&gt;
SAS Log from running the above program:&lt;BR /&gt;
[pre]&lt;BR /&gt;
1048  options mprint=1;&lt;BR /&gt;
1049  data newvar;&lt;BR /&gt;
1050   set fake_data;&lt;BR /&gt;
1051   %makestmt(suff=EAST)&lt;BR /&gt;
MPRINT(MAKESTMT):   newvar_EAST = (actual_EAST - predict_EAST) / predict_EAST;&lt;BR /&gt;
1052   %makestmt(suff=PRODUCT)&lt;BR /&gt;
MPRINT(MAKESTMT):   profit_PRODUCT = sales_PRODUCT * (1 - discount_PRODUCT);&lt;BR /&gt;
1053  run;&lt;BR /&gt;
                                      &lt;BR /&gt;
NOTE: There were 6 observations read from the data set WORK.FAKE_DATA.&lt;BR /&gt;
NOTE: The data set WORK.NEWVAR has 6 observations and 10 variables.&lt;BR /&gt;
NOTE: DATA statement used (Total process time):&lt;BR /&gt;
      real time           0.01 seconds&lt;BR /&gt;
      cpu time            0.00 seconds&lt;BR /&gt;
                            &lt;BR /&gt;
1054&lt;BR /&gt;
1055  proc print data=newvar;&lt;BR /&gt;
1056    title 'After using Macro to make new variables';&lt;BR /&gt;
1057  run;&lt;BR /&gt;
                       &lt;BR /&gt;
NOTE: There were 6 observations read from the data set WORK.NEWVAR.&lt;BR /&gt;
NOTE: PROCEDURE PRINT used (Total process time):&lt;BR /&gt;
      real time           0.00 seconds&lt;BR /&gt;
      cpu time            0.00 seconds&lt;BR /&gt;
[/pre]&lt;BR /&gt;
                                &lt;BR /&gt;
Notice the MPRINT results. This %IF in the macro program &lt;BR /&gt;
[pre]&lt;BR /&gt;
  %if %upcase(&amp;amp;suff) = EAST %then %do;&lt;BR /&gt;
    newvar_&amp;amp;suff = (actual_&amp;amp;suff - predict_&amp;amp;suff) / predict_&amp;amp;suff;&lt;BR /&gt;
  %end;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
                  &lt;BR /&gt;
resulted in this resolved statement &lt;BR /&gt;
being sent to the compiler when the value of the parameter &amp;amp;SUFF was set to EAST.&lt;BR /&gt;
[pre]&lt;BR /&gt;
newvar_EAST = (actual_EAST - predict_EAST) / predict_EAST;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
                       &lt;BR /&gt;
Here are some macro processing fundamentals that will bear more study and reading. &lt;BR /&gt;
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. &lt;BR /&gt;
&lt;A href="http://support.sas.com/kb/23/182.html" target="_blank"&gt;http://support.sas.com/kb/23/182.html&lt;/A&gt;&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a000210266.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a000210266.htm&lt;/A&gt;&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#tw3514-symput.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#tw3514-symput.htm&lt;/A&gt;&lt;BR /&gt;
&lt;BR /&gt;
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.&lt;BR /&gt;
&lt;A href="http://support.sas.com/kb/24/634.html" target="_blank"&gt;http://support.sas.com/kb/24/634.html&lt;/A&gt;&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a000543697.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a000543697.htm&lt;/A&gt;&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/lrdict/63026/HTML/default/viewer.htm#a000127810.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/lrdict/63026/HTML/default/viewer.htm#a000127810.htm&lt;/A&gt;&lt;BR /&gt;
 &lt;BR /&gt;
In comparison, my macro program creates 1 assignment statement WITHIN a single DATA step program when the passed parameter &amp;amp;SUFF is EAST and a different assignment statement when the passed parameter &amp;amp;SUFF is PRODUCT. Those assignment statements result in the creation of NEWVAR_EAST and PROFIT_PRODUCT variables in the new dataset. &lt;BR /&gt;
&lt;BR /&gt;
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.&lt;BR /&gt;
 &lt;BR /&gt;
cynthia</description>
      <pubDate>Thu, 28 Oct 2010 05:19:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/referencing-macro-variables-from-within-macros/m-p/24250#M4056</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2010-10-28T05:19:46Z</dc:date>
    </item>
  </channel>
</rss>

