<?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 syntax error %SCAN used in %DO loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-syntax-error-SCAN-used-in-DO-loop/m-p/287157#M59039</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/95727"&gt;@Elle&lt;/a&gt; wrote:&lt;BR /&gt;Actually, if I put SCAN instead of %SCAN I get another type of error: "Expecting an arithmetic operator".&lt;BR /&gt;&amp;amp;Price is a global variable that stores all the possible prices for an item. I use it to compare it to a column in my dataset. If the difference is positive I need to flag it and output everything up until that point into a different dataset (RedData), then remove all these rows from the Client dataset and begin all over again until there are no more rows for that client&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;%scan is a macro function; since macro language only knows the datatype text, quotes are not necessary (they even are wrongly used and cause problems in many cases). So you can use &amp;amp;price. as such&lt;/P&gt;
&lt;P&gt;scan, OTOH, is a data step function, and needs a variable of type char or a string literal as its first parameter, so you have to use &amp;amp;price as a literal &lt;U&gt;with&lt;/U&gt; quotes:&lt;/P&gt;
&lt;P&gt;scan("&amp;amp;price.",&amp;amp;j.,' ')&lt;/P&gt;
&lt;P&gt;In your case, I'd prefer to use %scan. Why? Because the substring extraction is performed once by the macro engine before the data step is compiled, while the data step function would be executed in each data step iteration when the step is executed. Therefore %scan is better performancewise.&lt;/P&gt;
&lt;P&gt;PS and since red is meant to be a numeric variable, you would need&lt;/P&gt;
&lt;P&gt;red = input(scan("&amp;amp;price.",&amp;amp;j.,' '),best.);&lt;/P&gt;
&lt;P&gt;to make it so.&lt;/P&gt;</description>
    <pubDate>Tue, 26 Jul 2016 12:21:43 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2016-07-26T12:21:43Z</dc:date>
    <item>
      <title>Macro syntax error %SCAN used in %DO loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-syntax-error-SCAN-used-in-DO-loop/m-p/287146#M59031</link>
      <description>&lt;P&gt;I have some code that I need to put in a DO LOOP. My code works perfectly outside the loop, but I can't seem to make it work inside it.&lt;/P&gt;&lt;P&gt;What it does is that it takes a global macro variable that has some numbers in them and extracts one number at a time, using that to do calculations:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%LET N=5;
/* example of macro var Price = 1000 1200 1300 5000 4500 */

%MACRO RED();
	%DO j=1 %TO &amp;amp;N.;
		DATA Client;
			SET Client;
			Red=%SCAN(&amp;amp;Price.,&amp;amp;j.+1," ");
			IF Red-Cost&amp;gt;0 THEN OUTPUT;
		RUN;			
	%END;
%MEND;
%RED()&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I tried a different version of the Scan function, but it doesn't work: &amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Red=%SCAN(&amp;amp;Price.,%eval(&amp;amp;j.+1)," ");&lt;/CODE&gt;&amp;nbsp;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jul 2016 11:11:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-syntax-error-SCAN-used-in-DO-loop/m-p/287146#M59031</guid>
      <dc:creator>Elle</dc:creator>
      <dc:date>2016-07-26T11:11:14Z</dc:date>
    </item>
    <item>
      <title>Re: Macro syntax error %SCAN used in %DO loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-syntax-error-SCAN-used-in-DO-loop/m-p/287148#M59032</link>
      <description>&lt;P&gt;Your in a data step, you can use SCAN, you don't need the macro version.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, what does &amp;amp;Price resolve to, and does that make sense within the comcept&amp;nbsp;of a data step. Ie Is if a list or a variable?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jul 2016 11:28:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-syntax-error-SCAN-used-in-DO-loop/m-p/287148#M59032</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-07-26T11:28:31Z</dc:date>
    </item>
    <item>
      <title>Re: Macro syntax error %SCAN used in %DO loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-syntax-error-SCAN-used-in-DO-loop/m-p/287151#M59034</link>
      <description>&lt;P&gt;Technically, " " is incorrect within a %SCAN function ... unless you want both quotes and blanks to be delimiters. &amp;nbsp;But that wouldn't cause a problem in this particular case. &amp;nbsp;The better delimiter would be %str( )&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One problem is using &amp;amp;j.+1 instead of &amp;amp;j. &amp;nbsp;When &amp;amp;J=5, the %SCAN function returns no characters (because &amp;amp;PRICE only contains 5 words) and your DATA step statement becomes:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Red= ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is that the only problem you are encountering?&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jul 2016 11:36:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-syntax-error-SCAN-used-in-DO-loop/m-p/287151#M59034</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-07-26T11:36:31Z</dc:date>
    </item>
    <item>
      <title>Re: Macro syntax error %SCAN used in %DO loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-syntax-error-SCAN-used-in-DO-loop/m-p/287152#M59035</link>
      <description>&lt;P&gt;When &amp;amp;j = 5, your %scan tries to look for word 5+1, which is not there and therefore the %scan function call returns an emtpy string, causing a syntax error. (see log!!)&lt;/P&gt;
