BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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
2 REPLIES 2
deleted_user
Not applicable
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.
deleted_user
Not applicable
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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 2 replies
  • 1008 views
  • 0 likes
  • 1 in conversation