SAS equivalent to SQL variables?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 138
Accepted Solution

SAS equivalent to SQL variables?

Hello,

 

As I am building different processes, mainly in Enterprise Guide but in base SAS as well, I would like to be able to set a variable that I can plug in to determine which library I am referencing for all of my data sets.  The primary purpose is to be able to switch back and forth between a development and production environment without having to change all of my code.  I have looked around and I am thinking maybe SAS macros are the answer but I am not completely sure.  I have not yet used them as I am still fairly new to SAS after a long history with SQL.

 

If someone could point me in the right direction I would be grateful!


Accepted Solutions
Solution
‎02-16-2016 09:01 AM
Esteemed Advisor
Esteemed Advisor
Posts: 7,250

Re: SAS equivalent to SQL variables?

Yes, probably a good fit for a macro variable:

libname prod "c:\production";
libname dev "c:\develop";

%let lname=prod; data &lname..xyz1; ... data &lname..abc; ... %let lname=dev; data &lname..xyz1; ...

So in this the first two steps run against a libname called prod, the last step runs against dev.

View solution in original post


All Replies
Solution
‎02-16-2016 09:01 AM
Esteemed Advisor
Esteemed Advisor
Posts: 7,250

Re: SAS equivalent to SQL variables?

Yes, probably a good fit for a macro variable:

libname prod "c:\production";
libname dev "c:\develop";

%let lname=prod; data &lname..xyz1; ... data &lname..abc; ... %let lname=dev; data &lname..xyz1; ...

So in this the first two steps run against a libname called prod, the last step runs against dev.

Frequent Contributor
Posts: 138

Re: SAS equivalent to SQL variables?

Ok, so I am new to macros.  Let's say I want to use this in Enterprise Guide where I have a process flow.  All of my tasks currently reference my static production library.  If I wanted to use this code, would I start the process with a program task to set the macro and then call it in each task? So, dev.dataset or prod.dataset?

Frequent Contributor
Posts: 138

Re: SAS equivalent to SQL variables?

I guess what I am really asking is how to use the macro so I never have to change the task, just the macro so the tasks dynamically change.

Esteemed Advisor
Esteemed Advisor
Posts: 7,250

Re: SAS equivalent to SQL variables?

Well, I am afraid it is a long long time since I used EG, so can't tell you much on that, but there must be a setup program - maybe thats is run first in the process, or part of the configuration - how else would you get the libraries?  I would agree with the other guys here, if it is just a case of what data the code uses, then changining the libname is probably the easiet.  However its likely that you will also want to run certain other things, or change the code based on DEV/PROD too, I mean things like pre-check data in dev for unclean data maybe.  If so, having a macro variable would allow you to macro condition run code based on that, although I suppose you could query the metadata tables each time.  

Respected Advisor
Posts: 4,998

Re: SAS equivalent to SQL variables?

Another possibility:  your code may be static containing either one of these:

 

libname lib 'production library';

libname lib 'development library';

 

You can query DICTIONARY.LIBNAMES to find the definition of LIB at any particular spot in a program.

Esteemed Advisor
Posts: 5,202

Re: SAS equivalent to SQL variables?

Why complicating with different librefs?
Just change the path in the libname statement depending on environment.
Data never sleeps
Frequent Contributor
Posts: 138

Re: SAS equivalent to SQL variables?

My thought was that because I have an autoexec that maps all of my libraries upon connection (including all of my dev and prod libraries) that I could have a variable that toggled them without having to rewrite the statements from the autoexec.

Respected Advisor
Posts: 3,066

Re: SAS equivalent to SQL variables?

Sounds like you are on the right track.

 

Another idea is to have a separate Prod and Dev autoexecs and switch between them using a macro variable:

%let Run_Type = dev;
*%let Run_Type = prod; * Comment with * in front.;

%include "autoexec_&Run_Type..sas";


CLUDE:

Frequent Contributor
Posts: 138

Re: SAS equivalent to SQL variables?

Thank you to everyone.  I will give these methods a try and report back soon.

Frequent Contributor
Posts: 138

Re: SAS equivalent to SQL variables?

Thanks again everyone.  I went with @RW9's solution but I tested all and they seemed to do the trick.  I appreciate it!

☑ This topic is SOLVED.

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

Discussion stats
  • 10 replies
  • 1389 views
  • 1 like
  • 5 in conversation