<?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: substr and length in macro variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/substr-and-length-in-macro-variable/m-p/539209#M148525</link>
    <description>&lt;P&gt;I second&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&amp;nbsp;'s recommendation of %superq, which I often use if I don't know the structure of the macro variable input (such as a generic macro that may be called by end users).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But, per my other post, I recommend you don't put commas in your macro variables at all, esp. those that may serve as parameters to other macros or macro functions.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;P.S.:&amp;nbsp; Your problem is that that commas in your macro variable are passed as syntax to the %substr() macro function, which then thinks you're passing in too many parameters to that function.&lt;/P&gt;</description>
    <pubDate>Thu, 28 Feb 2019 03:46:40 GMT</pubDate>
    <dc:creator>ScottBass</dc:creator>
    <dc:date>2019-02-28T03:46:40Z</dc:date>
    <item>
      <title>substr and length in macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/substr-and-length-in-macro-variable/m-p/539204#M148520</link>
      <description>&lt;P&gt;I have a situation where I got field names for sql join condition but with a comma at last. I want to remove the last comma. The field_names1 is calculated generically that it has not quotes.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let field_names1=a.field1=b.field1 and a.field1=b.field2,;&lt;BR /&gt;%put &amp;amp;field_names1;&lt;BR /&gt;%let field_names=%substr(&amp;amp;field_names1,1,%length(&amp;amp;field_names1)-1);&lt;/P&gt;&lt;P&gt;%put &amp;amp;field_names;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;it produces error:&lt;/P&gt;&lt;P&gt;ERROR: Macro function %SUBSTR has too many arguments.&lt;/P&gt;</description>
      <pubDate>Thu, 28 Feb 2019 01:56:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/substr-and-length-in-macro-variable/m-p/539204#M148520</guid>
      <dc:creator>bhu</dc:creator>
      <dc:date>2019-02-28T01:56:13Z</dc:date>
    </item>
    <item>
      <title>Re: substr and length in macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/substr-and-length-in-macro-variable/m-p/539206#M148522</link>
      <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let field_names=%substr(%superq(field_names1),1,%length(%superq(field_names1))-1);
%put &amp;amp;field_names;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I would do this instead:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let field_names=%sysfunc(compress(%superq(field_names1),%str(,)));
%put &amp;amp;field_names;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I like using function %superq().&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It allows to blissfully sidestep many issues with problematic characters in macro variables.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Feb 2019 02:08:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/substr-and-length-in-macro-variable/m-p/539206#M148522</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-02-28T02:08:19Z</dc:date>
    </item>
    <item>
      <title>Re: substr and length in macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/substr-and-length-in-macro-variable/m-p/539208#M148524</link>
      <description>&lt;P&gt;My own approach to this is, I don't include "syntax" with "data".&amp;nbsp; In other words, don't include commas, quotation marks, etc, etc in with your macro variables.&amp;nbsp; Especially commas - you're only going to confuse the macro tokenizer without convoluted quoting that you don't have to do.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Instead, "inject" the syntax into your code at "run time".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For this I use a couple macros:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://github.com/scottbass/SAS/blob/master/Macro/seplist.sas" target="_blank"&gt;https://github.com/scottbass/SAS/blob/master/Macro/seplist.sas&lt;/A&gt; (originally written by Richard Devenezia)&lt;/P&gt;&lt;P&gt;&lt;A href="https://github.com/scottbass/SAS/blob/master/Macro/squote.sas" target="_blank"&gt;https://github.com/scottbass/SAS/blob/master/Macro/squote.sas&lt;/A&gt; (originally written by "Tom") (useful for RDBMS explicit passthrough that requires single quoted literals)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;See the macro header for use cases and original author attribution.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, instead of:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let list = "foo","bar","blah";
