<?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 variables in substr function in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-variables-in-substr-function/m-p/610772#M177925</link>
    <description>&lt;P&gt;To help you we need to know the types of the ENTITY and CURRENCY_CD variables.&lt;/P&gt;
&lt;P&gt;Assuming the first is numeric and the second is character you want to do this, using the macro function %SUBSTR() instead of the SAS function SUBSTR().&amp;nbsp; So when ENT_CURR is 10000_EUR then this code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;case when entity=%substr(&amp;amp;ent_curr.,1,5) then "%substr(&amp;amp;ent_curr.,7,9)"
     else Currency_cd
end&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Will generate this SAS code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;case when entity=10000 then "EUR"
     else Currency_cd
end
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 10 Dec 2019 16:43:45 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2019-12-10T16:43:45Z</dc:date>
    <item>
      <title>Macro variables in substr function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-variables-in-substr-function/m-p/610769#M177922</link>
      <description>&lt;P&gt;I would like to know how to use substr function with macro variables. I have the macro variables as follows and I would like to look for first five characters and select the last three characters based on the condition as mentioned below. Not sure it is right and I do not have SAS to execute to it (downtime now). Correct me if I'm wrong.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let ent_curr=10000_EUR; /*i have many macro variables like this*/

/*and I have to write the condition as like below*/
/*given below is sample condition and there are some conditions as similar which I have to write*/

Case 
When entity =substr(&amp;amp;ent_curr.,1,5) then substr(&amp;amp;ent_curr.,7,9)
Else 
Currency_cd
End; &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 10 Dec 2019 16:31:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-variables-in-substr-function/m-p/610769#M177922</guid>
      <dc:creator>Babloo</dc:creator>
      <dc:date>2019-12-10T16:31:29Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variables in substr function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-variables-in-substr-function/m-p/610771#M177924</link>
      <description>&lt;P&gt;SUBSTR works on a text string&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token function"&gt;substr&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;ent_curr&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;5&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;tells SAS to use the text string value of variable by the name of &amp;amp;ENT_CURR, which resolves to the text string value of the variable named 10000_EUR, but the variable named 10000_EUR doesn't exist.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To make SUBSTR work on a text string and not the value of a variable, you have to enclose &amp;amp;ENT_CURR in ______________________. (Answer to be provided by the user)&lt;/P&gt;</description>
      <pubDate>Tue, 10 Dec 2019 17:51:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-variables-in-substr-function/m-p/610771#M177924</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-12-10T17:51:43Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variables in substr function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-variables-in-substr-function/m-p/610772#M177925</link>
      <description>&lt;P&gt;To help you we need to know the types of the ENTITY and CURRENCY_CD variables.&lt;/P&gt;
&lt;P&gt;Assuming the first is numeric and the second is character you want to do this, using the macro function %SUBSTR() instead of the SAS function SUBSTR().&amp;nbsp; So when ENT_CURR is 10000_EUR then this code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;case when entity=%substr(&amp;amp;ent_curr.,1,5) then "%substr(&amp;amp;ent_curr.,7,9)"
     else Currency_cd
end&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Will generate this SAS code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;case when entity=10000 then "EUR"
     else Currency_cd
end
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 10 Dec 2019 16:43:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-variables-in-substr-function/m-p/610772#M177925</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-12-10T16:43:45Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variables in substr function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-variables-in-substr-function/m-p/610773#M177926</link>
      <description>&lt;P&gt;Always do the text replacement to see what would happen and if it's valid code.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Case 
