<?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 Re: Assigning a macro variable inside a loop. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-variable-inside-a-loop/m-p/24030#M4027</link>
    <description>You are trying to create a variable in the middle of a datastep.   Have you looked at using your macro to set up your data structure prior to reading the data?  In this simple example I am using an array to create the variables prior to reading the data.&lt;BR /&gt;
&lt;BR /&gt;
%let p = 5;&lt;BR /&gt;
data one;&lt;BR /&gt;
array avar(*) var_&amp;amp;p - var_9 ;&lt;BR /&gt;
do j = &amp;amp;p to 9;&lt;BR /&gt;
  avar(j-&amp;amp;p+1) = J + 7;&lt;BR /&gt;
end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
Since I have the limits of the array, I am able to set up my data structure then use the datastep loop to assign them values.</description>
    <pubDate>Wed, 02 Dec 2009 13:26:02 GMT</pubDate>
    <dc:creator>Flip</dc:creator>
    <dc:date>2009-12-02T13:26:02Z</dc:date>
    <item>
      <title>Assigning a macro variable inside a loop.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-variable-inside-a-loop/m-p/24012#M4009</link>
      <description>Part of my program:&lt;BR /&gt;
.&lt;BR /&gt;
SOMETHING&lt;BR /&gt;
.&lt;BR /&gt;
do i=&amp;amp;m to 10;&lt;BR /&gt;
%let j=%eval(&amp;amp;m+i);&lt;BR /&gt;
.&lt;BR /&gt;
SOMETHING&lt;BR /&gt;
.&lt;BR /&gt;
end;&lt;BR /&gt;
&lt;BR /&gt;
Error message: A character operand was found in the %EVAL function ...&lt;BR /&gt;
The condition was: 1+i&lt;BR /&gt;
&lt;BR /&gt;
(&amp;amp;m happens to be 1 the first time)&lt;BR /&gt;
&lt;BR /&gt;
I would like to get j=2,3,...,11 as a macro variable which I use later in the loop.&lt;BR /&gt;
&lt;BR /&gt;
How can this be solved?</description>
      <pubDate>Tue, 01 Dec 2009 08:42:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-variable-inside-a-loop/m-p/24012#M4009</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-12-01T08:42:35Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning a macro variable inside a loop.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-variable-inside-a-loop/m-p/24013#M4010</link>
      <description>%let j=%eval(&amp;amp;m+&amp;amp;i);</description>
      <pubDate>Tue, 01 Dec 2009 09:48:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-variable-inside-a-loop/m-p/24013#M4010</guid>
      <dc:creator>SAS_user</dc:creator>
      <dc:date>2009-12-01T09:48:33Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning a macro variable inside a loop.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-variable-inside-a-loop/m-p/24014#M4011</link>
      <description>That doesn't work.&lt;BR /&gt;
&lt;BR /&gt;
&lt;B&gt;i&lt;/B&gt; isn't a macro variable.</description>
      <pubDate>Tue, 01 Dec 2009 10:22:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-variable-inside-a-loop/m-p/24014#M4011</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-12-01T10:22:00Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning a macro variable inside a loop.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-variable-inside-a-loop/m-p/24015#M4012</link>
      <description>You would need to give us more of your code, but it appears you are mixing compile time macro resolution with datastep values.  This will not work in the context you are presenting.&lt;BR /&gt;
Give us a better idea what you are trying to do and we may be able to help.&lt;BR /&gt;
&lt;BR /&gt;
You could for instance calculate a new value such as &lt;BR /&gt;
x = &amp;amp;m + i;&lt;BR /&gt;
call symput('j' , put(x best.))</description>
      <pubDate>Tue, 01 Dec 2009 12:58:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-variable-inside-a-loop/m-p/24015#M4012</guid>
      <dc:creator>Flip</dc:creator>
      <dc:date>2009-12-01T12:58:05Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning a macro variable inside a loop.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-variable-inside-a-loop/m-p/24016#M4013</link>
      <description>sorry i didn't mention it was not a macro loop.&lt;BR /&gt;
&lt;BR /&gt;
Flip is right, you can not create a new macro variable in datastep when trying to use %let statement.</description>
      <pubDate>Tue, 01 Dec 2009 13:37:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-variable-inside-a-loop/m-p/24016#M4013</guid>
      <dc:creator>SAS_user</dc:creator>
      <dc:date>2009-12-01T13:37:23Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning a macro variable inside a loop.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-variable-inside-a-loop/m-p/24017#M4014</link>
      <description>Useful reading on the subject:&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