&lt;P&gt;You also use the same dataset name in the data and set statements, causing permanent overwrites during the macro loop iteration.&lt;/P&gt;
&lt;P&gt;Try this instead:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data client;
input id $ cost;
cards;
a 900
b 1100
c 1400
d 4600
e 5100
;
run;

%let price=1000 1200 1300 5000 4500;

%macro red;
	%do j = 1 %to %sysfunc(countw(&amp;amp;price.,' '));
		data client&amp;amp;j.;
			set client;
			red=%scan(&amp;amp;price.,&amp;amp;j.," ");
			if red - cost &amp;gt; 0 then output;
		run;			
	%end;
%mend;
%red
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 26 Jul 2016 11:39:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-syntax-error-SCAN-used-in-DO-loop/m-p/287152#M59035</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-07-26T11:39:15Z</dc:date>
    </item>
    <item>
      <title>Re: Macro syntax error %SCAN used in %DO loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-syntax-error-SCAN-used-in-DO-loop/m-p/287154#M59036</link>
      <description>Actually, if I put SCAN instead of %SCAN I get another type of error: "Expecting an arithmetic operator".&lt;BR /&gt;&amp;amp;Price is a global variable that stores all the possible prices for an item. I use it to compare it to a column in my dataset. If the difference is positive I need to flag it and output everything up until that point into a different dataset (RedData), then remove all these rows from the Client dataset and begin all over again until there are no more rows for that client</description>
      <pubDate>Tue, 26 Jul 2016 12:02:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-syntax-error-SCAN-used-in-DO-loop/m-p/287154#M59036</guid>
      <dc:creator>Elle</dc:creator>
      <dc:date>2016-07-26T12:02:28Z</dc:date>
    </item>
    <item>
      <title>Re: Macro syntax error %SCAN used in %DO loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-syntax-error-SCAN-used-in-DO-loop/m-p/287155#M59037</link>
      <description>Thank you for your suggestion. I'm a bit afraid to use macro functions inside macro functions because I don't master them enough and instead of concentrating on the data I'm losing time trying to debug my program. When I saw this version of SCAN I thought if it works it's good enough, but thank you for your advice. I'll use it next time.And thank you for making me understand where the error came from. I thought it was weird my dataset was actually being created with that error in the log, but now I get it. I've got no more problems with the code, as crazy as that sounds. Thank you &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Tue, 26 Jul 2016 12:06:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-syntax-error-SCAN-used-in-DO-loop/m-p/287155#M59037</guid>
      <dc:creator>Elle</dc:creator>
      <dc:date>2016-07-26T12:06:57Z</dc:date>
    </item>
    <item>
      <title>Re: Macro syntax error %SCAN used in %DO loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-syntax-error-SCAN-used-in-DO-loop/m-p/287156#M59038</link>
      <description>Thank you for the suggestion. I've solved the problem now. Also, the overwriting of the table was supposed to happen because I eliminate rows if they meet a certain condition and re-start the analysis on the remaining ones until there are no more left. Thank you very much.</description>
      <pubDate>Tue, 26 Jul 2016 12:18:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-syntax-error-SCAN-used-in-DO-loop/m-p/287156#M59038</guid>
      <dc:creator>Elle</dc:creator>
      <dc:date>2016-07-26T12:18:20Z</dc:date>
    </item>
    <item>
      <title>Re: Macro syntax error %SCAN used in %DO loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-syntax-error-SCAN-used-in-DO-loop/m-p/287157#M59039</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/95727"&gt;@Elle&lt;/a&gt; wrote:&lt;BR /&gt;Actually, if I put SCAN instead of %SCAN I get another type of error: "Expecting an arithmetic operator".&lt;BR /&gt;&amp;amp;Price is a global variable that stores all the possible prices for an item. I use it to compare it to a column in my dataset. If the difference is positive I need to flag it and output everything up until that point into a different dataset (RedData), then remove all these rows from the Client dataset and begin all over again until there are no more rows for that client&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;%scan is a macro function; since macro language only knows the datatype text, quotes are not necessary (they even are wrongly used and cause problems in many cases). So you can use &amp;amp;price. as such&lt;/P&gt;
&lt;P&gt;scan, OTOH, is a data step function, and needs a variable of type char or a string literal as its first parameter, so you have to use &amp;amp;price as a literal &lt;U&gt;with&lt;/U&gt; quotes:&lt;/P&gt;
&lt;P&gt;scan("&amp;amp;price.",&amp;amp;j.,' ')&lt;/P&gt;
&lt;P&gt;In your case, I'd prefer to use %scan. Why? Because the substring extraction is performed once by the macro engine before the data step is compiled, while the data step function would be executed in each data step iteration when the step is executed. Therefore %scan is better performancewise.&lt;/P&gt;
&lt;P&gt;PS and since red is meant to be a numeric variable, you would need&lt;/P&gt;
&lt;P&gt;red = input(scan("&amp;amp;price.",&amp;amp;j.,' '),best.);&lt;/P&gt;
&lt;P&gt;to make it so.&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jul 2016 12:21:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-syntax-error-SCAN-used-in-DO-loop/m-p/287157#M59039</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-07-26T12:21:43Z</dc:date>
    </item>
  </channel>
</rss>

