<?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: Generating dynamic (where)based on table in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Generating-dynamic-where-based-on-table/m-p/522395#M141812</link>
    <description>It's entirely possible though a left join would definitely be faster. Create two sets of macro variables, one that holds the group and one that holds the amounts. &lt;BR /&gt;Then loop through them and call the macro as desired.</description>
    <pubDate>Wed, 19 Dec 2018 00:38:07 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2018-12-19T00:38:07Z</dc:date>
    <item>
      <title>Generating dynamic (where)based on table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generating-dynamic-where-based-on-table/m-p/522393#M141810</link>
      <description>&lt;P&gt;Hi everyone,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;a quick question. i have a table (Table A) that contains two columns looking like this&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 130pt;" border="0" width="174" cellspacing="0" cellpadding="0"&gt;
&lt;TBODY&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD width="87" height="21" style="height: 16.0pt; width: 65pt;"&gt;Group&lt;/TD&gt;
&lt;TD width="87" style="width: 65pt;"&gt;ID&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD height="21" style="height: 16.0pt;"&gt;A&lt;/TD&gt;
&lt;TD align="right"&gt;24&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD height="21" style="height: 16.0pt;"&gt;A&lt;/TD&gt;
&lt;TD align="right"&gt;54&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD height="21" style="height: 16.0pt;"&gt;A&lt;/TD&gt;
&lt;TD align="right"&gt;92&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD height="21" style="height: 16.0pt;"&gt;A&lt;/TD&gt;
&lt;TD align="right"&gt;6&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD height="21" style="height: 16.0pt;"&gt;A&lt;/TD&gt;
&lt;TD align="right"&gt;7&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD height="21" style="height: 16.0pt;"&gt;B&lt;/TD&gt;
&lt;TD align="right"&gt;25&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD height="21" style="height: 16.0pt;"&gt;B&lt;/TD&gt;
&lt;TD align="right"&gt;11&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD height="21" style="height: 16.0pt;"&gt;B&lt;/TD&gt;
&lt;TD align="right"&gt;97&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD height="21" style="height: 16.0pt;"&gt;C&lt;/TD&gt;
&lt;TD align="right"&gt;13&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD height="21" style="height: 16.0pt;"&gt;C&lt;/TD&gt;
&lt;TD align="right"&gt;18&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD height="21" style="height: 16.0pt;"&gt;D&lt;/TD&gt;
&lt;TD align="right"&gt;81&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD height="21" style="height: 16.0pt;"&gt;D&lt;/TD&gt;
&lt;TD align="right"&gt;10&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then I have a very large table (table B) from which i want to extract data based on the group and ID in a proc sql.&lt;/P&gt;
&lt;P&gt;For a number of reasons, I wont be able to use a left join or inner join statement, so I am trying to come up with a different approach that would look like this&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql,
create table TABLEC as
select *
from tableB
where &amp;amp;selection&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;the macro variable &amp;amp;selection should be generated dynamically based on tableA and should at the end look like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;(group=A and ID in&amp;nbsp;(24, , 54, 92, 6, 7) ) OR

(group=B and ID in (25, 11, 97)) OR

(group=C and ID in (13, 18)) OR

(group=D and ID in (81, 10))&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The number and name of groups is variable so the code should account for that too. The only thing that wont change is the name of columns (group, ID).&lt;/P&gt;
&lt;P&gt;Has anyone done something similar before?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yes, I know left join makes more sense but as I said, I need to this differently...&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;P&gt;Am&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Dec 2018 00:21:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generating-dynamic-where-based-on-table/m-p/522393#M141810</guid>
      <dc:creator>ammarhm</dc:creator>
      <dc:date>2018-12-19T00:21:03Z</dc:date>
    </item>
    <item>
      <title>Re: Generating dynamic (where)based on table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generating-dynamic-where-based-on-table/m-p/522395#M141812</link>
      <description>It's entirely possible though a left join would definitely be faster. Create two sets of macro variables, one that holds the group and one that holds the amounts. &lt;BR /&gt;Then loop through them and call the macro as desired.</description>
      <pubDate>Wed, 19 Dec 2018 00:38:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generating-dynamic-where-based-on-table/m-p/522395#M141812</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-12-19T00:38:07Z</dc:date>
    </item>
    <item>
      <title>Re: Generating dynamic (where)based on table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generating-dynamic-where-based-on-table/m-p/522398#M141813</link>
      <description>&lt;P&gt;Here's an example of how you can implement this. The exact implementation for your data set will be up to you. Note where the semicolons are, they are important in this type of problem.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;*create fake data;

data selection;
    input sex $ age;
    cards;
F 12
F 13
F 16
M 12
M 15
M 16
;
    ;
    ;
    ;
run;

*sort - can be assumed;

proc sort data=selection;
    by sex age;
run;

*create macro variables;

data _null_;
    set selection end=eof;
    *list has to be large enough to store the values;
    length list $200.;
    by sex;
    *keep values for last row to create macro variables;
    retain counter list;

    if first.sex then
        do;
            counter+1;
            call missing(list);
        end;
    list=catx(',', list, age);
    *create macro variables at the last record of each sex;

    if last.sex then
        do;
            call symputx(catt('group', counter), sex);
            call symputx(catt('member', counter), list);

            if eof then
                call symputx('Nloops', counter);
            output;
        end;
run;

*Test the macro variable creations;
%put &amp;amp;group1;
%put &amp;amp;group2.;
%put &amp;amp;member1;
%put &amp;amp;member2;
%put &amp;amp;nloops;
*macro to generate the conditions desired;