SAS Macro Language: Reference, Scopes of Macro Variables&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/a002047080.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/a002047080.htm&lt;/A&gt;&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Tue, 01 Dec 2009 13:46:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-variable-inside-a-loop/m-p/24017#M4014</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2009-12-01T13:46:14Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning a macro variable inside a loop.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-variable-inside-a-loop/m-p/24018#M4015</link>
      <description>Flip&lt;BR /&gt;
&lt;BR /&gt;
With your:&lt;BR /&gt;
x = &amp;amp;m + i;&lt;BR /&gt;
call symput('j' , put(x, best.)); &lt;BR /&gt;
&lt;BR /&gt;
I tried with the expression: p=%eval(&amp;amp;j - 5);&lt;BR /&gt;
&lt;BR /&gt;
But I get the same type of error message: A character operand was found ...&lt;BR /&gt;
The condition was: &amp;amp;j -5&lt;BR /&gt;
&lt;BR /&gt;
The code I have been referring to above was inside a macro, looking something like this:&lt;BR /&gt;
&lt;BR /&gt;
%macro mymacro;&lt;BR /&gt;
%do i=1 %to 10;&lt;BR /&gt;
data newset&amp;amp;i;&lt;BR /&gt;
set oldset&amp;amp;i;&lt;BR /&gt;
&lt;BR /&gt;
&lt;I&gt;code referred to&lt;/I&gt;&lt;BR /&gt;
&lt;BR /&gt;
run;&lt;BR /&gt;
%end;&lt;BR /&gt;
%mend;&lt;BR /&gt;
%mymacro;&lt;BR /&gt;
&lt;BR /&gt;
Why is p=%eval(&amp;amp;j - 5); invalid?</description>
      <pubDate>Tue, 01 Dec 2009 19:11:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-variable-inside-a-loop/m-p/24018#M4015</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-12-01T19:11:41Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning a macro variable inside a loop.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-variable-inside-a-loop/m-p/24019#M4016</link>
      <description>You should be able to diagnose the &amp;amp;J value with the following SAS OPTIONS set:&lt;BR /&gt;
&lt;BR /&gt;
OPTIONS SOURCE SOURCE2 MACROGEN SYMBOLGEN MLOGIC;&lt;BR /&gt;
&lt;BR /&gt;
SAS macro variable &amp;amp;J is going to have a value *AFTER* the DATA step execution, not during it.&lt;BR /&gt;
&lt;BR /&gt;
Moreover, I would encourage you to reveal the SAS code you represent as "code referred to" -- just to ensure that it is all DATA step code and not inconsistent macro language.&lt;BR /&gt;
&lt;BR /&gt;
Hopefully you have read up on the suggested SCOPING documentation for important relevance?&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Tue, 01 Dec 2009 19:31:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-variable-inside-a-loop/m-p/24019#M4016</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2009-12-01T19:31:09Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning a macro variable inside a loop.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-variable-inside-a-loop/m-p/24020#M4017</link>
      <description>The statement should work within a datastep providing that &amp;amp;j resolves to an integer.  Put an statement %put &amp;amp;j ; right before the data step.  That should tell you if it is resolving correctly.  Do not expect to be able to assign and resolve a macro variable within a single datastep.&lt;BR /&gt;
You may have better luck using dataset variables for these counters and assigning the macro variables conditionally from those.&lt;BR /&gt;
Is there a reason for updating the macro variables at each itteration of your loop?  Only the last loop will be assigned at the end of the datastep.&lt;BR /&gt;
&lt;BR /&gt;
I see from your first post that this is exactly what you are doing.  Macros don't work that way.

Message was edited by: Flip</description>
      <pubDate>Tue, 01 Dec 2009 19:37:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-variable-inside-a-loop/m-p/24020#M4017</guid>
      <dc:creator>Flip</dc:creator>
      <dc:date>2009-12-01T19:37:13Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning a macro variable inside a loop.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-variable-inside-a-loop/m-p/24021#M4018</link>
      <description>Hi:&lt;BR /&gt;
  When you use a %DO loop, your loop index becomes a MACRO variable. So in this case, any references to i should be &amp;amp;i; &lt;BR /&gt;
