<?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: Extracting item from macro variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Extracting-item-from-macro-variable/m-p/874575#M345555</link>
    <description>&lt;P&gt;&lt;FONT face="courier new,courier"&gt;%put &amp;amp;list$2;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;is not really the syntax you want. It writes to the log a string which is your variable &amp;amp;list with $2 appended.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To extract words from a string, you want to use the &lt;A href="https://documentation.sas.com/doc/en/pgmmvacdc/9.4/mcrolref/p1nhhymw6gxixvn1johcfl6kaygw.htm" target="_self"&gt;&lt;FONT face="courier new,courier"&gt;%scan&lt;/FONT&gt; macro function&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%put %scan(&amp;amp;list,2);&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 08 May 2023 22:43:13 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2023-05-08T22:43:13Z</dc:date>
    <item>
      <title>Extracting item from macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-item-from-macro-variable/m-p/874573#M345553</link>
      <description>&lt;P&gt;I'm wondering how I can extract an item from a list contained in a macro variable. I know I can do something like this using arrays inside of a data step, but am seeking a way to do this outside of a data step.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've included a simplified example, in which I create a macro variable containing a list and want to extract the 2nd item.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;%let list = dog cat mouse;
%put &amp;amp;list$2; /*want this to return cat/*&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 May 2023 22:32:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-item-from-macro-variable/m-p/874573#M345553</guid>
      <dc:creator>everyone</dc:creator>
      <dc:date>2023-05-08T22:32:41Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting item from macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-item-from-macro-variable/m-p/874575#M345555</link>
      <description>&lt;P&gt;&lt;FONT face="courier new,courier"&gt;%put &amp;amp;list$2;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;is not really the syntax you want. It writes to the log a string which is your variable &amp;amp;list with $2 appended.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To extract words from a string, you want to use the &lt;A href="https://documentation.sas.com/doc/en/pgmmvacdc/9.4/mcrolref/p1nhhymw6gxixvn1johcfl6kaygw.htm" target="_self"&gt;&lt;FONT face="courier new,courier"&gt;%scan&lt;/FONT&gt; macro function&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%put %scan(&amp;amp;list,2);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 08 May 2023 22:43:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-item-from-macro-variable/m-p/874575#M345555</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-05-08T22:43:13Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting item from macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-item-from-macro-variable/m-p/874594#M345571</link>
      <description>&lt;P&gt;What do you consider the second "item"?&lt;/P&gt;
&lt;P&gt;A macro variable is just a string of characters.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to treat the macro variable as a space delimited list of values then you can use the %SCAN() function&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let second=%scan(&amp;amp;list,2,%str( ));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or if you use the macro variable value to generate actual SAS code you can then use the SCAN() function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;second = scan("&amp;amp;list",2,' ');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 09 May 2023 14:45:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-item-from-macro-variable/m-p/874594#M345571</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-05-09T14:45:17Z</dc:date>
    </item>
  </channel>
</rss>

