Is there a magic 'Auto-Rename' option? No, not natively in the Data Step. But here are the three solutions proposed by experts to get around this chore.
1. The SQL Solution: Using Aliases
This is often the cleanest alternative. Unlike the Data Step, PROC SQL allows you to handle two columns with the same name by prefixing them with their source table (alias).
You don't need to rename the variables before the calculation; you do it on the fly.
proc sql;
create table test as
select a.id,
a.X,
b.X as X_Beta, /* Renommage simple pour la sortie */
(a.X - b.X) as Diff_X, /* Calcul direct sans renommage préalable */
(a.Y - b.Y) as Diff_Y
from alpha as a,
beta as b
where a.id = b.id;
quit;
1
PROC SQL;
2
create TABLE test as
3
select a.id,
4
a.X,
5
b.X as X_Beta, /* Renommage simple pour la sortie */
6
(a.X - b.X) as Diff_X, /* Calcul direct sans renommage préalable */
7
(a.Y - b.Y) as Diff_Y
8
from alpha as a,
9
beta as b
10
where a.id = b.id;
11
QUIT;
Advantage: More readable code and direct mathematical logic.
proc compare base=alpha compare=beta;
id id;
var X Y Z;
run;
1
PROC COMPARE base=alpha compare=beta;
2
id id;
3
var X Y Z;
4
RUN;
3. The "Hacker" Solution: Dynamic Macro
If you absolutely insist on using a Data StepMERGE and you have 100 variables to rename, the solution is to generate the RENAME=(...) list dynamically via a Macro.
The principle is as follows:
Read the metadata of the BETA table (via PROC CONTENTS or dictionary views).
Build a character string: X=X_Beta Y=Y_Beta ....
Inject this string into the code.
%macro auto_rename(lib, ds, suffix);
/* Code simplifié pour l'exemple */
proc sql noprint;
select catx('=', name, catx('_', name, "&suffix"))
into :renamelist separated by ' '
from dictionary.columns
where libname=upcase("&lib") and memname=upcase("&ds");
quit;
/* Renvoie la chaîne générée */
&renamelist
%mend;
/* Utilisation */
data test;
merge alpha
beta(rename=( %auto_rename(WORK, BETA, Beta) ));
by id;
run;
Die auf WeAreCAS.eu bereitgestellten Codes und Beispiele dienen Lehrzwecken. Es ist zwingend erforderlich, sie nicht blind in Ihre Produktionsumgebungen zu kopieren. Der beste Ansatz besteht darin, die Logik zu verstehen, bevor sie angewendet wird. Wir empfehlen dringend, diese Skripte in einer Testumgebung (Sandbox/Dev) zu testen. WeAreCAS übernimmt keine Verantwortung für mögliche Auswirkungen oder Datenverluste auf Ihren Systemen.
SAS und alle anderen Produkt- oder Dienstleistungsnamen von SAS Institute Inc. sind eingetragene Marken oder Marken von SAS Institute Inc. in den USA und anderen Ländern. ® zeigt die Registrierung in den USA an. WeAreCAS ist eine unabhängige Community-Site und nicht mit SAS Institute Inc. verbunden.
Diese Website verwendet technische und analytische Cookies, um Ihre Erfahrung zu verbessern.
Mehr erfahren.