&lt;BR /&gt;
  However, as it says here (and in the documenation):&lt;BR /&gt;
&lt;A href="http://support.sas.com/kb/22/987.html" target="_blank"&gt;http://support.sas.com/kb/22/987.html&lt;/A&gt;&lt;BR /&gt;
&lt;B&gt; &lt;BR /&gt;
-You cannot use a MACRO variable reference to retrieve the value of a MACRO&lt;BR /&gt;
variable in the same program (or step) in which SYMPUT creates that MACRO&lt;BR /&gt;
variable and assigns it a value.&lt;BR /&gt;
-You must explicitly use a step boundary statement to force the DATA Step to&lt;BR /&gt;
execute before referencing the MACRO variable that is created with SYMPUT. The boundary&lt;BR /&gt;
could be a RUN statement or another DATA or PROC statement.&lt;BR /&gt;
&lt;/B&gt;&lt;BR /&gt;
 &lt;BR /&gt;
This means that you CANNOT have the creation of &amp;amp;J and the use of &amp;amp;J in the same DATA step program.&lt;BR /&gt;
 &lt;BR /&gt;
This documentation is quite useful:&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/tw3514-symput.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/tw3514-symput.htm&lt;/A&gt;&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/a000210266.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/a000210266.htm&lt;/A&gt;&lt;BR /&gt;
 &lt;BR /&gt;
It still is not clear to me what your exact process needs to be. However, consider the program below, which shows 2 new (dataset) variables being created from &amp;amp;I and &amp;amp;J (which are created OUTSIDE the Data step program...&amp;amp;I is created in the %DO loop and &amp;amp;J is created with a %LET). Note how the global macro variables &amp;amp;NEW_I and &amp;amp;NEW_J are available AFTER the Data step program and AFTER the macro program. They would NOT have been available INSIDE the Data step program. Note that you will see the DATASET variables in the PROC PRINT output and you will see the MACRO variables in the TITLE statement and in the %PUT results in the SAS log.&lt;BR /&gt;
 &lt;BR /&gt;
cynthia&lt;BR /&gt;
[pre]&lt;BR /&gt;
                            &lt;BR /&gt;
%macro mymacro;&lt;BR /&gt;
%global new_i new_j;&lt;BR /&gt;
                            &lt;BR /&gt;
%do i=1 %to 3;&lt;BR /&gt;
                        &lt;BR /&gt;
%let j=%eval(&amp;amp;i + 10);&lt;BR /&gt;
                                 &lt;BR /&gt;
** because macro vars I and J are set -outside- the DATA step;&lt;BR /&gt;
** they can be referenced -inside- the program;&lt;BR /&gt;
                                     &lt;BR /&gt;
  data newset&amp;amp;i;&lt;BR /&gt;
     set sashelp.class;&lt;BR /&gt;
     if _n_ = &amp;amp;i then do;&lt;BR /&gt;
       newvar_i = &amp;amp;i + 10;&lt;BR /&gt;
       newvar_j = &amp;amp;j -10;&lt;BR /&gt;
       call symput('new_i',put(newvar_i,2.0));&lt;BR /&gt;
       call symput('new_j',put(newvar_j,2.0));&lt;BR /&gt;
       output;&lt;BR /&gt;
     end;&lt;BR /&gt;
  run;&lt;BR /&gt;
                      &lt;BR /&gt;
  ** because there is a step boundary after the creation of;&lt;BR /&gt;
  ** new_i and new_j macro variables, you can use them;&lt;BR /&gt;
  ** after the program is over. Also, because they are GLOBAL;&lt;BR /&gt;
  ** macro variables, they are available after the macro stops;&lt;BR /&gt;
  ** executing;&lt;BR /&gt;
                         &lt;BR /&gt;
  ods listing;&lt;BR /&gt;
  proc print data=newset&amp;amp;i;&lt;BR /&gt;
    title "Value of I=&amp;amp;i Value of J=&amp;amp;j at program START";&lt;BR /&gt;
    title2 "NEWVAR_I is &amp;amp;i + 10 = %eval(&amp;amp;i+10)";&lt;BR /&gt;
    title3 "NEWVAR_J is &amp;amp;j - 10 = %eval(&amp;amp;j-10)";&lt;BR /&gt;
    title4 "Inside Macro: new_i = &amp;amp;new_i and new_j = &amp;amp;new_j";&lt;BR /&gt;
  run;&lt;BR /&gt;
                                   &lt;BR /&gt;
