BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
victhor
Fluorite | Level 6

Good Morning

 

I am new to SAS but have quite a bit of experience with SQL (Oracle and SQL Server).

 

I have two datasets that I am trying to merge together, but one has a couple of additional variables.

 

The first something like

idnamestreetcitystate
1aaa111citynamexx
2bbb222citynamexx
3ccc333citynamexx
4ddd444citynamexx

 

The second something like:

idnamestreetaptcitycountystate
5eee555#313citynamecountynamexx
6fff666 citynamecountynamexx
7ggg777#acitynamecountynamexx
8hh888 citynamecountynamexx

 

And this is what I want:

idnamestreetaptcitycountystate
1aaa111 cityname xx
2bbb222 cityname xx
3ccc333 cityname xx
4ddd444 cityname xx
5eee555#313citynamecountynamexx
6fff666 citynamecountynamexx
7ggg777#acitynamecountynamexx

 

I can do it in proc sql (union accounting for the creating the new variables in the first table) but I was wondering if there is a simpler way with native SAS code.

 

I have read a lot but can't find a solution.  This may be sort of duh 101 stuff so my apologies for my ignorance in advance. 😁 

 

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

You are not "merging" , which is a row , usually matching, operation. This is appending or stacking.

With different variables then

 

 

data want;
   set have1
         have2
        <other data sets if needed>
   ;
run;

Warning: same named variables in the two sets must be the same type, either numeric or character in both.

 

Warning: if the same named variables are character but have different lengths you WILL get a warning in the log about mismatched lengths and possible truncation of data. The easiest fix in that case is to place a LENGTH statement for the variable before the set statement to set the length to the longest needed.

Suppose one of the sets has a different length then you would use code like below to set the length to the longer of the two sets.

 

data want;
   length street $ 45;
   set have1
         have2
        <other data sets if needed>
   ;
run;

 

View solution in original post

2 REPLIES 2
ballardw
Super User

You are not "merging" , which is a row , usually matching, operation. This is appending or stacking.

With different variables then

 

 

data want;
   set have1
         have2
        <other data sets if needed>
   ;
run;

Warning: same named variables in the two sets must be the same type, either numeric or character in both.

 

Warning: if the same named variables are character but have different lengths you WILL get a warning in the log about mismatched lengths and possible truncation of data. The easiest fix in that case is to place a LENGTH statement for the variable before the set statement to set the length to the longest needed.

Suppose one of the sets has a different length then you would use code like below to set the length to the longer of the two sets.

 

data want;
   length street $ 45;
   set have1
         have2
        <other data sets if needed>
   ;
run;

 

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 950 views
  • 3 likes
  • 2 in conversation