<?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: Using WHERE to find a variable value in an SQL macro generated list in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-WHERE-to-find-a-variable-value-in-an-SQL-macro-generated/m-p/398415#M96395</link>
    <description>Brilliant, thanks Kurt</description>
    <pubDate>Sun, 24 Sep 2017 21:37:55 GMT</pubDate>
    <dc:creator>robulon</dc:creator>
    <dc:date>2017-09-24T21:37:55Z</dc:date>
    <item>
      <title>Using WHERE to find a variable value in an SQL macro generated list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-WHERE-to-find-a-variable-value-in-an-SQL-macro-generated/m-p/398378#M96368</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm using SAS Univeristy Edition and I've created a new variable by concatenating two existing character variables and have then used proc SQL to create a macro variable list of the concatenated values when other criteria is met.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I'm then trying to do is identify variables from a different dataset (where I have also concatenated the same two variables) that match one of the values form the macro list. I thought I should be able to do this using the FINDW function by specifying&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;where findw(&amp;amp;a_rt_list,a_rt) &amp;gt; 0;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;but am getting a syntax error when parsing the WHERE clause. Any advice would be gratefully received, I've included the code and log detail below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	create table alarm_test_4 as
	select
		 a.*
		,compress(a.alarms)||compress(a.racetype) as a_rt
	from alarm_test_2 as a,
	alarm_test_b as b
	where a.alarms = b.alarms;
quit;

proc sql noprint;
	select 
		a_rt
	into :a_rt_list separated by ' '
	from alarm_test_4
	where bfvo_roi &amp;gt;= 1.1;
quit;

%put &amp;amp;a_rt_list;

data test;
	set dailies_remerge (keep= alarms racetype);
	where not missing (alarms);
	a_rt = compress(alarms)||compress(racetype);
run;

