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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 796 views
  • 0 likes
  • 1 in conversation