<?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 define optional macro parameters? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-define-optional-macro-parameters/m-p/259139#M50076</link>
    <description>&lt;P&gt;So given you definition (&lt;SPAN&gt;%macro prog(infile, output, p1, p2, o1, o2);&amp;nbsp;... %mend;&amp;nbsp;) you could call it with any number of those parameter given a value.&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%prog ('myfile.csv', mydata, AGE, GENDER,AGE&amp;lt;15,GENDER='M'); 
%prog ('myfile.csv', mydata, AGE, GENDER);
%prog ('myfile.csv', mydata,,,AGE&amp;lt;15,GENDER='M'); &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;But also note one nice thing about SAS macros is that even though you have defined the macro with position arguments you can still specify the values by name.&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%prog 
(C2=GENDER='M'
,INFILE='myfile.csv'
,C1=AGE&amp;lt;15
,OUTPUT=mydata
,P1=AGE
,P2=GENDER
) ;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;Now inside the macro definition you need to figure out if you need do something different in the code generation based on whether the user has supplied a value for a particulr parameter. &amp;nbsp;For example C1 might be used as the condition to apply in a WHERE statment. &amp;nbsp;So you might code it this way.&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if %length(&amp;amp;c1) %then %do;
  where &amp;amp;c1 ;
%end ;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 25 Mar 2016 19:54:40 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2016-03-25T19:54:40Z</dc:date>
    <item>
      <title>how to define optional macro parameters?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-define-optional-macro-parameters/m-p/259131#M50072</link>
      <description>&lt;P&gt;Hi there, I have a basic question (maybe). I am a new learner for SAS macro. I try to define some optional parameters that I may use in the macro or may not. But I am not sure how to do it. Specifically,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro prog (infile, output, p1, p2, o1, o2)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;in the above macro, I would like to input infile name, output name, parameter 1 and 2 (p1 and p2), and with two optional parameters o1 and o2. My purpose is to use them if I call them (by including them in the program), while not use them if I omit them in the program. Please let me know how to do it. thanks.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Mar 2016 19:40:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-define-optional-macro-parameters/m-p/259131#M50072</guid>
      <dc:creator>SeanZ</dc:creator>
      <dc:date>2016-03-25T19:40:17Z</dc:date>
    </item>
    <item>
      <title>Re: how to define optional macro parameters?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-define-optional-macro-parameters/m-p/259139#M50076</link>
      <description>&lt;P&gt;So given you definition (&lt;SPAN&gt;%macro prog(infile, output, p1, p2, o1, o2);&amp;nbsp;... %mend;&amp;nbsp;) you could call it with any number of those parameter given a value.&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%prog ('myfile.csv', mydata, AGE, GENDER,AGE&amp;lt;15,GENDER='M'); 
%prog ('myfile.csv', mydata, AGE, GENDER);
%prog ('myfile.csv', mydata,,,AGE&amp;lt;15,GENDER='M'); &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;But also note one nice thing about SAS macros is that even though you have defined the macro with position arguments you can still specify the values by name.&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%prog 
(C2=GENDER='M'
,INFILE='myfile.csv'
,C1=AGE&amp;lt;15
,OUTPUT=mydata
,P1=AGE
,P2=GENDER
) ;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;Now inside the macro definition you need to figure out if you need do something different in the code generation based on whether the user has supplied a value for a particulr parameter. &amp;nbsp;For example C1 might be used as the condition to apply in a WHERE statment. &amp;nbsp;So you might code it this way.&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if %length(&amp;amp;c1) %then %do;
  where &amp;amp;c1 ;
%end ;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Mar 2016 19:54:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-define-optional-macro-parameters/m-p/259139#M50076</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2016-03-25T19:54:40Z</dc:date>
    </item>
    <item>
      <title>Re: how to define optional macro parameters?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-define-optional-macro-parameters/m-p/259146#M50079</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/52375"&gt;@SeanZ﻿&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As a minor addition to what Tom has written: You can also specify default values to parameters in the %MACRO statement, which will be used if the respective parameter is not specified in the macro call.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro prog(infile, output, p1, p2, o1=365, o2=C:\Temp);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In Tom's example of a WHERE condition you could write &lt;FONT face="courier new,courier"&gt;c1=1&lt;/FONT&gt;, i.e. use 1 as the default value. "&lt;FONT face="courier new,courier"&gt;where 1;&lt;/FONT&gt;" (after resolution of macro code) is a valid statement and 1 is always true, so you could safely omit the %LENGTH check.&lt;/P&gt;</description>
      <pubDate>Fri, 25 Mar 2016 20:20:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-define-optional-macro-parameters/m-p/259146#M50079</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-03-25T20:20:30Z</dc:date>
    </item>
  </channel>
</rss>

