<?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: dynamically selecting variables in SQL in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/dynamically-selecting-variables-in-SQL/m-p/578881#M164256</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/226886"&gt;@Jody_H&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I was hoping to avoid coding a list of variables for each group. I was&amp;nbsp;thinking I could have a table of variables and groups and passing information into the SQL, but wasn't sure how to pass that information into the SQL.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If, for example, you have a text file that shows all 50 groups, and the variables needed from each group, then yes this could be made dynamic. Your read the text file into a SAS data set first. Then, it would either require a macro or CALL EXECUTE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is a macro solution, where the input text file has the group number separated by a space and then all the desired variable name(s), separated by space(s). I use data set ABC to hold this text information.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data abc;
    infile cards truncover;
    input string $200.;
cards;
1 temperature wind rain
18 homerun single  double triple
;

data _null_;
    set abc end=eof;
    string=compbl(string);
    group=scan(string,1);
    call symputx('group'||left(_n_),cats('group',group));
    call symputx('text'||left(_n_),translate(trim(left(substr(string,length(group)+1))),',',' '));
    if eof then call symputx('nrows',_n_);
run;


%macro dothis;
proc sql;
    %do i=1 %to &amp;amp;nrows;
        create table &amp;amp;&amp;amp;group&amp;amp;i as select &amp;amp;&amp;amp;text&amp;amp;i from yourdatabase;
    %end;
quit;
%mend;

