DATA Step, Macro, Functions and more

Auto renaming in data step

Reply
N/A
Posts: 0

Auto renaming in data step

Hello,

I'm quite often comparing datasets with the same variable names.
For this purpose I often use data steps like the following:

DATA TEST;
MERGE ALPHA(IN=IN1 KEEP=A X Y Z)
BETA (IN=IN2 KEEP=A X Y Z
RENAME=(X=X_Beta Y=Y_Beta Z=Z_Beta));
BY A;
IF IN1;
X_DIF=X-X_Beta;
RUN;

My question is whether it is possible to do renaming automatically? Giving all variables the same suffix (in this case "_Beta")? A bit in the same way as the "\autoname" option in the proc means statement?

I often have to rename >30 variables, can it really be true that I have to make a datastep before the merge datastep?

I'm very thankful for any answer.
/Hans
N/A
Posts: 0

Re: Auto renaming in data step

While we usually argue against the use of macros for some things, this is a perfect (even classic) case where a macro is precisely what is needed.

I'd have to do some research for the details, but that would rob you of the fun and learning.

Write a macro that uses dataset information functions to determine the names of the variables in a given dataset, then your could code
[pre]
DATA TEST;
MERGE ALPHA(IN=IN1 KEEP=A X Y Z)
BETA (IN=IN2 KEEP=A X Y Z %rename_vars(BETA));
BY A;
IF IN1;
X_DIF=X-X_Beta;
RUN;
[/pre]

Just for grins and giggles, have you considered using SQL to do this for you?
[pre]
proc sql;
create table test as
select a, x,y,z, (a.x - b.x) as x_dif, (a.y - b.y) as y_dif, (a.z - b.z) as z_dif
from ALPHA as a, BETA as b
where a.A = b.A;
quit;
[/pre]
This could be macroized as well, starting with
[pre]
%macro diffs(ALPHA, BETA, a, x, y, z);
proc sql;
create table test as
select &a, &x,&y,&z, (a.&x - b.&x) as &x._dif, (a.&y - b.&y) as &y._dif, (a.&z - b.&z) as &z._dif
from &ALPHA as a, &BETA as b
where a&a = b.&a;
quit;
%mend;
[/pre]
and it could be further generalized using the ???parm??? thingy for macros to allow for variable length parameter lists.
N/A
Posts: 0

Re: Auto renaming in data step

sometimes the difficulty of doing something in base SAS (without writing macros) is "no bad thing"!.
Getting a $0.01 for every situation where BY-processing is the substitute for %macro iteration, would make us wealthy.
Sometimes there is an alternative to data step MERGE for bringing data together.

Some situations achieve the simple effect required using UPDATE instead of MERGE. Other situations benefit from use of MODIFY for merge/update-type processing.

Comparing datasets offers another opportunity to step away from MERGE. Two alternatives come to mind:
If a datastep solution is really needed, you can also use interleaving SET, but really, PROC COMPARE is shouting to be heard ;-)

PeterC
Ask a Question
Discussion stats
  • 2 replies
  • 122 views
  • 0 likes
  • 1 in conversation