Retrieve the list of names: Use dictionary views (SASHELP.VTABLE) to automatically list everything in a library.
Generate the code dynamically: Use the CALL EXECUTE statement in a Data Step to run the macro for each name found.
2. The Code Step by Step
Step 0: Define the Processing Macro
This is the code the user wants to execute for a single table.
%macro jig(name);
data datdir.NEW_&name;
/* Attention : les deux tables doivent être triées par ID au préalable */
merge excelsheetdata datdir.&name;
by ID;
run;
%mend jig;
1
%macro jig(name);
2
DATA datdir.NEW_&name;
3
/* Attention : les deux tables doivent être triées par ID au préalable */
proc sql;
create table liste_tables as
select memname
from sashelp.vtable
where libname='DATDIR' /* Nom de la lib en MAJUSCULES */
and memname like 'DATA_%'; /* Filtre optionnel si besoin */
quit;
1
PROC SQL;
2
create TABLE liste_tables as
3
select memname
4
from sashelp.vtable
5
where LIBNAME='DATDIR'/* Nom de la lib en MAJUSCULES */
6
and memname like 'DATA_%'; /* Filtre optionnel si besoin */
7
QUIT;
Note: SASHELP.VTABLE contains the metadata for all active tables.
Note : Step 2: Automation (CALL EXECUTE)
This is where the magic happens. We loop through the list created in Step 1, and for each row, we build a text command that calls the macro.
data _null_;
set liste_tables;
/* Construction de la chaîne : '%jig(TABLE_1)' */
commande = '%jig(' || strip(memname) || ')';
/* Exécution immédiate de cette commande */
call execute(commande);
run;
1
DATA _null_;
2
SET liste_tables;
3
4
/* Construction de la chaîne : '%jig(TABLE_1)' */
5
commande = '%jig(' || strip(memname) || ')';
6
7
/* Exécution immédiate de cette commande */
8
call execute(commande);
9
RUN;
3. Syntax Analysis of CALL EXECUTE
In the solution proposed by WeAreCas, you will notice a somewhat complex syntax:
Technical advice: Don't forget that for a MERGE to work, all your tables (all 50 of them!) must be previously sorted by the key variable (ID). If this is not the case, you will need to add a PROC SORT inside your %jig macro.
Important Disclaimer
The codes and examples provided on WeAreCAS.eu are for educational purposes. It is imperative not to blindly copy-paste them into your production environments. The best approach is to understand the logic before applying it. We strongly recommend testing these scripts in a test environment (Sandbox/Dev). WeAreCAS accepts no responsibility for any impact or data loss on your systems.
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.