options mprint;
%dothis
options nomprint;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 03 Aug 2019 11:22:43 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2019-08-03T11:22:43Z</dc:date>
    <item>
      <title>dynamically selecting variables in SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/dynamically-selecting-variables-in-SQL/m-p/578786#M164214</link>
      <description>&lt;P&gt;I'm trying to consolidate some code. Currently, I have:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For&amp;nbsp;group&amp;nbsp;X:&lt;/P&gt;&lt;P&gt;PROC&amp;nbsp; SQL;&lt;/P&gt;&lt;P&gt;select&amp;nbsp;Var_A, Var_B, Var_C&lt;/P&gt;&lt;P&gt;from mytable;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For&amp;nbsp;group Y:&lt;/P&gt;&lt;P&gt;Proc SQL;&lt;/P&gt;&lt;P&gt;Select Var_A, Var_C, Var_D&lt;/P&gt;&lt;P&gt;from mytable;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;is there a way to say;&lt;/P&gt;&lt;P&gt;Proc SQL;&lt;/P&gt;&lt;P&gt;Select [variables contingent on group]&lt;/P&gt;&lt;P&gt;from mytable;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;FYI-using SAS EG 7.15 with connection to Hadoop data.&lt;/P&gt;&lt;P&gt;My actual data has 50 groups and over 100 variables, so I'd like to make this as dynamic as possible. The key point is I don't want all of my variables for all of my groups. Thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Aug 2019 17:35:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/dynamically-selecting-variables-in-SQL/m-p/578786#M164214</guid>
      <dc:creator>Jody_H</dc:creator>
      <dc:date>2019-08-02T17:35:09Z</dc:date>
    </item>
    <item>
      <title>Re: dynamically selecting variables in SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/dynamically-selecting-variables-in-SQL/m-p/578787#M164215</link>
      <description>&lt;P&gt;Is group some variable in the dataset/database?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How would someone writing this code know what variables are to be extracted for each group? (We can't give you dynamic code based on your two groups shown)&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;What is the desired output? One SAS data set, many SAS data sets, or printed/html results?&lt;/P&gt;</description>
      <pubDate>Fri, 02 Aug 2019 17:55:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/dynamically-selecting-variables-in-SQL/m-p/578787#M164215</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-08-02T17:55:42Z</dc:date>
    </item>
    <item>
      <title>Re: dynamically selecting variables in SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/dynamically-selecting-variables-in-SQL/m-p/578788#M164216</link>
      <description>&lt;P&gt;Do you need something more "dynamic" than CASE WHEN expressions?&lt;/P&gt;</description>
      <pubDate>Fri, 02 Aug 2019 17:50:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/dynamically-selecting-variables-in-SQL/m-p/578788#M164216</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-08-02T17:50:54Z</dc:date>
    </item>
    <item>
      <title>Re: dynamically selecting variables in SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/dynamically-selecting-variables-in-SQL/m-p/578791#M164218</link>
      <description>&lt;P&gt;You could do something like this:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Create a macro variable for each group&lt;OL&gt;&lt;LI&gt;Using numbers instead of letters makes it easier to iterate&lt;/LI&gt;&lt;/OL&gt;&lt;/LI&gt;&lt;LI&gt;Use a %do loop to run all your sql queries sequentially&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;You would somehow need to get your variables into their group lists. Copy/paste from excel?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let group1=Var_A, Var_B, Var_C;
%let group2=Var_A, Var_C, Var_D;
/*...*/

%let table=mytable;
%let num_groups=2;

%macro loop;
	proc sql;
		%do i = 1 %to &amp;amp;num_groups;
			select &amp;amp;&amp;amp;group&amp;amp;i
			from &amp;amp;table;
		%end;
	quit;

%mend;
%loop

*testing;
%let group1=make, model;
%let group2=make, type;
%let table=sashelp.cars;
%let num_groups=2;
%loop&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Aug 2019 17:55:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/dynamically-selecting-variables-in-SQL/m-p/578791#M164218</guid>
      <dc:creator>noling</dc:creator>
      <dc:date>2019-08-02T17:55:54Z</dc:date>
    </item>
    <item>
      <title>Re: dynamically selecting variables in SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/dynamically-selecting-variables-in-SQL/m-p/578792#M164219</link>
      <description>&lt;P&gt;Yes, group is a variable in the dataset.&lt;/P&gt;&lt;P&gt;The goal is separate datasets for each group with the appropriate variables in each group.&lt;/P&gt;&lt;P&gt;&amp;nbsp;Thanks for the responses so far, based on the number of groups and number of variables, I was hoping to avoid coding a list of variables for each group. I was&amp;nbsp;thinking I could have a table of variables and groups and passing information into the SQL, but wasn't sure how to pass that information into the SQL.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Aug 2019 18:00:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/dynamically-selecting-variables-in-SQL/m-p/578792#M164219</guid>
      <dc:creator>Jody_H</dc:creator>
      <dc:date>2019-08-02T18:00:17Z</dc:date>
    </item>
    <item>
      <title>Re: dynamically selecting variables in SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/dynamically-selecting-variables-in-SQL/m-p/578793#M164220</link>
      <description>&lt;P&gt;At some point you or a system will have to specify which variables are in which groups. You could have this process be driven by a csv file or other flat file - 1 record per group with each field holding a variable in that group. This would help maintain your groups for changes. Read that csv file into SAS, then dynamically create the group macro variables each containing their list of fields. Then pass those group macro variables into your sql query.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Aug 2019 18:13:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/dynamically-selecting-variables-in-SQL/m-p/578793#M164220</guid>
      <dc:creator>noling</dc:creator>
      <dc:date>2019-08-02T18:13:28Z</dc:date>
    </item>
    <item>
      <title>Re: dynamically selecting variables in SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/dynamically-selecting-variables-in-SQL/m-p/578881#M164256</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/226886"&gt;@Jody_H&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I was hoping to avoid coding a list of variables for each group. I was&amp;nbsp;thinking I could have a table of variables and groups and passing information into the SQL, but wasn't sure how to pass that information into the SQL.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If, for example, you have a text file that shows all 50 groups, and the variables needed from each group, then yes this could be made dynamic. Your read the text file into a SAS data set first. Then, it would either require a macro or CALL EXECUTE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is a macro solution, where the input text file has the group number separated by a space and then all the desired variable name(s), separated by space(s). I use data set ABC to hold this text information.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data abc;
    infile cards truncover;
    input string $200.;
cards;
1 temperature wind rain
18 homerun single  double triple
;

data _null_;
    set abc end=eof;
    string=compbl(string);
    group=scan(string,1);
    call symputx('group'||left(_n_),cats('group',group));
    call symputx('text'||left(_n_),translate(trim(left(substr(string,length(group)+1))),',',' '));
    if eof then call symputx('nrows',_n_);
run;


%macro dothis;
proc sql;
    %do i=1 %to &amp;amp;nrows;
        create table &amp;amp;&amp;amp;group&amp;amp;i as select &amp;amp;&amp;amp;text&amp;amp;i from yourdatabase;
    %end;
quit;
%mend;

options mprint;
%dothis
options nomprint;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 03 Aug 2019 11:22:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/dynamically-selecting-variables-in-SQL/m-p/578881#M164256</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-08-03T11:22:43Z</dc:date>
    </item>
  </channel>
</rss>