data test_2;
	set test;
		where findw(&amp;amp;a_rt_list,a_rt) &amp;gt; 0;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;62         proc sql;
 63         create table alarm_test_4 as
 64         select
 65          a.*
 66         ,compress(a.alarms)||compress(a.racetype) as a_rt
 67         from alarm_test_2 as a,
 68         alarm_test_b as b
 69         where a.alarms = b.alarms;
 NOTE: Table USER.ALARM_TEST_4 created, with 31 rows and 12 columns.
 
 70         quit;
 NOTE: PROCEDURE SQL used (Total process time):
       real time           0.09 seconds
       cpu time            0.02 seconds
       
 
 71         
 72         proc sql noprint;
 73         select
 74         a_rt
 75         into :a_rt_list separated by ' '
 76         from alarm_test_4
 77         where bfvo_roi &amp;gt;= 1.1;
 78         quit;
 NOTE: PROCEDURE SQL used (Total process time):
       real time           0.00 seconds
       cpu time            0.00 seconds
       
 
 79         
 80         %put &amp;amp;a_rt_list;
 +DGHURDLE +WCHASE +WFLAT -B&amp;gt;CHASE -B&amp;gt;FLAT 7BCHASE BMAW BMCHASE BMFLAT BMHURDLE BMNHFLA TPAW TPCHASE TPFLAT TPNHFLA
 81         
 82         data test;
 83         set dailies_remerge (keep= alarms racetype);
 84         where not missing (alarms);
 85         a_rt = compress(alarms)||compress(racetype);
 86         run;
 
 NOTE: There were 126 observations read from the data set USER.DAILIES_REMERGE.
       WHERE not MISSING(alarms);
 NOTE: The data set USER.TEST has 126 observations and 3 variables.
 NOTE: DATA statement used (Total process time):
       real time           0.06 seconds
       cpu time            0.02 seconds
       
 
 87         
 88         data test_2;
 89         set test;
 90         where findw(&amp;amp;a_rt_list,a_rt) &amp;gt; 0;
 NOTE: Line generated by the macro variable "A_RT_LIST".
 90         +DGHURDLE +WCHASE +WFLAT -B&amp;gt;CHASE -B&amp;gt;FLAT 7BCHASE BMAW BMCHASE BMFLAT BMHURDLE BMNHFLA TPAW TPCHASE TPFLAT TPNHFLA
                                                      _
                                                      22
                                                      76
 ERROR: Syntax error while parsing WHERE clause.
 ERROR 22-322: Syntax error, expecting one of the following: !, !!, &amp;amp;, (, ), *, **, +, ',', -, /, &amp;lt;, &amp;lt;=, &amp;lt;&amp;gt;, =, &amp;gt;, &amp;gt;=, AND, EQ, GE, 
               GT, LE, LT, NE, NOT, OR, ^, ^=, |, ||, ~, ~=.  
 
 ERROR 76-322: Syntax error, statement will be ignored.
 
 91         run;
 
 NOTE: The SAS System stopped processing this step because of errors.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Many thanks,&lt;/P&gt;&lt;P&gt;Rob&lt;/P&gt;</description>
      <pubDate>Sun, 24 Sep 2017 12:40:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-WHERE-to-find-a-variable-value-in-an-SQL-macro-generated/m-p/398378#M96368</guid>
      <dc:creator>robulon</dc:creator>
      <dc:date>2017-09-24T12:40:55Z</dc:date>
    </item>
    <item>
      <title>Re: Using WHERE to find a variable value in an SQL macro generated list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-WHERE-to-find-a-variable-value-in-an-SQL-macro-generated/m-p/398380#M96369</link>
      <description>&lt;P&gt;findw() expects strings, so you must use double quotes around your macro variable.&lt;/P&gt;</description>
      <pubDate>Sun, 24 Sep 2017 13:58:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-WHERE-to-find-a-variable-value-in-an-SQL-macro-generated/m-p/398380#M96369</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-09-24T13:58:43Z</dc:date>
    </item>
    <item>
      <title>Re: Using WHERE to find a variable value in an SQL macro generated list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-WHERE-to-find-a-variable-value-in-an-SQL-macro-generated/m-p/398387#M96372</link>
      <description>&lt;P&gt;Seems pretty clear in the log. So you can see in the log that your macro variable has something like&lt;/P&gt;
&lt;PRE&gt;+DGHURDLE +WCHASE +WFLAT&lt;/PRE&gt;
&lt;P&gt;So when you use like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where findw(&amp;amp;a_rt_list,a_rt) &amp;gt; 0;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You end up generating code that looks like this.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where findw(+DGHURDLE +WCHASE +WFLAT,a_rt) &amp;gt; 0;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which SAS properly rejects as gibberish.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How would you change that last statement to be valid syntax? &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You would need to make the first argument to FINDW() be a string expression. &amp;nbsp;So it needs quotes.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where findw("&amp;amp;a_rt_list",a_rt) &amp;gt; 0;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 24 Sep 2017 14:31:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-WHERE-to-find-a-variable-value-in-an-SQL-macro-generated/m-p/398387#M96372</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-09-24T14:31:29Z</dc:date>
    </item>
    <item>
      <title>Re: Using WHERE to find a variable value in an SQL macro generated list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-WHERE-to-find-a-variable-value-in-an-SQL-macro-generated/m-p/398391#M96375</link>
      <description>&lt;P&gt;Although you can technically do this, and it's obviously possible, I suspect there's an alternative method that may be more efficient.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Something like the following perhaps?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table vars_in as
