<?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: Pass in varying number of values for query in a Macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Pass-in-varying-number-of-values-for-query-in-a-Macro/m-p/394971#M277980</link>
    <description>&lt;P&gt;I am not sure what you are asking at all. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you need help with passing empty values to a SAS macro? &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your code doesn't seem to involve a macro however, just a few macro variables. Are you actually writing a macro or just trying to use macro variables?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also your code seems to be all about calling some type of stored procedure in a database.&amp;nbsp;Do you know how that stored procedure works? &amp;nbsp;If so then it should be simple to generate the proper syntax in a SAS macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example if the stored procedure might want you to leave the parameter empty when the value is not specified.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;execute GETDATA "&amp;amp;Company",,"&amp;amp;Campaign"&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or perhaps type the keyword NULL instead.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;execute GETDATA "&amp;amp;Company",NULL,"&amp;amp;Campaign"&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;One way to do this is to just use a little %IF/%THEN processing to set the macro variables properly.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if %length(&amp;amp;company) %then %let company=%sysfunc(quote(&amp;amp;company));
%if %length(&amp;amp;product) %then %let product=%sysfunc(quote(&amp;amp;product));
%if %length(&amp;amp;Campaign) %then %let Campaign=%sysfunc(quote(&amp;amp;Campaign));
...
execute GETDATA &amp;amp;Company,&amp;amp;product,&amp;amp;Campaign&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you don't actually have SAS macro then you can use IF/THEN processing in a data step instead to modify the macro variables. &amp;nbsp;For example to replace COMPANY with either NULL or the value of company in quotes you do use this IF/THEN/ELSE&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  if not missing(symget('company')) then call symputx('company',quote(symget('company'));
  else call symputx('company','null');
