<?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: Macro Variable Scope in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-Variable-Scope/m-p/26454#M4728</link>
    <description>The reason for the problem boils down to your macro %test generating the following code (as reported on log by turning on the mprint option):&lt;BR /&gt;
[pre]&lt;BR /&gt;
   MPRINT(TEST):   DATA _NULL_;&lt;BR /&gt;
   MPRINT(TEST):   DO INC=1 TO 3;&lt;BR /&gt;
   MPRINT(TEST):   PUT "2";&lt;BR /&gt;
   MPRINT(TEST):   END;&lt;BR /&gt;
   MPRINT(TEST):   RUN;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
If you really want to return 2, 3, 4, then your macro should be something like:&lt;BR /&gt;
[pre]&lt;BR /&gt;
   %macro count(from=2, to=4);&lt;BR /&gt;
     %local i;&lt;BR /&gt;
     %do i = &amp;amp;from %to &amp;amp;to;&lt;BR /&gt;
       %if &amp;amp;i &amp;gt; &amp;amp;from %then %*;,;&lt;BR /&gt;
       %*;&amp;amp;i&lt;BR /&gt;
     %end;&lt;BR /&gt;
   %mend  count;&lt;BR /&gt;
&lt;BR /&gt;
   %put %count(to=4);&lt;BR /&gt;
   %*-- on log&lt;BR /&gt;
   2,3,4&lt;BR /&gt;
   --*;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
This may be too advance for you, but just in case, looping over a list of blank delimited words is such a common task that there have been much development in making it simple. For instance, you can separate the looping part out into a separate macro so that you can do something like this one posted on the sas-l:&lt;BR /&gt;
   &lt;A href="http://www.listserv.uga.edu/cgi-bin/wa?A2=ind1011a&amp;amp;L=sas-l&amp;amp;O=D&amp;amp;P=999" target="_blank"&gt;http://www.listserv.uga.edu/cgi-bin/wa?A2=ind1011a&amp;amp;L=sas-l&amp;amp;O=D&amp;amp;P=999&lt;/A&gt;</description>
    <pubDate>Tue, 02 Nov 2010 13:43:36 GMT</pubDate>
    <dc:creator>chang_y_chung_hotmail_com</dc:creator>
    <dc:date>2010-11-02T13:43:36Z</dc:date>
    <item>
      <title>Macro Variable Scope</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Variable-Scope/m-p/26452#M4726</link>
      <description>I am trying to write a macro to run through a list of variables and calculate descriptive statistics.  I have run into a problem, I think with variable scope, that I can't get past, and I would greatly appreciate any help.  The problem boils down to this:&lt;BR /&gt;
&lt;BR /&gt;
%MACRO TEST();&lt;BR /&gt;
     DATA _NULL_;&lt;BR /&gt;
     %LET I = 1;&lt;BR /&gt;
     DO INC=1 TO 3;&lt;BR /&gt;
          %LET I = %EVAL(&amp;amp;I + 1);&lt;BR /&gt;
          PUT "&amp;amp;I";&lt;BR /&gt;
     END;&lt;BR /&gt;
     RUN;&lt;BR /&gt;
%MEND;&lt;BR /&gt;
%TEST();&lt;BR /&gt;
&lt;BR /&gt;
This generates 2, 2, 2 rather than 2, 3, 4.&lt;BR /&gt;
&lt;BR /&gt;
The full code I am trying to use, if that helps, is this:&lt;BR /&gt;
&lt;BR /&gt;
%MACRO DATAINFO(DATA, VARS);&lt;BR /&gt;
DATA _NULL_;&lt;BR /&gt;
%LET FILENAME = "&amp;amp;SASDATA.MACROS\PRG.SAS";&lt;BR /&gt;
FILE &amp;amp;FILENAME;&lt;BR /&gt;
%LET VAR = %SYSFUNC(SCAN(%QUOTE(&amp;amp;VARS), 1));&lt;BR /&gt;
PUT "PROC SQL;";&lt;BR /&gt;
PUT "SELECT "; PUT """&amp;amp;VAR"""; PUT " AS VAR,";&lt;BR /&gt;
PUT "COUNT(&amp;amp;VAR) LABEL='Total of Obs',";&lt;BR /&gt;
PUT "COUNT(DISTINCT &amp;amp;VAR) LABEL='Unique Values',";&lt;BR /&gt;
PUT "FROM &amp;amp;DATA";&lt;BR /&gt;
PUT "GROUP BY VAR";&lt;BR /&gt;
%LET COUNT = %SYSFUNC(COUNTW(%QUOTE(&amp;amp;VARS)));&lt;BR /&gt;
IF &amp;amp;COUNT &amp;gt; 1 THEN DO;&lt;BR /&gt;
%LET I = 1;&lt;BR /&gt;
	DO INC=2 TO &amp;amp;COUNT;&lt;BR /&gt;
		%LET I = %EVAL(&amp;amp;I + 1);&lt;BR /&gt;
		%PUT &amp;amp;I;&lt;BR /&gt;
		%LET VAR = %SYSFUNC(SCAN(%QUOTE(&amp;amp;VARS), &amp;amp;I));&lt;BR /&gt;
		PUT "UNION";&lt;BR /&gt;
		PUT "SELECT "; PUT """&amp;amp;VAR"""; PUT " AS VAR,";&lt;BR /&gt;
		PUT "COUNT(&amp;amp;VAR) LABEL='Total of Obs',";&lt;BR /&gt;
		PUT "COUNT(DISTINCT &amp;amp;VAR) LABEL='Unique Values',";&lt;BR /&gt;
		PUT "FROM &amp;amp;DATA";&lt;BR /&gt;
		PUT "GROUP BY VAR";&lt;BR /&gt;
	END;&lt;BR /&gt;
