<?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: How do i handle ampersand in macro loop? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-i-handle-ampersand-in-macro-loop/m-p/530587#M145126</link>
    <description>&lt;P&gt;You can use the QUOTE function to quote the values in the SQL expression. And if you use a single quote (second argument), SAS will not try to interpret the ampersands:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  select quote(trim(Name),"'") into :name1-:name&amp;amp;SysMaxLong
  from subset;
  %let cnt=&amp;amp;sqlobs;
quit;

%macro loop();
proc sql;
  create table final as
  select * from master
  where name IN (&amp;amp;name1
  %do  i=2 %to &amp;amp;cnt;
    ,&amp;amp;&amp;amp;name&amp;amp;i
    %end;
);
quit;
%mend;

%loop;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I also used the SQLOBS Automatic macro variable to get the number of variables generated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But it is much simpler to use a single macro variable and SEPARATED BY:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  select quote(trim(Name),"'") into :names separated by ','
  from subset;
quit;

proc sql;
  create table final as
  select * from master
  where name IN (&amp;amp;names);
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 28 Jan 2019 11:17:08 GMT</pubDate>
    <dc:creator>s_lassen</dc:creator>
    <dc:date>2019-01-28T11:17:08Z</dc:date>
    <item>
      <title>How do i handle ampersand in macro loop?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-i-handle-ampersand-in-macro-loop/m-p/530582#M145123</link>
      <description>&lt;P&gt;Hi everyone. I am running a script that has macro to loop SQL statement. The thing is, it is possible for Macro variable to have value of Ampersand that is supposed to be treated as text. I tried various functions like superq and brquote but i dont understand any of the sample online and i will never get it working. It is always error for me. Can anyone help me on this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is my code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data subset;

Input Name $;
Alice!
Andy&amp;amp;Ace
P&amp;amp;L
A&amp;amp;L
BBB
CCC
ZZZ
XXX
;
run;





data master;
Input Name $;
Alice!
Andy&amp;amp;Ace
P&amp;amp;L
A&amp;amp;L
BBB
CCC
ZZZ
XXX
OOO
PPP
EEE
RRR
TTT
;
run;

proc sql noprint;

select Name into :name1-:name&amp;amp;SysMaxLong

from subset;

select count(*) into:cnt

from subset;

quit;



%macro loop();

proc sql;

create table final as

select * from master

where name IN ("&amp;amp;name1"

%do&amp;nbsp; i=1 %to &amp;amp;cnt;

,"&amp;amp;&amp;amp;name&amp;amp;i.."

%end;

);

quit;



%mend;

%loop;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Jan 2019 10:58:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-i-handle-ampersand-in-macro-loop/m-p/530582#M145123</guid>
      <dc:creator>WorkingMan</dc:creator>
      <dc:date>2019-01-28T10:58:38Z</dc:date>
    </item>
    <item>
      <title>Re: How do i handle ampersand in macro loop?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-i-handle-ampersand-in-macro-loop/m-p/530587#M145126</link>
      <description>&lt;P&gt;You can use the QUOTE function to quote the values in the SQL expression. And if you use a single quote (second argument), SAS will not try to interpret the ampersands:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  select quote(trim(Name),"'") into :name1-:name&amp;amp;SysMaxLong
  from subset;
  %let cnt=&amp;amp;sqlobs;
quit;

%macro loop();
proc sql;
  create table final as
  select * from master
  where name IN (&amp;amp;name1
  %do  i=2 %to &amp;amp;cnt;
    ,&amp;amp;&amp;amp;name&amp;amp;i
    %end;
);
quit;
%mend;

%loop;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I also used the SQLOBS Automatic macro variable to get the number of variables generated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But it is much simpler to use a single macro variable and SEPARATED BY:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  select quote(trim(Name),"'") into :names separated by ','
  from subset;
quit;