select *
from table1 
where concatenated_key not in (select concatenated_key from table2);
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 24 Sep 2017 15:54:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-WHERE-to-find-a-variable-value-in-an-SQL-macro-generated/m-p/398391#M96375</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-09-24T15:54:44Z</dc:date>
    </item>
    <item>
      <title>Re: Using WHERE to find a variable value in an SQL macro generated list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-WHERE-to-find-a-variable-value-in-an-SQL-macro-generated/m-p/398410#M96390</link>
      <description>&lt;P&gt;Thanks Tom &amp;amp; Kurt for your help, and thanks Reeza, I've no doubt that what you say is correct but I'm just trying to get to grips with creating macro variables using proc sql.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;That said, I'm still struggling with this and can't work out why. Using the same code as previously, I've tried creating seperate macro variables and that works fine: -&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; 62         /* Create sql macro variables from profitable alarms */
 63         proc sql noprint;
 64         select
 65         a_rt
 66         into :a_rt_list_1 - :a_rt_list_15
 67         from alarm_test_4
 68         where bfvo_roi &amp;gt;= 1.1;
 69         quit;
 NOTE: PROCEDURE SQL used (Total process time):
       real time           0.00 seconds
       cpu time            0.01 seconds
       
 
 70         
 71         %put &amp;amp;a_rt_list;
 AWTP CHASETP FLATTP NHFLATP AWBM CHASEBM FLATBM HURDLEBM NHFLABM CHASE-B&amp;gt; FLAT-B&amp;gt; CHASE+W FLAT+W CHASE7B HURDLE+DG
 72         
 73         data test;
 74         set dailies_remerge (keep= alarms time racetype);
 75         where not missing (alarms);
 76         a_rt = compress(racetype)||compress(alarms);
 77         run;
 
 NOTE: There were 126 observations read from the data set USER.DAILIES_REMERGE.
       WHERE not MISSING(alarms);
 NOTE: The data set USER.TEST has 126 observations and 4 variables.
 NOTE: DATA statement used (Total process time):
       real time           0.14 seconds
       cpu time            0.01 seconds
       
 
 78         
 79         data test_2;
 80         set test;
 81         where a_rt in ("&amp;amp;a_rt_list_1"  "&amp;amp;a_rt_list_2"  "&amp;amp;a_rt_list_3"  "&amp;amp;a_rt_list_4"  "&amp;amp;a_rt_list_5"  "&amp;amp;a_rt_list_6"
 82         "&amp;amp;a_rt_list_7"  "&amp;amp;a_rt_list_8"  "&amp;amp;a_rt_list_9"  "&amp;amp;a_rt_list_10"  "&amp;amp;a_rt_list_11"  "&amp;amp;a_rt_list_12"
 83         "&amp;amp;a_rt_list_13"  "&amp;amp;a_rt_list_14"  "&amp;amp;a_rt_list_15");
 84         run;
 
 NOTE: There were 1 observations read from the data set USER.TEST.
       WHERE a_rt in ('AWBM', 'AWTP', 'CHASE+W', 'CHASE-B&amp;gt;', 'CHASE7B', 'CHASEBM', 'CHASETP', 'FLAT+W', 'FLAT-B&amp;gt;', 'FLATBM', 
       'FLATTP', 'HURDLE+DG', 'HURDLEBM', 'NHFLABM', 'NHFLATP');
 NOTE: The data set USER.TEST_2 has 1 observations and 4 variables.
 NOTE: DATA statement used (Total process time):
       real time           0.07 seconds
       cpu time            0.01 seconds&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But when I'm creating the list that contains the same values using the 'separated by' method, it doesn;t work: -&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; 62         proc sql noprint;
 63         select
 64         a_rt
 65         into :a_rt_list separated by ' '
 66         from alarm_test_4
 67         where bfvo_roi &amp;gt;= 1.1;
 68         quit;
 NOTE: PROCEDURE SQL used (Total process time):
       real time           0.00 seconds
       cpu time            0.00 seconds
       
 
 69         
 70         %put &amp;amp;a_rt_list;
 AWTP CHASETP FLATTP NHFLATP AWBM CHASEBM FLATBM HURDLEBM NHFLABM CHASE-B&amp;gt; FLAT-B&amp;gt; CHASE+W FLAT+W CHASE7B HURDLE+DG
 71         
 72         data test;
 73         set dailies_remerge (keep= alarms racetype horse);
 74         where not missing (alarms);
 75         a_rt = compress(racetype)||compress(alarms);
 76         run;
 
 NOTE: There were 126 observations read from the data set USER.DAILIES_REMERGE.
       WHERE not MISSING(alarms);
 NOTE: The data set USER.TEST has 126 observations and 4 variables.
 NOTE: DATA statement used (Total process time):
       real time           0.05 seconds
       cpu time            0.01 seconds
       
 
 77         
 78         data test_2;
 79         set test;
 80         where findw("&amp;amp;a_rt_list",a_rt) &amp;gt; 0;
 81         run;
 
 NOTE: There were 0 observations read from the data set USER.TEST.
       WHERE FINDW('AWTP CHASETP FLATTP NHFLATP AWBM CHASEBM FLATBM HURDLEBM NHFLABM CHASE-B&amp;gt; FLAT-B&amp;gt; CHASE+W FLAT+W CHASE7B 
       HURDLE+DG', a_rt)&amp;gt;0;
 NOTE: The data set USER.TEST_2 has 0 observations and 4 variables.
 NOTE: DATA statement used (Total process time):
       real time           0.06 seconds
       cpu time            0.01 seconds&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Whether it has any specific relevance or not, I'm not sure but the characters that the code is finding in the first iteration but not the second is FLAT-B&amp;gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Again, any suggestions gratefully received.&lt;/P&gt;&lt;P&gt;Rob&lt;/P&gt;</description>
      <pubDate>Sun, 24 Sep 2017 21:07:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-WHERE-to-find-a-variable-value-in-an-SQL-macro-generated/m-p/398410#M96390</guid>
      <dc:creator>robulon</dc:creator>
      <dc:date>2017-09-24T21:07:15Z</dc:date>
    </item>
    <item>
      <title>Re: Using WHERE to find a variable value in an SQL macro generated list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-WHERE-to-find-a-variable-value-in-an-SQL-macro-generated/m-p/398414#M96394</link>
      <description>&lt;P&gt;Either use the "R" modifier in the findw() function, or use trim() to get rid of trailing blanks:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where findw("&amp;amp;a_rt_list",a_rt,' ','R') &amp;gt; 0;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where findw("&amp;amp;a_rt_list",trim(a_rt)) &amp;gt; 0;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 24 Sep 2017 21:33:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-WHERE-to-find-a-variable-value-in-an-SQL-macro-generated/m-p/398414#M96394</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-09-24T21:33:45Z</dc:date>
    </item>
    <item>
      <title>Re: Using WHERE to find a variable value in an SQL macro generated list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-WHERE-to-find-a-variable-value-in-an-SQL-macro-generated/m-p/398415#M96395</link>
      <description>Brilliant, thanks Kurt</description>
      <pubDate>Sun, 24 Sep 2017 21:37:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-WHERE-to-find-a-variable-value-in-an-SQL-macro-generated/m-p/398415#M96395</guid>
      <dc:creator>robulon</dc:creator>
      <dc:date>2017-09-24T21:37:55Z</dc:date>
    </item>
    <item>
      <title>Re: Using WHERE to find a variable value in an SQL macro generated list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-WHERE-to-find-a-variable-value-in-an-SQL-macro-generated/m-p/398416#M96396</link>
      <description>&lt;P&gt;Making a lot of individual macro variables is probably not effecient. (unless you expect lots of value, in which case use a join instead of macro variables).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select quote(trim(a_rt))
  into :a_rt_list separated by ' '
  from alarm_test_4
  where bfvo_roi &amp;gt;= 1.1
;
quit;
...
where a_rt in (&amp;amp;a_rt_list)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 24 Sep 2017 21:53:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-WHERE-to-find-a-variable-value-in-an-SQL-macro-generated/m-p/398416#M96396</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-09-24T21:53:31Z</dc:date>
    </item>
  </channel>
</rss>

