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

In a data step I would like to conditionally concatenate (stack) two SAS tables.  One of the tables may not always be created and would not need to be stacked.  When I have a blank library or table name, I am getting errors.  Here is the sample code.

 

LIBNAME LIB1 "Filepath";
LIBNAME LIB2 "";
%LET LIB1_TABLE = table_name;
%LET LIB2_TABLE = ;
%LET USE_LIB2 = N;

DATA STACK_TEST;
IF "&USE_LIB1" = "Y" THEN SET LIB1.&LIB1_TABLE. LIB2.&LIB2_TABLE.;
ELSE SET LIB1.&LIB1_TABLE.;
RUN;

With this setup, I would expect the code to revert to the else portion of the "if-then-else" but I get errors in my log.  

 

%LET LIB1_TABLE = table_name;
%LET LIB2_TABLE = ;
%LET USE_LIB2 = N;

DATA STACK_TEST;
IF "&USE_LIB2" = "Y" THEN SET LIB1.&LIB1_TABLE. LIB2.&LIB2_TABLE.;
NOTE: Line generated by the macro variable "LIB2_TABLE".
LIB2.
_____
22
201

ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, (, -, :, ;, CUROBS, END, INDSNAME, KEY,
KEYRESET, KEYS, NOBS, OPEN, POINT, _DATA_, _LAST_, _NULL_.

ERROR 201-322: The option is not recognized and will be ignored.

ELSE SET LIB1.&LIB1_TABLE.;
RUN;

 

Does someone have a solution on how to go about conditionally concatenating the two tables in a data step when one of the libraries is not populated because the table does not exist?  I am using SAS Enterprise Guide 6.1 (64-bit). I need to perform other tasks within this data step after getting the data from the table(s) which is why I am not using the Append procedure.  

 

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

IFC nested in SYSFUNC would be my prescription:

 

data stack_test;
   set lib1.&lib1_table  %sysfunc(ifc(&use_lib2=Y,lib2.&lib2_table,));
run;

 

%sysfunc around IFN or IFC is a great way to do the equivalent of a macro %IF test in open code.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

3 REPLIES 3
novinosrin
Tourmaline | Level 20

something like this can help i think

 


LIBNAME LIB1 "Filepath";
LIBNAME LIB2 "";
%LET LIB1_TABLE = table_name;
%LET LIB2_TABLE = ;
%LET USE_LIB2 = N;
%macro t;
DATA STACK_TEST;
%IF &USE_LIB1 = Y %THEN %str(SET LIB1.&LIB1_TABLE. LIB2.&LIB2_TABLE.;);
%ELSE %str(SET LIB1.&LIB1_TABLE.;);
RUN;
%mend t;
%t
mkeintz
PROC Star

IFC nested in SYSFUNC would be my prescription:

 

data stack_test;
   set lib1.&lib1_table  %sysfunc(ifc(&use_lib2=Y,lib2.&lib2_table,));
run;

 

%sysfunc around IFN or IFC is a great way to do the equivalent of a macro %IF test in open code.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Philmingo
Fluorite | Level 6

Thanks.  Your solution is exactly what I was looking for.  

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
  • 3 replies
  • 1723 views
  • 7 likes
  • 3 in conversation