%end;&lt;BR /&gt;
%mend mymacro;&lt;BR /&gt;
                                       &lt;BR /&gt;
%mymacro;&lt;BR /&gt;
           &lt;BR /&gt;
%put ************* Look in Log ****************;&lt;BR /&gt;
%put Outside Macro: new_i = &amp;amp;new_i and new_j = &amp;amp;new_j;&lt;BR /&gt;
[/pre]</description>
      <pubDate>Tue, 01 Dec 2009 19:51:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-variable-inside-a-loop/m-p/24021#M4018</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2009-12-01T19:51:06Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning a macro variable inside a loop.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-variable-inside-a-loop/m-p/24022#M4019</link>
      <description>%eval is confused by one macro variable &amp;amp;m and one SAS dataset variable i.&lt;BR /&gt;
&lt;BR /&gt;
the easiest way out is not to use %eval:&lt;BR /&gt;
&lt;BR /&gt;
%let j=&amp;amp;m+i;</description>
      <pubDate>Tue, 01 Dec 2009 20:40:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-variable-inside-a-loop/m-p/24022#M4019</guid>
      <dc:creator>abdullala</dc:creator>
      <dc:date>2009-12-01T20:40:56Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning a macro variable inside a loop.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-variable-inside-a-loop/m-p/24023#M4020</link>
      <description>You Go Cynthia!</description>
      <pubDate>Tue, 01 Dec 2009 20:57:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-variable-inside-a-loop/m-p/24023#M4020</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2009-12-01T20:57:56Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning a macro variable inside a loop.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-variable-inside-a-loop/m-p/24024#M4021</link>
      <description>I believe that you can't assign a macro variable a value with a data set variable, because the macro statements will be compiled to generate open code value or statements before the data set statements were executed.&lt;BR /&gt;
So p=&amp;amp;m+i will work, and after complied, this will  be converted to a open code statement with a real value of macro variable m, ie. p=3+i;</description>
      <pubDate>Tue, 01 Dec 2009 20:57:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-variable-inside-a-loop/m-p/24024#M4021</guid>
      <dc:creator>SUN59338</dc:creator>
      <dc:date>2009-12-01T20:57:57Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning a macro variable inside a loop.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-variable-inside-a-loop/m-p/24025#M4022</link>
      <description>The OP error is due to the fact that the %eval is compile time.  At that point i  is 'i' not the value of the datastep variable i.&lt;BR /&gt;
&lt;BR /&gt;
It leads to other confusion of using macro variables for program flow within a datastep when they are  not needed.  It is much better to use datastep variables to control loops within the datastep whenever possible.   My feeling is that this all could be done without the use of macros, except for the repitition of the datastep itself.</description>
      <pubDate>Tue, 01 Dec 2009 20:58:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-variable-inside-a-loop/m-p/24025#M4022</guid>
      <dc:creator>Flip</dc:creator>
      <dc:date>2009-12-01T20:58:48Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning a macro variable inside a loop.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-variable-inside-a-loop/m-p/24026#M4023</link>
      <description>My macro looked something like this:&lt;BR /&gt;
&lt;BR /&gt;
%macro mymacro;&lt;BR /&gt;
%do i=1 %to 10;&lt;BR /&gt;
data newset&amp;amp;i;&lt;BR /&gt;
set oldset&amp;amp;i;&lt;BR /&gt;
&lt;BR /&gt;
code referred to&lt;BR /&gt;
&lt;BR /&gt;
run;&lt;BR /&gt;
%end;&lt;BR /&gt;
%mend;&lt;BR /&gt;
%mymacro;&lt;BR /&gt;
&lt;BR /&gt;
After "code referred to" I wanted to name (and assign values to) some variables:&lt;BR /&gt;
&lt;BR /&gt;
For the naming I tried with Var%eval(&amp;amp;k+1), where k depended upon &amp;amp;i and another variable j (not macro variabel), which in turn depended upon &amp;amp;i in certain and differing ways.&lt;BR /&gt;
&lt;BR /&gt;
The important thing to me is the naming of the variables. I have checked every step in my pseudo code algorithm, and it is correct. It's the translation into SAS code that's difficult. In an ordinary programming language it had been easy to accomplish this.&lt;BR /&gt;
&lt;BR /&gt;
SYMGET can't do me any good I think. It seems as if it get the values from the macro variable. But I want to put a  number STRING after Var.&lt;BR /&gt;
&lt;BR /&gt;
I have a lot to read now.</description>
      <pubDate>Tue, 01 Dec 2009 21:02:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-variable-inside-a-loop/m-p/24026#M4023</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-12-01T21:02:54Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning a macro variable inside a loop.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-variable-inside-a-loop/m-p/24027#M4024</link>
      <description>Hi:&lt;BR /&gt;
  Here's my .02 on your statement that: &lt;B&gt;&lt;BR /&gt;