proc sql;
  create table final as
  select * from master
  where name IN (&amp;amp;names);
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 28 Jan 2019 11:17:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-i-handle-ampersand-in-macro-loop/m-p/530587#M145126</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2019-01-28T11:17:08Z</dc:date>
    </item>
    <item>
      <title>Re: How do i handle ampersand in macro loop?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-i-handle-ampersand-in-macro-loop/m-p/530589#M145128</link>
      <description>&lt;P&gt;Thanks for the clear message and code. Much appreciated. However, i notice that my data step:&lt;/P&gt;&lt;P&gt;data have;&lt;/P&gt;&lt;P&gt;name=a&amp;amp;w;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It will throw error. I also tried wrapping the whole value with %nrstr and %quote, but it will give warning and create 2 variables called "a" and "w". How should i handle it?&lt;/P&gt;</description>
      <pubDate>Mon, 28 Jan 2019 11:28:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-i-handle-ampersand-in-macro-loop/m-p/530589#M145128</guid>
      <dc:creator>WorkingMan</dc:creator>
      <dc:date>2019-01-28T11:28:07Z</dc:date>
    </item>
    <item>
      <title>Re: How do i handle ampersand in macro loop?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-i-handle-ampersand-in-macro-loop/m-p/530591#M145129</link>
      <description>&lt;P&gt;Whilst I am sure that you will get an answer to your question, I would suggest that anything you do get, or try, will result in very messy, hard to maintain code.&amp;nbsp; Macro is not a data manipulation language, its one and only purpose is to generate text.&amp;nbsp; What that text is then used for is up to the next process - generally it gets feed into the Base SAS compiler to be operated upon.&lt;/P&gt;
&lt;P&gt;Look at what it is you are trying to do, isolate the input data, and the required output, then assess a procedure or set of steps to get from one to the other.&amp;nbsp; Just from looking at the code you&amp;nbsp; provided (which really doesn't give a good picture), I would start by dropping the SQL thinking, look at proc means/summary - which are specifically built to get counts and other simple statistics from data, and use those to calculate the result.&amp;nbsp; This will be &amp;amp;cnt. + 1 proc step getting counts % faster, use &amp;amp;cnt. + 1 less resource, and be far simpler to maintain and expand upon.&lt;/P&gt;
&lt;P&gt;If your data isn't in a good shape to do a simple proc over, then consider re-modelling your data to be easy to procedure over.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As I have no test data to work with, I obviously can't show you an example with your data, but consider:&lt;/P&gt;
&lt;PRE&gt;id1  var1 var2...&lt;/PRE&gt;
&lt;P&gt;I I want totals of those, I could loop over that dataset, once for each column and do a sum() by id, or I could proc over that data once and do something like;&lt;/P&gt;
&lt;PRE&gt;proc means data=have;
  by id;
  var var:;
  output out=want sum=;
run;&lt;/PRE&gt;
&lt;P&gt;Effectively the same output, however it is one step (1 * read/write), rather than number of columns * read/write.&amp;nbsp; Also it will expand and shrink with your data without any need to modification or increase in resource.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Jan 2019 11:42:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-i-handle-ampersand-in-macro-loop/m-p/530591#M145129</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2019-01-28T11:42:48Z</dc:date>
    </item>
    <item>
      <title>Re: How do i handle ampersand in macro loop?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-i-handle-ampersand-in-macro-loop/m-p/530593#M145130</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/189060"&gt;@WorkingMan&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks for the clear message and code. Much appreciated. However, i notice that my data step:&lt;/P&gt;
&lt;P&gt;data have;&lt;/P&gt;
&lt;P&gt;name=a&amp;amp;w;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It will throw error. I also tried wrapping the whole value with %nrstr and %quote, but it will give warning and create 2 variables called "a" and "w". How should i handle it?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Do you actually want to set name to the string"a&amp;amp;w"? If yes, do this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
name = 'a&amp;amp;w';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The single quotes will prevent any attempt to resolve macro variables.&lt;/P&gt;
&lt;P&gt;In your code, after the reslution of macro variable &amp;amp;w fails, the resulting code is equivalent to this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
name = a &amp;amp; w;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which is equivalent to&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
name = a and w;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Since the two missing values in (undefined) a and w are considered as false, the result of the right side is also a false, and name will be defined as numeric and set to zero.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Jan 2019 11:53:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-i-handle-ampersand-in-macro-loop/m-p/530593#M145130</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-01-28T11:53:11Z</dc:date>
    </item>
  </channel>
</rss>