%macro loop_conditions();
    %do i=1 %to &amp;amp;nloops;
        %*build your criteria here, note the lack of a semicolon at the end;
        (Sex="&amp;amp;&amp;amp;Group&amp;amp;i." %str(AND) Age in (&amp;amp;&amp;amp;member&amp;amp;i)) %*add OR except for last;

        %if &amp;amp;i ^=&amp;amp;nloops %then
            %str(OR);
    %end;
    ;
%mend;

*example usage;

proc sql;
    create table want as 
    select * 
    from sashelp.class 
    where %loop_conditions();
quit;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/24798"&gt;@ammarhm&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi everyone,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;a quick question. i have a table (Table A) that contains two columns looking like this&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 130pt;" border="0" width="174" cellspacing="0" cellpadding="0"&gt;
&lt;TBODY&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD width="87" height="21" style="height: 16.0pt; width: 65pt;"&gt;Group&lt;/TD&gt;
&lt;TD width="87" style="width: 65pt;"&gt;ID&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD height="21" style="height: 16.0pt;"&gt;A&lt;/TD&gt;
&lt;TD align="right"&gt;24&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD height="21" style="height: 16.0pt;"&gt;A&lt;/TD&gt;
&lt;TD align="right"&gt;54&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD height="21" style="height: 16.0pt;"&gt;A&lt;/TD&gt;
&lt;TD align="right"&gt;92&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD height="21" style="height: 16.0pt;"&gt;A&lt;/TD&gt;
&lt;TD align="right"&gt;6&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD height="21" style="height: 16.0pt;"&gt;A&lt;/TD&gt;
&lt;TD align="right"&gt;7&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD height="21" style="height: 16.0pt;"&gt;B&lt;/TD&gt;
&lt;TD align="right"&gt;25&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD height="21" style="height: 16.0pt;"&gt;B&lt;/TD&gt;
&lt;TD align="right"&gt;11&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD height="21" style="height: 16.0pt;"&gt;B&lt;/TD&gt;
&lt;TD align="right"&gt;97&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD height="21" style="height: 16.0pt;"&gt;C&lt;/TD&gt;
&lt;TD align="right"&gt;13&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD height="21" style="height: 16.0pt;"&gt;C&lt;/TD&gt;
&lt;TD align="right"&gt;18&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD height="21" style="height: 16.0pt;"&gt;D&lt;/TD&gt;
&lt;TD align="right"&gt;81&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD height="21" style="height: 16.0pt;"&gt;D&lt;/TD&gt;
&lt;TD align="right"&gt;10&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then I have a very large table (table B) from which i want to extract data based on the group and ID in a proc sql.&lt;/P&gt;
&lt;P&gt;For a number of reasons, I wont be able to use a left join or inner join statement, so I am trying to come up with a different approach that would look like this&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql,
create table TABLEC as
select *
from tableB
where &amp;amp;selection&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;the macro variable &amp;amp;selection should be generated dynamically based on tableA and should at the end look like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;(group=A and ID in&amp;nbsp;(24, , 54, 92, 6, 7) ) OR

(group=B and ID in (25, 11, 97)) OR

(group=C and ID in (13, 18)) OR

(group=D and ID in (81, 10))&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The number and name of groups is variable so the code should account for that too. The only thing that wont change is the name of columns (group, ID).&lt;/P&gt;
&lt;P&gt;Has anyone done something similar before?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yes, I know left join makes more sense but as I said, I need to this differently...&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;P&gt;Am&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Dec 2018 01:01:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generating-dynamic-where-based-on-table/m-p/522398#M141813</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-12-19T01:01:29Z</dc:date>
    </item>
    <item>
      <title>Re: Generating dynamic (where)based on table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generating-dynamic-where-based-on-table/m-p/522399#M141814</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data a;
input Group $ ID;
cards;
A 24
A 54
A 92
A 6
A 7
B 25
B 11
B 97
C 13
C 18
D 81
D 10
;
run;

Data tmp;
length grp idp idp2 $25 final $100;
idp=' ';

do until(last.group);

	set a;
	by Group;
	
	idp=catx(',',idp,id);
end;

grp=quote(cats(Group)); 
idp2=cats('(',idp,')');

final=catt('(group= ',grp, ' and Id in ',idp2,')');

keep final;
run;


proc sql;
	Select final into :selection separated by ' OR '
	from tmp
	;
quit;

proc sql;
create table TABLEC as
select *
from tableB
where &amp;amp;selection;
quit;





&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 Dec 2018 01:16:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generating-dynamic-where-based-on-table/m-p/522399#M141814</guid>
      <dc:creator>r_behata</dc:creator>
      <dc:date>2018-12-19T01:16:32Z</dc:date>
    </item>
    <item>
      <title>Re: Generating dynamic (where)based on table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generating-dynamic-where-based-on-table/m-p/522403#M141816</link>
      <description>&lt;P&gt;Try this.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data list;
  infile datalines;
  input
  group: $1.
  id: $3.;
datalines;
A 24 
A 54 
A 92 
A 6 
A 7 
B 25 
B 11 
B 97 
C 13 
C 18 
D 81 
D 10 
;
run;


proc sql noprint;
  select catt(count(distinct group)) into :grpn from  list;

  select distinct group into :grp1-:grp&amp;amp;grpn. from list;
quit;

%macro test;
  %do i=1 %to &amp;amp;grpn.;
    
  proc sql noprint;
    select distinct id into: ids separated by "," 
    from list
		  where group="&amp;amp;&amp;amp;grp&amp;amp;i.";
  quit;
  
  %put where group="&amp;amp;&amp;amp;grp&amp;amp;i" and id in (&amp;amp;ids.);
  %end;
%mend;
%test;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 Dec 2018 01:43:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generating-dynamic-where-based-on-table/m-p/522403#M141816</guid>
      <dc:creator>ShiroAmada</dc:creator>
      <dc:date>2018-12-19T01:43:00Z</dc:date>
    </item>
  </channel>
</rss>