The important thing to me is the naming of the variables. I have checked every step in my pseudo code algorithm, and it is correct. It's the translation into SAS code that's difficult. In an ordinary programming language it had been easy to accomplish this.&lt;BR /&gt;
&lt;/B&gt;&lt;BR /&gt;
 &lt;BR /&gt;
When you are creating a SAS macro program to accomplish something, you CANNOT work from pseudo code unless you have TONS and TONS of SAS macro experience.&lt;BR /&gt;
&lt;BR /&gt;
The recommended steps are:&lt;BR /&gt;
1) have a WORKING SAS program -- without ANY macro variable references AT all. If your WORKING SAS program needs to have hardcoded values, then write and test a WORKING program to make sure that your program generates at least 1 of your desired results. (for example, if you need to create 100 datasets, create a working SAS program with hard-coded logic to create the 1st dataset; then generate a second SAS program with hard-coded logic to create the 2nd dataset.) Somewhere along the way between writing program 1 and changing program 1 to become program 2, you will identify all the "mechanical" changes (need to increment a counter here) or all the opportunities to use a macro variable value.&lt;BR /&gt;
[pre]             &lt;BR /&gt;
** step 1 -- have a working program or programs;&lt;BR /&gt;
data make_s1;&lt;BR /&gt;
  set sashelp.class;&lt;BR /&gt;
  var1 = age + 1;&lt;BR /&gt;
  var2 = weight /(height * height);&lt;BR /&gt;
run;&lt;BR /&gt;
                 &lt;BR /&gt;
proc print data=make_s1;&lt;BR /&gt;
  title 'Step 1: Have a Working SAS program';&lt;BR /&gt;
  title2 'First time create VAR1 and VAR2';&lt;BR /&gt;
run;&lt;BR /&gt;
                   &lt;BR /&gt;
data make_s2;&lt;BR /&gt;
  set sashelp.class;&lt;BR /&gt;
  var2 = age + 2;&lt;BR /&gt;
  var4 = weight /(height * height);&lt;BR /&gt;
run;&lt;BR /&gt;
              &lt;BR /&gt;
proc print data=make_s2;&lt;BR /&gt;
  title 'Step 1: Have a Working SAS program';&lt;BR /&gt;
  title2 'Second time create VAR2 and VAR4';&lt;BR /&gt;
run;&lt;BR /&gt;
               &lt;BR /&gt;
data make_s3;&lt;BR /&gt;
  set sashelp.class;&lt;BR /&gt;
  var3 = age + 3;&lt;BR /&gt;
  var5 = weight /(height * height);&lt;BR /&gt;
run;&lt;BR /&gt;
               &lt;BR /&gt;
proc print data=make_s3;&lt;BR /&gt;
  title 'Step 1: Have a Working SAS program';&lt;BR /&gt;
  title2 'Third time create VAR3 and VAR5';&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
                          &lt;BR /&gt;
                        &lt;BR /&gt;
2) Now, take program #1 and create a macro variables with %LET and then use the macro variables in your program. You are NOT writing a macro program at this point -- you are just verifying that you identified the places in your program where macro variables could be used in the code. Now, test this somewhat generic program with the %LET statements that you need to produce the 1st data set -- if everything works as expected, now change the %LET statements in order to produce the 2nd data set. If everything works as expected, now make a macro program. &lt;BR /&gt;
                    &lt;BR /&gt;
