DATA Step, Macro, Functions and more

How do I set up my code so it can run in production or test based on a variable?

Accepted Solution Solved
Reply
Contributor
Posts: 22
Accepted Solution

How do I set up my code so it can run in production or test based on a variable?

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!


Accepted Solutions
Solution
‎05-16-2017 05:42 PM
Regular Contributor
Posts: 175

Re: How do I set up my code so it can run in production or test based on a variable?

Posted in reply to sschleede

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;

View solution in original post


All Replies
Solution
‎05-16-2017 05:42 PM
Regular Contributor
Posts: 175

Re: How do I set up my code so it can run in production or test based on a variable?

Posted in reply to sschleede

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;
Contributor
Posts: 22

Re: How do I set up my code so it can run in production or test based on a variable?

Posted in reply to paulkaefer

Thank you, Paul! That fixed it!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 144 views
  • 1 like
  • 2 in conversation