When entity =substr(&lt;FONT size="4" color="#FF0000"&gt;&lt;STRONG&gt;10000_EUR&lt;/STRONG&gt;&lt;/FONT&gt;,1,5) then substr(&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;10000_EUR&lt;/STRONG&gt;&lt;/FONT&gt;,7,9)
Else 
Currency_cd
End; &lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can see the above would not be valid code.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To make it valid you can add quotes around the macro variable so it's treated as text.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Case 
When entity =substr(&lt;FONT size="4" color="#FF6600"&gt;&lt;STRONG&gt;"&lt;/STRONG&gt;&lt;/FONT&gt;10000_EUR&lt;FONT size="4" color="#FF6600"&gt;&lt;STRONG&gt;"&lt;/STRONG&gt;&lt;/FONT&gt;,1,5) then substr(&lt;FONT size="4" color="#FF6600"&gt;&lt;STRONG&gt;"&lt;/STRONG&gt;&lt;/FONT&gt;10000_EUR&lt;FONT size="4" color="#FF6600"&gt;&lt;STRONG&gt;"&lt;/STRONG&gt;&lt;/FONT&gt;,7,9)
Else 
Currency_cd
End; &lt;/PRE&gt;
&lt;P&gt;And then replace the quotes around your macro variable to finish the correction.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 10 Dec 2019 16:45:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-variables-in-substr-function/m-p/610773#M177926</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-12-10T16:45:29Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variables in substr function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-variables-in-substr-function/m-p/610774#M177927</link>
      <description>I don't understand the second part of your explanation. Could you please&lt;BR /&gt;help me with one example which works for my scenario?&lt;BR /&gt;</description>
      <pubDate>Tue, 10 Dec 2019 16:46:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-variables-in-substr-function/m-p/610774#M177927</guid>
      <dc:creator>Babloo</dc:creator>
      <dc:date>2019-12-10T16:46:49Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variables in substr function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-variables-in-substr-function/m-p/610777#M177929</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/8409"&gt;@Babloo&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;I don't understand the second part of your explanation. Could you please&lt;BR /&gt;help me with one example which works for my scenario?&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I strongly suggest that you start making use of the tool that sits between your ears. The answer to &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;'s exercise is DEAD SIMPLE.&lt;/P&gt;
&lt;P&gt;Especially for someone who's been around here for ~1000 posts and probably seen it tens of times in our answers to previous questions.&lt;/P&gt;</description>
      <pubDate>Tue, 10 Dec 2019 16:56:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-variables-in-substr-function/m-p/610777#M177929</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-12-10T16:56:44Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variables in substr function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-variables-in-substr-function/m-p/610822#M177945</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/8409"&gt;@Babloo&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just to add: The third argument of the &lt;A href="https://documentation.sas.com/?docsetId=lefunctionsref&amp;amp;docsetTarget=n0n08xougp40i5n1xw7njpcy0a2b.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_blank" rel="noopener"&gt;SUBSTR&lt;/A&gt; and &lt;A href="https://documentation.sas.com/?docsetId=mcrolref&amp;amp;docsetTarget=n0nq1hkovbu54en1h78i6ci7gz51.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_blank" rel="noopener"&gt;%SUBSTR&lt;/A&gt;&amp;nbsp;functions is the &lt;EM&gt;length&lt;/EM&gt;&amp;nbsp;&lt;SPAN&gt;of the substring to extract, not the position of the last character of the substring (although it is tempting to think so). Hence, it should be &lt;EM&gt;3&lt;/EM&gt; in your second function call, not 9 (which would be invalid, although not refused). Or you can simply omit the third argument to extract the &lt;EM&gt;rest&lt;/EM&gt; of the string (from starting position 7).&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Actually, in your example I'd rather use &lt;A href="https://documentation.sas.com/?docsetId=lefunctionsref&amp;amp;docsetTarget=p0jshdjy2z9zdzn1h7k90u99lyq6.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_blank" rel="noopener"&gt;SCAN&lt;/A&gt; (or &lt;A href="https://documentation.sas.com/?docsetId=mcrolref&amp;amp;docsetTarget=p1nhhymw6gxixvn1johcfl6kaygw.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_blank" rel="noopener"&gt;%SCAN&lt;/A&gt;) because it doesn't make assumptions about lengths of substrings.&lt;/P&gt;</description>
      <pubDate>Tue, 10 Dec 2019 20:24:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-variables-in-substr-function/m-p/610822#M177945</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-12-10T20:24:31Z</dc:date>
    </item>
  </channel>
</rss>

