BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hi,

I have on question.I have two data sets with same variable names,How to merge two data sets with diff names,without using reaname function .by using arrays how can do?
Dataset 1,

X Y
a 23
b 24

dataset2

X Y
x 234
s 123

output

x1 y1 x2 y2
a 23 x 234
b 24 s 123.
3 REPLIES 3
deleted_user
Not applicable
why the restrictions ?
deleted_user
Not applicable
Becase in our dataset there are 25 field names.
deleted_user
Not applicable
since you want a common suffix, depending on the source data set, I don't understand why having many names causes the rename restriction, nor the suggestion of using an array. The "data dictionary" tables in a SAS environment, allow the names to be treated as data and stored for use in a macro variable, like[pre]
proc contents data= your.dataset1 noprint out= names1; run;
proc contents data= your.dataset2 noprint out= names2; run;
proc sql ;
select trim( name) !! '=' !! trim( name) !! '_1'
into :rename1 separated by ' '
from names1
where lowcase(name) ne "key_column_name"
;
select trim( name) !! '=' !! trim( name) !! '_2'
into :rename2 separated by ' '
from names2
where lowcase(name) ne "key_column_name"
;
quit;[/pre]
That prepares the rename information without you having to name any columns, except logical exclusions from renaming, like the key fields.
Use this rename information in your merge step, like[pre]
data merged_data ;
merge your.dataset1( rename= (&rename1))
your.dataset2( rename= (&rename2)) ;
by key_column_name ;
run;
[/pre]
So, is there some real reason to use arrays and avoid the rename processing ?

PeterC

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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