So in the program sample below, the first program creates a dataset called makevar_s1 where &amp;amp;X=1 and &amp;amp;Y=2; then the second program creates a dataset called makevar_s2 where &amp;amp;X=2 and &amp;amp;Y=4, and then the third program creates a dataset called makevar_s3 where &amp;amp;X=3 and &amp;amp;Y=5 ...and this pattern will repeat for all my other programs/datasets: the value of &amp;amp;Y will be equal to &amp;amp;X + 2 -- except for the first time. So this is the working code that I have now, with these data step programs and hard-coded %LET statements:&lt;BR /&gt;
[pre]&lt;BR /&gt;
                                       &lt;BR /&gt;
** step 2 -- use %LET to create var1 and var2;&lt;BR /&gt;
options mprint symbolgen;&lt;BR /&gt;
%let x = 1;&lt;BR /&gt;
%let y = 2;&lt;BR /&gt;
                         &lt;BR /&gt;
data makevar_s&amp;amp;x;&lt;BR /&gt;
  set sashelp.class;&lt;BR /&gt;
  var&amp;amp;x = age + &amp;amp;x;&lt;BR /&gt;
  var&amp;amp;y = weight /(height * height);&lt;BR /&gt;
run;&lt;BR /&gt;
                 &lt;BR /&gt;
proc print data=makevar_s&amp;amp;x;&lt;BR /&gt;
  title 'Step 2: use macro variables set with %LET';&lt;BR /&gt;
  title2 "Dataset is: makevar_s&amp;amp;x X=&amp;amp;X Y=&amp;amp;Y";&lt;BR /&gt;
run;&lt;BR /&gt;
                      &lt;BR /&gt;
** step 2a -- use %LET to create var2 and var4;&lt;BR /&gt;
options mprint symbolgen;&lt;BR /&gt;
%let x = 2;&lt;BR /&gt;
%let y = 4;&lt;BR /&gt;
                    &lt;BR /&gt;
data makevar_s&amp;amp;x;&lt;BR /&gt;
  set sashelp.class;&lt;BR /&gt;
  var&amp;amp;x = age + &amp;amp;x;&lt;BR /&gt;
  var&amp;amp;y = weight /(height * height);&lt;BR /&gt;
run;&lt;BR /&gt;
                     &lt;BR /&gt;
proc print data=makevar_s&amp;amp;x;&lt;BR /&gt;
  title 'Step 2a: use macro variables set with %LET and diff values';&lt;BR /&gt;
  title2 "Dataset is: makevar_s&amp;amp;x X=&amp;amp;X Y=&amp;amp;Y";&lt;BR /&gt;
run;&lt;BR /&gt;
                  &lt;BR /&gt;
** step 2a -- use %LET with different values and see if;&lt;BR /&gt;
**            program still works. This time create the;&lt;BR /&gt;
**            variables var3 and var5.;&lt;BR /&gt;
options mprint symbolgen;&lt;BR /&gt;
%let x = 3;&lt;BR /&gt;
%let y = 5;&lt;BR /&gt;
                    &lt;BR /&gt;
data makevar_s&amp;amp;x;&lt;BR /&gt;
  set sashelp.class;&lt;BR /&gt;
  var&amp;amp;x = age + &amp;amp;x;&lt;BR /&gt;
  var&amp;amp;y = weight /(height * height);&lt;BR /&gt;
run;&lt;BR /&gt;
               &lt;BR /&gt;
proc print data=makevar_s&amp;amp;x;&lt;BR /&gt;
  title 'Step 2a: use macro variables set with %LET and diff values';&lt;BR /&gt;
  title2 "Dataset is: makevar_s&amp;amp;x X=&amp;amp;X Y=&amp;amp;Y";&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
                                                &lt;BR /&gt;
3) Turn your program with macro variable references into a macro -program- (between %MACRO/%MEND). Now and -only- now do you put in the %IF and %DO logic. If you have tested your program (not pseudo code) at every step along the way, your debugging during this step should not be too burdensome. If you go straight to step 3, without understanding how to make the transition between step 1 and step 2 and then between step 2 and step 3 -- your macro program will be prone to errors, partly because you did not start with a WORKING program and did not understand which pieces of your process belonged to DATA step and procedure code and which pieces of your process belonged to the macro program. For example, in the macro program below, on the first time through the %DO loop, if &amp;amp;X = 1, then &amp;amp;Y is set to 2...for all other iterations, &amp;amp;Y is set to be equal to &amp;amp;X + 2.&lt;BR /&gt;
                                                     &lt;BR /&gt;
