As the first step in the decommissioning of sasCommunity.org the site has been converted to read-only mode.


Here are some tips for How to share your SAS knowledge with your professional network.


Difference between revisions of "Tips:Setting Default Values for Macro Parameters"

From sasCommunity
Jump to: navigation, search
m (gardening)
m
 
Line 1: Line 1:
Quite often it is necessary to set a default value for a [[macro]] parameter that is passed to a program. If the program is a macro, the %IF statement can be used to check if the value is provided and provide a default value if not. Since <big><source lang="sas" enclose="none">%IF</source></big> [[statements]] are not allowed outside of macros, that technique won't work for a non-macro program (e.g., a file/program that is '''%INCLUDE'''d). The following code snippet demonstrates the use of the '''COALESCEC''' function to assign a default value.
+
Quite often it is necessary to set a default value for a [[macro]] parameter that is passed to a program. If the program is a macro, the <big><source lang="sas" enclose="none">%IF</source></big> [[statement]] can be used to check if the value is provided and provide a default value if not. Since <big><source lang="sas" enclose="none">%IF</source></big> statements are not allowed outside of macros, that technique won't work for a non-macro program (e.g., a file/program that is '''%INCLUDE'''d). The following code snippet demonstrates the use of the '''COALESCEC''' function to assign a default value.
 
<source lang="sas">
 
<source lang="sas">
 
%global myParameter;  /* ensure it exists */
 
%global myParameter;  /* ensure it exists */

Latest revision as of 14:53, 16 October 2017

Quite often it is necessary to set a default value for a macro parameter that is passed to a program. If the program is a macro, the %IF statement can be used to check if the value is provided and provide a default value if not. Since %IF statements are not allowed outside of macros, that technique won't work for a non-macro program (e.g., a file/program that is %INCLUDEd). The following code snippet demonstrates the use of the COALESCEC function to assign a default value.

%global myParameter;  /* ensure it exists */
%let myParameter = %sysfunc(coalescec(&myParameter,default-value));
  • if myParameter has a value, it will be used, otherwise default-value will be assigned as the value.
  • since macro is a text manipulation facility, the coalesceC function is used regardless of whether the expected value for the parameter is numeric (i.e., everything in macro is interpretted as a character string).
Submitted By Don Henderson

....see also