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.
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.
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.
I find @Reeza Solution much elegant than what I provided.
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.
did not think of libname function.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.