[pre]&lt;BR /&gt;
** step 3: NOW make and test a macro program;&lt;BR /&gt;
** when x = 1, create variables var1 and var2 (use %IF);&lt;BR /&gt;
** when x = 2, create variables var2 and var4 (&amp;amp;y = &amp;amp;x + 2);&lt;BR /&gt;
** when x = 3, create variables var3 and var5 (&amp;amp;y = &amp;amp;x + 2);&lt;BR /&gt;
** The macro logic is that inside the %DO loop, &amp;amp;X will be;&lt;BR /&gt;
** incremented by 2 to create &amp;amp;Y -- for every iteration except&lt;BR /&gt;
** when &amp;amp;X = 1.;&lt;BR /&gt;
                                      &lt;BR /&gt;
%macro calcvars;&lt;BR /&gt;
options mprint symbolgen;&lt;BR /&gt;
%do x = 1 %to 3;&lt;BR /&gt;
%let y = %eval(&amp;amp;x + 2);&lt;BR /&gt;
                              &lt;BR /&gt;
*** The first time thru the loop, want &amp;amp;Y to be 2 (not 3);&lt;BR /&gt;
%if &amp;amp;x = 1 %then %let y = 2;&lt;BR /&gt;
                      &lt;BR /&gt;
data makevar_s&amp;amp;x;&lt;BR /&gt;
  set sashelp.class;&lt;BR /&gt;
  var&amp;amp;x = age + &amp;amp;x;&lt;BR /&gt;
  var&amp;amp;y = weight /(height * height);&lt;BR /&gt;
run;&lt;BR /&gt;
                       &lt;BR /&gt;
ods listing;&lt;BR /&gt;
proc print data=makevar_s&amp;amp;x;&lt;BR /&gt;
  title "Step 3: Value for X = &amp;amp;X and Y = &amp;amp;Y";&lt;BR /&gt;
  title2 "Dataset is: makevar_s&amp;amp;x";&lt;BR /&gt;
run;&lt;BR /&gt;
%end;&lt;BR /&gt;
                        &lt;BR /&gt;
%mend calcvars;&lt;BR /&gt;
                  &lt;BR /&gt;
%calcvars;&lt;BR /&gt;
                                &lt;BR /&gt;
[/pre]&lt;BR /&gt;
                 &lt;BR /&gt;
If you examine the output from the above examples, the macro program has the %DO loop and the %IF logic added. This is my never-fail method for writing a complex macro program that doesn't have endless debugging issues. If I start with a WORKING program and not pseudo code, I always have better success than if I jump straight to step 3. That's just my experience. &lt;BR /&gt;
                       &lt;BR /&gt;
The most important thing to remember is that the SAS Macro facility is acting like a big, dumb typewriter. In the above programs, every time the Macro word scanner sees "&amp;amp;X", it will type whatever the current value of &amp;amp;X is into the program code that is being generated. &lt;BR /&gt;
                                           &lt;BR /&gt;
The Macro facility does not "compile" or "execute" any code. It only "resolves" code and then the resolved code is sent to the compiler. That's why it's important to know what your WORKING program needs to look like -- because you want your macro program to generate that same WORKING program or programs for you. When a Macro PROGRAM executes, it is ONLY generating code based on the %DO loop(s) or %IF statement(s) in your MACRO program. the generated code is then sent to the compiler and, if free of compile errors, will be executed. By the time your code gets to the SAS compiler, there are no macro variable references or %DO loops or %IF logic. The links previously posted will enlighten you on the issues of macro variable scope and whether macro variables are LOCAL or GLOBAL in scope, however, even these issues only apply to the way that resolved code is generated.&lt;BR /&gt;
 &lt;BR /&gt;
cynthia</description>
      <pubDate>Tue, 01 Dec 2009 23:15:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-variable-inside-a-loop/m-p/24027#M4024</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2009-12-01T23:15:36Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning a macro variable inside a loop.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-variable-inside-a-loop/m-p/24028#M4025</link>
      <description>Cynthia&lt;BR /&gt;
