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

Is there a way to conditionally assign a library?  I have an EG project (egp file) with a prompt, and I want the user to pick which of 4 databases they want to connect to.  If they pick 2, then I only want to invoke the libname statements for the 2 they selected.  I have 4 macro variables that indicate whether they want the library assigned or not.

 

It is not working to put the libname statement in a select or an if-then-else because it gets invoked regardless.

1 ACCEPTED SOLUTION

Accepted Solutions
smantha
Lapis Lazuli | Level 10

Can you show some code. If mac variables are mac1 , mac2, mac3 and mac4 and they have a boolean of YES or NO you can do the following

%macro assign_libname;
%if &mac1 = YES %then %do;
data _NULL_;
call execute(<libname statement 1>);
run;
%end;
%if &mac1 = YES %then %do;
data _NULL_;
call execute(<libname statement 1>);
run;
%end;
%if &mac2 = YES %then %do;
data _NULL_;
call execute(<libname statement 2>);
run;
%end;
%if &mac3 = YES %then %do;
data _NULL_;
call execute(<libname statement 3>);
run;
%end;
%if &mac4 = YES %then %do;
data _NULL_;
call execute(<libname statement 4>);
run;
%end;
%mend assign_libname;
%assign_libname;

my guess is libname in open code is compile time statement and hence you see the behavior.

View solution in original post

6 REPLIES 6
smantha
Lapis Lazuli | Level 10

Can you show some code. If mac variables are mac1 , mac2, mac3 and mac4 and they have a boolean of YES or NO you can do the following

%macro assign_libname;
%if &mac1 = YES %then %do;
data _NULL_;
call execute(<libname statement 1>);
run;
%end;
%if &mac1 = YES %then %do;
data _NULL_;
call execute(<libname statement 1>);
run;
%end;
%if &mac2 = YES %then %do;
data _NULL_;
call execute(<libname statement 2>);
run;
%end;
%if &mac3 = YES %then %do;
data _NULL_;
call execute(<libname statement 3>);
run;
%end;
%if &mac4 = YES %then %do;
data _NULL_;
call execute(<libname statement 4>);
run;
%end;
%mend assign_libname;
%assign_libname;

my guess is libname in open code is compile time statement and hence you see the behavior.

MrsC
Calcite | Level 5
CALL EXECUTE! Of course! Thank you so much.
smantha
Lapis Lazuli | Level 10

I find @Reeza  Solution much elegant than what I provided.

Reeza
Super User

Use the libname function. 

 

data _null_;

if "&mac1" = "Y" then libname(name, path);
if "&mac2" = "Y" then libname(......);
if "&mac3" = "Y" then libname(......);
if "&mac4" = "Y" then libname(......);

run;

@MrsC wrote:

Is there a way to conditionally assign a library?  I have an EG project (egp file) with a prompt, and I want the user to pick which of 4 databases they want to connect to.  If they pick 2, then I only want to invoke the libname statements for the 2 they selected.  I have 4 macro variables that indicate whether they want the library assigned or not.

 

It is not working to put the libname statement in a select or an if-then-else because it gets invoked regardless.


 

smantha
Lapis Lazuli | Level 10

did not think of libname function.

Reeza
Super User
I likely have the benefit of seeing this problem before 🙂
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
  • 6 replies
  • 2377 views
  • 3 likes
  • 3 in conversation