END;&lt;BR /&gt;
PUT ";QUIT;";&lt;BR /&gt;
RUN;&lt;BR /&gt;
*%INCLUDE &amp;amp;FILENAME;&lt;BR /&gt;
%MEND;&lt;BR /&gt;
&lt;BR /&gt;
/*USAGE*/&lt;BR /&gt;
/*&lt;BR /&gt;
%LET INITIAL = AGE HAZARD_GROUP CLASSCOUNT;&lt;BR /&gt;
%DATAINFO(MODEL.DATA, &amp;amp;INITIAL);&lt;BR /&gt;
*/&lt;BR /&gt;
&lt;BR /&gt;
The problem is, whether I try to use I or INC, I can't get a variable to increment in this line:&lt;BR /&gt;
&lt;BR /&gt;
%LET VAR = %SYSFUNC(SCAN(%QUOTE(&amp;amp;VARS), &amp;amp;I));&lt;BR /&gt;
&lt;BR /&gt;
If I use INC, it says this is not numeric.  If I use I, I can't get it to increment.  I would appreciate ANY suggestions.&lt;BR /&gt;
&lt;BR /&gt;
Thanks,&lt;BR /&gt;
Chris</description>
      <pubDate>Tue, 02 Nov 2010 11:53:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Variable-Scope/m-p/26452#M4726</guid>
      <dc:creator>cjohnson</dc:creator>
      <dc:date>2010-11-02T11:53:01Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Variable Scope</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Variable-Scope/m-p/26453#M4727</link>
      <description>I can't say I understand what you are trying to do here but when I look at your program I think, PROC MEANS.&lt;BR /&gt;
&lt;BR /&gt;
PROC MEANS can process MANY variables at one time so no looping over lists of variables is necessary.  Plus the syntax is designed to calculate many stats with little effort and the CLASS statement allows many WAYS of grouping.</description>
      <pubDate>Tue, 02 Nov 2010 13:07:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Variable-Scope/m-p/26453#M4727</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2010-11-02T13:07:25Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Variable Scope</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Variable-Scope/m-p/26454#M4728</link>
      <description>The reason for the problem boils down to your macro %test generating the following code (as reported on log by turning on the mprint option):&lt;BR /&gt;
[pre]&lt;BR /&gt;
   MPRINT(TEST):   DATA _NULL_;&lt;BR /&gt;
   MPRINT(TEST):   DO INC=1 TO 3;&lt;BR /&gt;
   MPRINT(TEST):   PUT "2";&lt;BR /&gt;
   MPRINT(TEST):   END;&lt;BR /&gt;
   MPRINT(TEST):   RUN;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
If you really want to return 2, 3, 4, then your macro should be something like:&lt;BR /&gt;
[pre]&lt;BR /&gt;
   %macro count(from=2, to=4);&lt;BR /&gt;
     %local i;&lt;BR /&gt;
     %do i = &amp;amp;from %to &amp;amp;to;&lt;BR /&gt;
       %if &amp;amp;i &amp;gt; &amp;amp;from %then %*;,;&lt;BR /&gt;
       %*;&amp;amp;i&lt;BR /&gt;
     %end;&lt;BR /&gt;
   %mend  count;&lt;BR /&gt;
&lt;BR /&gt;
   %put %count(to=4);&lt;BR /&gt;
   %*-- on log&lt;BR /&gt;
   2,3,4&lt;BR /&gt;
   --*;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
This may be too advance for you, but just in case, looping over a list of blank delimited words is such a common task that there have been much development in making it simple. For instance, you can separate the looping part out into a separate macro so that you can do something like this one posted on the sas-l:&lt;BR /&gt;
   &lt;A href="http://www.listserv.uga.edu/cgi-bin/wa?A2=ind1011a&amp;amp;L=sas-l&amp;amp;O=D&amp;amp;P=999" target="_blank"&gt;http://www.listserv.uga.edu/cgi-bin/wa?A2=ind1011a&amp;amp;L=sas-l&amp;amp;O=D&amp;amp;P=999&lt;/A&gt;</description>
      <pubDate>Tue, 02 Nov 2010 13:43:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Variable-Scope/m-p/26454#M4728</guid>
      <dc:creator>chang_y_chung_hotmail_com</dc:creator>
      <dc:date>2010-11-02T13:43:36Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Variable Scope</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Variable-Scope/m-p/26455#M4729</link>
      <description>Yes, do something similar to what Chang said, or just change yours as follows:&lt;BR /&gt;
&lt;BR /&gt;
%MACRO TEST();&lt;BR /&gt;
DATA _NULL_;&lt;BR /&gt;
%DO i=1 %TO 3;&lt;BR /&gt;
%LET J = %EVAL(&amp;amp;I + 1);&lt;BR /&gt;
PUT "&amp;amp;J";&lt;BR /&gt;
%end;&lt;BR /&gt;
RUN;&lt;BR /&gt;
%MEND;&lt;BR /&gt;
%TEST();</description>
      <pubDate>Mon, 13 Dec 2010 15:13:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Variable-Scope/m-p/26455#M4729</guid>
      <dc:creator>PatrickG</dc:creator>
      <dc:date>2010-12-13T15:13:13Z</dc:date>
    </item>
  </channel>
</rss>