&lt;BR /&gt;
Thank you for your detailed answer.&lt;BR /&gt;
&lt;BR /&gt;
I could move assignments of a macro variable outside the data steps:&lt;BR /&gt;
&lt;BR /&gt;
%if &amp;amp;i le 5 %then &amp;amp;p=1;&lt;BR /&gt;
%else &amp;amp;p=&amp;amp;i-1;&lt;BR /&gt;
&lt;BR /&gt;
BUT inside the data steps I must have the counter j in a do loop:&lt;BR /&gt;
&lt;BR /&gt;
do j=&amp;amp;p to 9;&lt;BR /&gt;
&lt;BR /&gt;
some code&lt;BR /&gt;
&lt;BR /&gt;
naming Var (and assigning values)&lt;BR /&gt;
&lt;BR /&gt;
end;&lt;BR /&gt;
&lt;BR /&gt;
And it is the j that should influence the the names on my variables  Var_(&lt;BR /&gt;
 &lt;I&gt;depending on j&lt;/I&gt;).&lt;BR /&gt;
&lt;BR /&gt;
I am not sure if the following is allowed inside the datasteps and without risk of spoiling something:&lt;BR /&gt;
&lt;BR /&gt;
%do j=&amp;amp;p %to 9;&lt;BR /&gt;
&lt;BR /&gt;
some code&lt;BR /&gt;
&lt;BR /&gt;
naming Var (and assigning values)&lt;BR /&gt;
&lt;BR /&gt;
%end;&lt;BR /&gt;
&lt;BR /&gt;
If allowed, then my problem would be solved.</description>
      <pubDate>Wed, 02 Dec 2009 10:43:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-variable-inside-a-loop/m-p/24028#M4025</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-12-02T10:43:11Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning a macro variable inside a loop.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-variable-inside-a-loop/m-p/24029#M4026</link>
      <description>Honestly, you need to read and digest the SCOPING discussion for macro variables -- it appears that you still do not get it -- the line of code below will not change with a DATA step execution:&lt;BR /&gt;
&lt;BR /&gt;
call symput('j' , put(x, best.)); &lt;BR /&gt;
&lt;BR /&gt;
You will see the SAS compilation activity if you turn on the diagnostics as suggested earlier in this thread.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Wed, 02 Dec 2009 11:04:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-variable-inside-a-loop/m-p/24029#M4026</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2009-12-02T11:04:20Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning a macro variable inside a loop.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-variable-inside-a-loop/m-p/24030#M4027</link>
      <description>You are trying to create a variable in the middle of a datastep.   Have you looked at using your macro to set up your data structure prior to reading the data?  In this simple example I am using an array to create the variables prior to reading the data.&lt;BR /&gt;
&lt;BR /&gt;
%let p = 5;&lt;BR /&gt;
data one;&lt;BR /&gt;
array avar(*) var_&amp;amp;p - var_9 ;&lt;BR /&gt;
do j = &amp;amp;p to 9;&lt;BR /&gt;
  avar(j-&amp;amp;p+1) = J + 7;&lt;BR /&gt;
end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
Since I have the limits of the array, I am able to set up my data structure then use the datastep loop to assign them values.</description>
      <pubDate>Wed, 02 Dec 2009 13:26:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-variable-inside-a-loop/m-p/24030#M4027</guid>
      <dc:creator>Flip</dc:creator>
      <dc:date>2009-12-02T13:26:02Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning a macro variable inside a loop.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-variable-inside-a-loop/m-p/24031#M4028</link>
      <description>Scott Barry&lt;BR /&gt;
&lt;BR /&gt;
Your wording: "the &lt;B&gt;line of code &lt;/B&gt; below will not change with a DATA step execution".&lt;BR /&gt;
&lt;BR /&gt;
Can you explain what you really want to say. Do you mean that the line of code will have no effect or what?&lt;BR /&gt;
&lt;BR /&gt;
And I have been reading some documentation ...&lt;BR /&gt;
&lt;BR /&gt;
For instance: "You can create global macro variables any time during a SAS session or job. Except for some automatic macro variables, you can change the values of global macro variables any time during a SAS session or job."&lt;BR /&gt;
&lt;BR /&gt;
If I declared a macro variabel n outside the data steps with %GLOBAL n;,&lt;BR /&gt;
wouldn't it  be possibel to assign the counter j inside the data steps to n, and then name my variables Var_&amp;amp;n?</description>
      <pubDate>Wed, 02 Dec 2009 14:12:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assigning-a-macro-variable-inside-a-loop/m-p/24031#M4028</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-12-02T14:12:04Z</dc:date>
    </item>
  </channel>
</rss>

