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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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