<?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: Can I use %INDEX to search a macro list? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Can-I-use-INDEX-to-search-a-macro-list/m-p/474988#M285963</link>
    <description>&lt;P&gt;Very odd. I tried this method using in my proc sql like so:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;select compress(WestAfr,"'") into :WestAfrlist separated by ' '&lt;/P&gt;&lt;P&gt;from work.data&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and that didn't work (I had Cote d'Ivoire, thus the compress)!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, your quote(trim( worked perfectly. I'll have to check the SAS docs to get the specifics on quote trim. Thank you Tom and thank you Paige!&lt;/P&gt;</description>
    <pubDate>Mon, 02 Jul 2018 17:14:43 GMT</pubDate>
    <dc:creator>AreYouLikeNew</dc:creator>
    <dc:date>2018-07-02T17:14:43Z</dc:date>
    <item>
      <title>Can I use %INDEX to search a macro list?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-I-use-INDEX-to-search-a-macro-list/m-p/474967#M285959</link>
      <description>&lt;P&gt;Hi all,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've created a list &amp;amp;WestAfrlist in proc sql (not shown). The %put looks great and produces a list of countries in W. Africa. Now, I want to search that list for a match in the variable "country1" for each observation in my dataset. In other words, if for an observation country1=Benin, I'd like to search WestAfrlist for Benin and return a value of 1 for "region_ind". With the bad code below, region_ind doesn't initialize and&amp;nbsp;&lt;SPAN&gt;ERROR: Variable REGION_IND not found.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Version 9.4.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;%macro&lt;/STRONG&gt;&lt;/SPAN&gt; abc(dv);&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;%let&lt;/SPAN&gt; findabc=&lt;SPAN&gt;%index&lt;/SPAN&gt;(&amp;amp;WestAfrlist.,dv);&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;%if&lt;/SPAN&gt; &amp;amp;findabc=&lt;SPAN&gt;&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;/SPAN&gt; &lt;SPAN&gt;%then&lt;/SPAN&gt; &lt;SPAN&gt;%do&lt;/SPAN&gt;;&lt;/P&gt;&lt;P&gt;region_ind=&lt;SPAN&gt;&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;/SPAN&gt;;&lt;/P&gt;&lt;P&gt;%end&lt;SPAN&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;%mend&lt;/STRONG&gt;&lt;/SPAN&gt; abc;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/SPAN&gt; travel2017;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;set&lt;/SPAN&gt; travelhx;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;if&lt;/SPAN&gt; country1=&lt;SPAN&gt;'NA'&lt;/SPAN&gt; &lt;SPAN&gt;then&lt;/SPAN&gt; country1=&lt;SPAN&gt;""&lt;/SPAN&gt;;&lt;/P&gt;&lt;P&gt;%&lt;STRONG&gt;&lt;I&gt;abc&lt;/I&gt;&lt;/STRONG&gt; (country1);&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;SPAN&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Any ideas on how to accomplish this? Thanks in advance.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 02 Jul 2018 16:27:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-I-use-INDEX-to-search-a-macro-list/m-p/474967#M285959</guid>
      <dc:creator>AreYouLikeNew</dc:creator>
      <dc:date>2018-07-02T16:27:28Z</dc:date>
    </item>
    <item>
      <title>Re: Can I use %INDEX to search a macro list?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-I-use-INDEX-to-search-a-macro-list/m-p/474979#M285960</link>
      <description>&lt;P&gt;Ok, I just realized that my "list" is simply concatenated strings. Am working on getting it through the first observation, unless someone has a better idea first!&lt;/P&gt;</description>
      <pubDate>Mon, 02 Jul 2018 16:42:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-I-use-INDEX-to-search-a-macro-list/m-p/474979#M285960</guid>
      <dc:creator>AreYouLikeNew</dc:creator>
      <dc:date>2018-07-02T16:42:44Z</dc:date>
    </item>
    <item>
      <title>Re: Can I use %INDEX to search a macro list?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-I-use-INDEX-to-search-a-macro-list/m-p/474984#M285961</link>
      <description>&lt;P&gt;You could create the list in a way that makes is useful for generating an IN () condition test in SAS code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint ;
  select quote(trim(country)) into :list separated by ' '
  from source_table
  where region='West Africa'
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then just expand the list where you would normally have to type string literals.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if country1 in (&amp;amp;list) then region_ind=1 ;
else region_ind=0;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 02 Jul 2018 16:55:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-I-use-INDEX-to-search-a-macro-list/m-p/474984#M285961</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-07-02T16:55:33Z</dc:date>
    </item>
    <item>
      <title>Re: Can I use %INDEX to search a macro list?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-I-use-INDEX-to-search-a-macro-list/m-p/474986#M285962</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/218810"&gt;@AreYouLikeNew&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/SPAN&gt; travel2017;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;set&lt;/SPAN&gt; travelhx;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;if&lt;/SPAN&gt; country1=&lt;SPAN&gt;'NA'&lt;/SPAN&gt; &lt;SPAN&gt;then&lt;/SPAN&gt; country1=&lt;SPAN&gt;""&lt;/SPAN&gt;;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;%&lt;STRONG&gt;&lt;I&gt;abc&lt;/I&gt;&lt;/STRONG&gt; (country1)&lt;/FONT&gt;;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;SPAN&gt;;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;You can't use a data step variable as an argument in the call to %abc&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But, if you have the macro list of countries in West Africa, you don't need further macros to do this. You can use data step code, and the data step INDEX or FIND function to achieve what you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;UNTESTED CODE&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set have;
    if find("&amp;amp;westafrlist",country1,'it')&amp;gt;0 then region_ind=1;
    else region_ind=0;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As a side comment, using readable variable names and readable macro variable names (as shown in my example)would be very helpful to you when you program, and to us in this forum when we are trying to help, rather than &amp;amp;dv and %abc and &amp;amp;findabc.&lt;/P&gt;</description>
      <pubDate>Mon, 02 Jul 2018 17:14:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-I-use-INDEX-to-search-a-macro-list/m-p/474986#M285962</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-07-02T17:14:13Z</dc:date>
    </item>
    <item>
      <title>Re: Can I use %INDEX to search a macro list?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-I-use-INDEX-to-search-a-macro-list/m-p/474988#M285963</link>
      <description>&lt;P&gt;Very odd. I tried this method using in my proc sql like so:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;select compress(WestAfr,"'") into :WestAfrlist separated by ' '&lt;/P&gt;&lt;P&gt;from work.data&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and that didn't work (I had Cote d'Ivoire, thus the compress)!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, your quote(trim( worked perfectly. I'll have to check the SAS docs to get the specifics on quote trim. Thank you Tom and thank you Paige!&lt;/P&gt;</description>
      <pubDate>Mon, 02 Jul 2018 17:14:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-I-use-INDEX-to-search-a-macro-list/m-p/474988#M285963</guid>
      <dc:creator>AreYouLikeNew</dc:creator>
      <dc:date>2018-07-02T17:14:43Z</dc:date>
    </item>
    <item>
      <title>Re: Can I use %INDEX to search a macro list?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-I-use-INDEX-to-search-a-macro-list/m-p/474994#M285964</link>
      <description>&lt;P&gt;In SAS code you need to put string literals in quotes so that SAS doesn't think you are referencing a variable.&lt;/P&gt;
&lt;P&gt;The QUOTE() function will add quotes around a value.&amp;nbsp; So&lt;/P&gt;
&lt;PRE&gt;Cote d'Ivoire&lt;/PRE&gt;
&lt;P&gt;becomes&lt;/P&gt;
&lt;PRE&gt;"Cote d'Ivoire"&lt;/PRE&gt;
&lt;P&gt;or if you used the optional second argument to tell&amp;nbsp; QUOTE() to use single quotes then it would become&lt;/P&gt;
&lt;PRE&gt;'Cote d''Ivoire'&lt;/PRE&gt;</description>
      <pubDate>Mon, 02 Jul 2018 17:45:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-I-use-INDEX-to-search-a-macro-list/m-p/474994#M285964</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-07-02T17:45:19Z</dc:date>
    </item>
  </channel>
</rss>