...
&lt;/CODE&gt;&lt;/PRE&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;</description>
    <pubDate>Tue, 12 Sep 2017 02:44:43 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2017-09-12T02:44:43Z</dc:date>
    <item>
      <title>Pass in varying number of values for query in a Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Pass-in-varying-number-of-values-for-query-in-a-Macro/m-p/394959#M277979</link>
      <description>&lt;P&gt;I'm working with a stored procedure that pulls data via an ODBC connection &amp;nbsp;using proc sql. In particular, there are 3 columns that hold values for which I would like to shape my query in either the proq sql step or a secondary data step. The columns are basically hierachical, Company, then Product, then Campaign. Sometimes there Company, Product, and Campaign values are all the same while other companys might have X amount of products and Y amount of campaigns.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'd like to be able to pass values to the macro via "%let" statements, but sometimes it is unnecessary and/or redundant to give values for Product and Campaign. Other times I might want to specify one product and many times I will want to specify 1 or more campaigns.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The goal is to automate the query such that we can add in varying numbers of macro parameters such that if no value for a particular column is passed the query simply does not use it as a condition.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let Company &amp;nbsp;= ABCD;&lt;/P&gt;&lt;P&gt;%let Product = ;&lt;/P&gt;&lt;P&gt;%let Campaign = ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;proc sql;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;connect to odbc (dsn='DW' uid='**********'&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;pwd='*************');&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;create table work.table as&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;select *&amp;nbsp;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;from connection to odbc (execute&amp;nbsp;GETDATA&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;"&amp;amp;Company","&amp;amp;Product","&amp;amp;Campaign")&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;disconnect from odbc;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;quit;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the case above, I would simply want the query to return me all data associated with Company ABCD. Since no Product of Campaign was assigned (or it's blank/missing), I would not want the query to subset the data based on those columns.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Additionally:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let Company &amp;nbsp;= EFGH;&lt;/P&gt;&lt;P&gt;%let Product = ZYX;&lt;/P&gt;&lt;P&gt;%let Campaign = DEF FED;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;connect to odbc (dsn='DW' uid='**********'&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;pwd='*************');&lt;BR /&gt;create table work.table as&lt;BR /&gt;select *&amp;nbsp;&lt;BR /&gt;from connection to odbc (execute&amp;nbsp;GETDATA&lt;BR /&gt;"&amp;amp;Company","&amp;amp;Product","&amp;amp;Campaign")&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;;&lt;BR /&gt;disconnect from odbc;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the case above, I would simply want the query to return me all data associated with the EFGH Company for their product ZYX and the campaigns for that product of DEF and FED.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It seems like it might be a better idea to execute this in a data step after the full data set has been brought in. Does anyone have an suggestions on dealing with optional macro parameters?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Michael&lt;/P&gt;</description>
      <pubDate>Tue, 12 Sep 2017 00:14:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Pass-in-varying-number-of-values-for-query-in-a-Macro/m-p/394959#M277979</guid>
      <dc:creator>SmcGarrett</dc:creator>
      <dc:date>2017-09-12T00:14:42Z</dc:date>
    </item>
    <item>
      <title>Re: Pass in varying number of values for query in a Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Pass-in-varying-number-of-values-for-query-in-a-Macro/m-p/394971#M277980</link>
      <description>&lt;P&gt;I am not sure what you are asking at all. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you need help with passing empty values to a SAS macro? &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your code doesn't seem to involve a macro however, just a few macro variables. Are you actually writing a macro or just trying to use macro variables?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also your code seems to be all about calling some type of stored procedure in a database.&amp;nbsp;Do you know how that stored procedure works? &amp;nbsp;If so then it should be simple to generate the proper syntax in a SAS macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example if the stored procedure might want you to leave the parameter empty when the value is not specified.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;execute GETDATA "&amp;amp;Company",,"&amp;amp;Campaign"&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or perhaps type the keyword NULL instead.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;execute GETDATA "&amp;amp;Company",NULL,"&amp;amp;Campaign"&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;One way to do this is to just use a little %IF/%THEN processing to set the macro variables properly.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if %length(&amp;amp;company) %then %let company=%sysfunc(quote(&amp;amp;company));
%if %length(&amp;amp;product) %then %let product=%sysfunc(quote(&amp;amp;product));
%if %length(&amp;amp;Campaign) %then %let Campaign=%sysfunc(quote(&amp;amp;Campaign));
...
execute GETDATA &amp;amp;Company,&amp;amp;product,&amp;amp;Campaign&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you don't actually have SAS macro then you can use IF/THEN processing in a data step instead to modify the macro variables. &amp;nbsp;For example to replace COMPANY with either NULL or the value of company in quotes you do use this IF/THEN/ELSE&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  if not missing(symget('company')) then call symputx('company',quote(symget('company'));
  else call symputx('company','null');
...
&lt;/CODE&gt;&lt;/PRE&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;</description>
      <pubDate>Tue, 12 Sep 2017 02:44:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Pass-in-varying-number-of-values-for-query-in-a-Macro/m-p/394971#M277980</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-09-12T02:44:43Z</dc:date>
    </item>
    <item>
      <title>Re: Pass in varying number of values for query in a Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Pass-in-varying-number-of-values-for-query-in-a-Macro/m-p/394975#M277981</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;%macro param(company,product,campaign);
data _null_;
exec=
tranwrd(
catx(",",
quote(coalescec("&amp;amp;company","")),
quote(coalescec("&amp;amp;product","")),
quote(coalescec("&amp;amp;campaign",""))
),
'""',"")
run;
%mend;

%param(you,,);
%param(you,wanna,);
%param(you,wanna,bet);
%param(,,);
%param(,wanna,bet);
%param(,,bet);
%param(,wanna,);
%param(you,,bet);

&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Sep 2017 03:44:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Pass-in-varying-number-of-values-for-query-in-a-Macro/m-p/394975#M277981</guid>
      <dc:creator>ShiroAmada</dc:creator>
      <dc:date>2017-09-12T03:44:55Z</dc:date>
    </item>
    <item>
      <title>Re: Pass in varying number of values for query in a Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Pass-in-varying-number-of-values-for-query-in-a-Macro/m-p/395160#M277982</link>
      <description>&lt;P&gt;Thanks for the response. This gives me a lot to work with.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The part that I may have been unclear about is that sometimes I may want to query more than one of any of these parameters. I will always ONLY want one Company but sometimes 2 or more products and most of the time 2 or more campaigns. Therefore, I'd like information on how to potentially pass a "list of values" through a macro parameter so that they data set created contains all observations for which the values are true. Assuming we call ALL the data through the stored procedure and then use a dataset to subset the data that we do want, are you familiar with how to pass a list of values through a macro parameter?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;A very ugly way of doing then might be (We'd macro this at some point):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let LIST_PRODUCTS = "P_One","P_Two","P_Three";&lt;/P&gt;&lt;P&gt;%let LIST_CAMPAIGNS =&amp;nbsp;&lt;SPAN&gt;"C_One","C_Two","C_Three";&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;set have;&lt;/P&gt;&lt;P&gt;where Product in (&amp;amp;LIST_PRODUCTS) and Campaign in (&amp;amp;List_CAMPAIGNS);&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, it seems very inefficient and like there could be a better way.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Sep 2017 16:08:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Pass-in-varying-number-of-values-for-query-in-a-Macro/m-p/395160#M277982</guid>
      <dc:creator>SmcGarrett</dc:creator>
      <dc:date>2017-09-12T16:08:03Z</dc:date>
    </item>
    <item>
      <title>Re: Pass in varying number of values for query in a Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Pass-in-varying-number-of-values-for-query-in-a-Macro/m-p/395166#M277983</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/80268"&gt;@SmcGarrett&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;A very ugly way of doing then might be (We'd macro this at some point):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let LIST_PRODUCTS = "P_One","P_Two","P_Three";&lt;/P&gt;
&lt;P&gt;%let LIST_CAMPAIGNS =&amp;nbsp;&lt;SPAN&gt;"C_One","C_Two","C_Three";&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;where Product in (&amp;amp;LIST_PRODUCTS) and Campaign in (&amp;amp;List_CAMPAIGNS);&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, it seems very inefficient and like there could be a better way.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I don't know about your stored process, but in pure SAS code passing a list is a good method. I wouldn't add the commas at it will just make the macro variable harder to deal with. &amp;nbsp;The IN () operator in SAS does not need the commas.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Sep 2017 16:53:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Pass-in-varying-number-of-values-for-query-in-a-Macro/m-p/395166#M277983</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-09-12T16:53:03Z</dc:date>
    </item>
  </channel>
</rss>

