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.
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.
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
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.
Thanks. Your solution is exactly what I was looking for.
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: