<?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: How to use macro parameter as keyword to select variables from a dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-macro-parameter-as-keyword-to-select-variables-from-a/m-p/776626#M247032</link>
    <description>You can resolve &amp;amp;var using double-quotes instead of single-quotes.;</description>
    <pubDate>Tue, 26 Oct 2021 21:46:55 GMT</pubDate>
    <dc:creator>data_null__</dc:creator>
    <dc:date>2021-10-26T21:46:55Z</dc:date>
    <item>
      <title>How to use macro parameter as keyword to select variables from a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-macro-parameter-as-keyword-to-select-variables-from-a/m-p/776621#M247030</link>
      <description>&lt;P&gt;I have a dataset that contains over 70 variables. For example, the variable name is like "dataname_parameter", I want to use '&amp;amp;parameter' to select variables from the dataset in macro. I know my code is not correct, can't use &amp;amp;var inside quotation mark but I don't know how to fix it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro select(var);&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;filename tmp temp;&lt;/P&gt;&lt;P&gt;data _null_;&lt;BR /&gt;file tmp;&lt;BR /&gt;if 0 then set data1;&lt;BR /&gt;length varname $32;&lt;BR /&gt;do until (varname='varname');&lt;BR /&gt;call vnext(varname);&lt;BR /&gt;if index(varname,'&amp;amp;var') then put varname;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;set data1;&lt;BR /&gt;keep&lt;BR /&gt;%include tmp;&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%mend;&lt;/P&gt;&lt;P&gt;%select(SNO);&lt;/P&gt;</description>
      <pubDate>Tue, 26 Oct 2021 21:41:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-macro-parameter-as-keyword-to-select-variables-from-a/m-p/776621#M247030</guid>
      <dc:creator>tutu_</dc:creator>
      <dc:date>2021-10-26T21:41:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to use macro parameter as keyword to select variables from a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-macro-parameter-as-keyword-to-select-variables-from-a/m-p/776623#M247031</link>
      <description>Can you query the SASHELP.VCOLUMNS/dictionary.columns data set instead? Your requirements are a bit unclear but querying the dictionary tables seems to make this easier. &lt;BR /&gt;&lt;BR /&gt;You CAN use macro variables inside double quotations perfectly fine by the way, just not single quotes.</description>
      <pubDate>Tue, 26 Oct 2021 21:44:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-macro-parameter-as-keyword-to-select-variables-from-a/m-p/776623#M247031</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-10-26T21:44:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to use macro parameter as keyword to select variables from a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-macro-parameter-as-keyword-to-select-variables-from-a/m-p/776626#M247032</link>
      <description>You can resolve &amp;amp;var using double-quotes instead of single-quotes.;</description>
      <pubDate>Tue, 26 Oct 2021 21:46:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-macro-parameter-as-keyword-to-select-variables-from-a/m-p/776626#M247032</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2021-10-26T21:46:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to use macro parameter as keyword to select variables from a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-macro-parameter-as-keyword-to-select-variables-from-a/m-p/776630#M247033</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; options mprint symbolgen;

%macro select(lib =, ds_in=, pattern=, ds_out=);

proc sql noprint;
select name into :var_list separated by ' '
from dictionary.columns
where libname = upcase("&amp;amp;lib")
and memname = upcase("&amp;amp;ds_in")
and find(name,  "&amp;amp;pattern.", 'it')&amp;gt;0;
quit;


data &amp;amp;ds_out;
set &amp;amp;lib..&amp;amp;ds_in.;
keep &amp;amp;var_list.;
run;

%mend;

%select(lib=sashelp, ds_in=cars, pattern=mpg, ds_out=demo);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;A more robust solution in the long run. Depends on how generic you need it to be.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://gist.github.com/statgeek/8f90f5c545e01c48e4aaafb82d1a8ae8" target="_blank"&gt;https://gist.github.com/statgeek/8f90f5c545e01c48e4aaafb82d1a8ae8&lt;/A&gt;&lt;/P&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/404436"&gt;@tutu_&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have a dataset that contains over 70 variables. For example, the variable name is like "dataname_parameter", I want to use '&amp;amp;parameter' to select variables from the dataset in macro. I know my code is not correct, can't use &amp;amp;var inside quotation mark but I don't know how to fix it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro select(var);&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;filename tmp temp;&lt;/P&gt;
&lt;P&gt;data _null_;&lt;BR /&gt;file tmp;&lt;BR /&gt;if 0 then set data1;&lt;BR /&gt;length varname $32;&lt;BR /&gt;do until (varname='varname');&lt;BR /&gt;call vnext(varname);&lt;BR /&gt;if index(varname,'&amp;amp;var') then put varname;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;set data1;&lt;BR /&gt;keep&lt;BR /&gt;%include tmp;&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%mend;&lt;/P&gt;
&lt;P&gt;%select(SNO);&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Oct 2021 21:59:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-macro-parameter-as-keyword-to-select-variables-from-a/m-p/776630#M247033</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-10-26T21:59:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to use macro parameter as keyword to select variables from a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-macro-parameter-as-keyword-to-select-variables-from-a/m-p/776642#M247035</link>
      <description>&lt;P&gt;The macro processor ignores text inside of single quotes.&amp;nbsp; You need to use double quote characters instead.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But your code is way too complicated.&amp;nbsp; Plus you probably want to ignore the case of the variable names and protect yourself against non-standard variable names.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro select(var);
proc sql noprint;
select nliteral(name) into :var separated by ' '
  from dictionary.columns
  where libname='WORK' and memname='DATA1'
    and find(name,"&amp;amp;var",'i')
  ;
quit;
data want;
  set data1;
  keep &amp;amp;var ;
run;
%mend;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 27 Oct 2021 00:07:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-macro-parameter-as-keyword-to-select-variables-from-a/m-p/776642#M247035</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-10-27T00:07:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to use macro parameter as keyword to select variables from a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-macro-parameter-as-keyword-to-select-variables-from-a/m-p/776643#M247036</link>
      <description>NLITERAL is a great addition to the solution!</description>
      <pubDate>Wed, 27 Oct 2021 00:08:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-macro-parameter-as-keyword-to-select-variables-from-a/m-p/776643#M247036</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-10-27T00:08:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to use macro parameter as keyword to select variables from a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-macro-parameter-as-keyword-to-select-variables-from-a/m-p/776652#M247040</link>
      <description>&lt;P&gt;Thank you! I tried your code and it went well!&lt;/P&gt;</description>
      <pubDate>Wed, 27 Oct 2021 02:36:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-macro-parameter-as-keyword-to-select-variables-from-a/m-p/776652#M247040</guid>
      <dc:creator>tutu_</dc:creator>
      <dc:date>2021-10-27T02:36:01Z</dc:date>
    </item>
  </channel>
</rss>

