I am trying to use the same code for production and test. The goal is to have a variable at the top of the program to tell me if the environment is production or test, then this variable would be used in conditional statements.
I need to do 2 things:
1. Set different SQL Server ODBC connection strings based on whether I am in production or test
2. Set a variable to different directories for libnames and filename and ods rtf statements
I am very open to suggestions. Here is what I am trying:
For #1, this works:
/* Set up SQL server connections */
%macro SetSQL(Environment = Production);
%if "&Environment." = "Production" %then %do;
/* Production environment */
libname SQL odbc
noprompt="DRIVER=SQL Server;SERVER=xxxxxx;WSID=dbo;DATABASE=xxxxx;Trusted_Connection=Yes"
preserve_tab_names=yes Ignore_Read_Only_Columns=Yes;
%end;
%else %if "&Environment." = "Validation" %then %do;
/* Validation environment */
libname SQL odbc
noprompt="DRIVER=SQL Server;SERVER=yyyyyyy;WSID=dbo;DATABASE=yyyyy;Trusted_Connection=Yes"
preserve_tab_names=yes Ignore_Read_Only_Columns=Yes;
%end;
%mend;
/* Options are Production or Validation */
%SetSQL (Environment = Validatation);
Trying the same approach for #2 does not work. Are variables assigned in a macro local variables? Is there a way to make them global variables?
%let Environment = Production; /* Production or Validation */
%macro SetDir;
%if "&Environment." = "Production" %then
%let SICD_Dir = C:\Prod;
%else
%let SICD_Dir = C:\VAL;
%put "&SICD_Dir."; /* This prints the correct value to the log */
%mend;
%SetDir;
data _null_;
a = "&SICD_Dir.";
b = "&Environment.";
put a = b =;
run;
/* Error on the null data step for a = "&SICD_Dir."; line */
WARNING: Apparent symbolic reference SICD_DIR not resolved.
ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, a numeric constant,
a datetime constant, a missing value, INPUT, PUT.
*/
I would then use the &SICD_Dir variable this way:
libname raw "&SICD_Dir\Data";
Any help would be wonderful!
The issue is SICD_DIR is only defined within the macro, meaning it's a local variable. Try adding the following to the top of your program:
%global SICD_DIR;
The issue is SICD_DIR is only defined within the macro, meaning it's a local variable. Try adding the following to the top of your program:
%global SICD_DIR;
Thank you, Paul! That fixed it!
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.