<?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: Looking to loop through input from a SAS data table to query a SQL database in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Looking-to-loop-through-input-from-a-SAS-data-table-to-query-a/m-p/799318#M314298</link>
    <description>&lt;P&gt;Well, possibly I don't understand the entire task, but I don't think you need loops, and I don't think you need macros.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Something like this ought to work&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
    create table final as select a.x1,a.x2,a.x3 from data_table a
    left join test_input b where a.x1=b.x1 and a.x2=b.x2;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 01 Mar 2022 14:22:30 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2022-03-01T14:22:30Z</dc:date>
    <item>
      <title>Looking to loop through input from a SAS data table to query a SQL database</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looking-to-loop-through-input-from-a-SAS-data-table-to-query-a/m-p/799317#M314297</link>
      <description>&lt;P&gt;I have test_input which is table that I would like to loop through and query those combinations of variables x1 &amp;amp; x2 (("A", "A"), ("A", "B"), etc...).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What's the easiest and most effective way to do so?&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; Note, example code below shows a manual attempt. Then, I did an automatic approach where I create a concatenated column of multiple variables, create macro variable list, then loop through that list using scan(). It seems very complicated and a lot of code. Is there a better way?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Code:&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE&gt;DATA test_input;
	INPUT x1 $ x2 $;
	DATALINES;
A A
A B
A C
B A
B B
B C
;
RUN;

DATA data_table;
	INPUT x1 $ x2 $ x3;
	DATALINES;
A A 1
A A 2
A B 3
A B 4
A B 5
A B 6
B A 7
B B 8
B B 9
B B 10
B C 11
B C 12
B C 13
;
RUN;

/* Manual */
%PUT _ALL_;

%LET x1 = "A";
%LET x2 = "B";

PROC SQL;
	CREATE TABLE queried_data AS
	SELECT x1, x2, x3,
	       compress(catx(x1, "_", x2)) AS concat_condition
	FROM data_table
	WHERE x1 = &amp;amp;x1 AND x2 = &amp;amp;x2
	;
QUIT;

/* Automatic */

DATA test_input;
	SET test_input;
	concat_condition = compress(catx(x1, "_", x2));
RUN;

PROC SQL noprint;
	SELECT quote(trim(concat_condition))
	INTO :concat_condition SEPARATED BY "~"
	FROM test_input
	;
QUIT;

%MACRO loop_condition();
	%DO i=1 %TO 5;
		%LET cur_var = %scan(&amp;amp;concat_condition, &amp;amp;i, "~");
		PROC SQL;
			CREATE TABLE queried_data_&amp;amp;i AS
			SELECT x1, x2, x3,
			       compress(catx(x1, "_", x2)) AS concat_condition
			FROM data_table
			WHERE CALCULATED concat_condition = "&amp;amp;cur_var"
			;
		QUIT;
	%END;
%MEND;

%loop_condition();&lt;/PRE&gt;</description>
      <pubDate>Tue, 01 Mar 2022 14:15:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looking-to-loop-through-input-from-a-SAS-data-table-to-query-a/m-p/799317#M314297</guid>
      <dc:creator>narnia649</dc:creator>
      <dc:date>2022-03-01T14:15:57Z</dc:date>
    </item>
    <item>
      <title>Re: Looking to loop through input from a SAS data table to query a SQL database</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looking-to-loop-through-input-from-a-SAS-data-table-to-query-a/m-p/799318#M314298</link>
      <description>&lt;P&gt;Well, possibly I don't understand the entire task, but I don't think you need loops, and I don't think you need macros.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Something like this ought to work&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
    create table final as select a.x1,a.x2,a.x3 from data_table a
    left join test_input b where a.x1=b.x1 and a.x2=b.x2;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 01 Mar 2022 14:22:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looking-to-loop-through-input-from-a-SAS-data-table-to-query-a/m-p/799318#M314298</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-03-01T14:22:30Z</dc:date>
    </item>
  </channel>
</rss>

