BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sschleede
Obsidian | Level 7

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!

1 ACCEPTED SOLUTION

Accepted Solutions
paulkaefer
Lapis Lazuli | Level 10

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

2 REPLIES 2
paulkaefer
Lapis Lazuli | Level 10

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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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