data foo;
   set bar;
   where var in (&amp;amp;list);
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I use:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let list = foo bar blah;
data foo;
   set bar;
   where var in (%seplist(&amp;amp;list,nest=qq));
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Hope this helps...&lt;/P&gt;</description>
      <pubDate>Thu, 28 Feb 2019 03:41:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/substr-and-length-in-macro-variable/m-p/539208#M148524</guid>
      <dc:creator>ScottBass</dc:creator>
      <dc:date>2019-02-28T03:41:35Z</dc:date>
    </item>
    <item>
      <title>Re: substr and length in macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/substr-and-length-in-macro-variable/m-p/539209#M148525</link>
      <description>&lt;P&gt;I second&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&amp;nbsp;'s recommendation of %superq, which I often use if I don't know the structure of the macro variable input (such as a generic macro that may be called by end users).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But, per my other post, I recommend you don't put commas in your macro variables at all, esp. those that may serve as parameters to other macros or macro functions.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;P.S.:&amp;nbsp; Your problem is that that commas in your macro variable are passed as syntax to the %substr() macro function, which then thinks you're passing in too many parameters to that function.&lt;/P&gt;</description>
      <pubDate>Thu, 28 Feb 2019 03:46:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/substr-and-length-in-macro-variable/m-p/539209#M148525</guid>
      <dc:creator>ScottBass</dc:creator>
      <dc:date>2019-02-28T03:46:40Z</dc:date>
    </item>
    <item>
      <title>Re: substr and length in macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/substr-and-length-in-macro-variable/m-p/539215#M148530</link>
      <description>&lt;P&gt;This works.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let field_names1=%str(a.field1=b.field1 and a.field1=b.field2,);
%put &amp;amp;=field_names1;

data _null_;
orginal_string="&amp;amp;field_names1.";
result_string=substr(strip(orginal_string),1,length(strip(orginal_string))-1);
call symput('field_names',result_string);
run;

%put &amp;amp;=field_names;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Please let us know if it worked for you.&lt;/P&gt;</description>
      <pubDate>Thu, 28 Feb 2019 05:13:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/substr-and-length-in-macro-variable/m-p/539215#M148530</guid>
      <dc:creator>Satish_Parida</dc:creator>
      <dc:date>2019-02-28T05:13:49Z</dc:date>
    </item>
    <item>
      <title>Re: substr and length in macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/substr-and-length-in-macro-variable/m-p/539392#M148615</link>
      <description>%superq works like magic. Thanks.</description>
      <pubDate>Thu, 28 Feb 2019 17:19:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/substr-and-length-in-macro-variable/m-p/539392#M148615</guid>
      <dc:creator>bhu</dc:creator>
      <dc:date>2019-02-28T17:19:27Z</dc:date>
    </item>
    <item>
      <title>Re: substr and length in macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/substr-and-length-in-macro-variable/m-p/539393#M148616</link>
      <description>Hi Satish,&lt;BR /&gt;Your solution works fine in a datastep. Thanks.&lt;BR /&gt;Is there any difference between &amp;amp;variable. and &amp;amp;=variable?</description>
      <pubDate>Thu, 28 Feb 2019 17:21:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/substr-and-length-in-macro-variable/m-p/539393#M148616</guid>
      <dc:creator>bhu</dc:creator>
      <dc:date>2019-02-28T17:21:51Z</dc:date>
    </item>
    <item>
      <title>Re: substr and length in macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/substr-and-length-in-macro-variable/m-p/539491#M148656</link>
      <description>&lt;P&gt;&lt;A href="http://support.sas.com/documentation/cdl/en/mcrolref/62978/HTML/default/viewer.htm#n189qvy83pmkt6n1bq2mmwtyb4oe.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/mcrolref/62978/HTML/default/viewer.htm#n189qvy83pmkt6n1bq2mmwtyb4oe.htm&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Scroll down to the tip.&lt;/P&gt;</description>
      <pubDate>Thu, 28 Feb 2019 21:10:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/substr-and-length-in-macro-variable/m-p/539491#M148656</guid>
      <dc:creator>ScottBass</dc:creator>
      <dc:date>2019-02-28T21:10:46Z</dc:date>
    </item>
  </channel>
</rss>

