DATA Step, Macro, Functions and more

Merge two datasets without common variable

Accepted Solution Solved
Reply
Contributor
Posts: 48
Accepted Solution

Merge two datasets without common variable

I want to merge two datasets  the variable in dataset 1is IATA and in dataset2 is UNIQUE_CARRIER. In the output, I want to remove the obserbations that "IATA" does'not have. Generally I know use the "PROC merge" like that. but it seems it only works when they have common variable. I don't know how to do if they don't have common name of a variable. could anybody give me some instructions?

 

 

 

DATA  dataset3;
MERGE dataset1 (IN=a) 
dataset2 (IN=b );
By common_variable
IF a=1;
RUN;

 

one is 

 

IATA  

 

B1      

OO     

QU      

 

UNIQUE_CARRIER   

 

AA

AA

B1                               

OO                              

QU                                

 


Accepted Solutions
Solution
‎10-25-2015 06:44 PM
Regular Contributor
Posts: 161

Re: Merge two datasets without common variable

[ Edited ]

Rename the key variable for the merge as shown below and it should take care of your problem.

DATA IATA  ;
 INPUT IATA:$2.;
DATALINES;
B1      
OO     
QU      
;
RUN;
DATA UNIQUE_CARRIER ;
 INPUT UNIQUE_CARRIER:$2.;
DATALINES; 
AA
AA
B1                               
OO                              
QU  
;
RUN;
PROC SORT DATA=IATA;
  BY IATA;
RUN;
PROC SORT DATA=UNIQUE_CARRIER(RENAME=UNIQUE_CARRIER=IATA);
  BY IATA;
RUN;
DATA NEW_IATA;
     MERGE IATA(IN=A)
           UNIQUE_CARRIER(IN=B);
        BY IATA;
     IF A;
RUN;
PROC PRINT;
RUN;

The output it generates is here for your reference:

 


Obs	IATA
1	B1
2	OO
3	QU

 

Kannan Deivasigamani

View solution in original post


All Replies
Solution
‎10-25-2015 06:44 PM
Regular Contributor
Posts: 161

Re: Merge two datasets without common variable

[ Edited ]

Rename the key variable for the merge as shown below and it should take care of your problem.

DATA IATA  ;
 INPUT IATA:$2.;
DATALINES;
B1      
OO     
QU      
;
RUN;
DATA UNIQUE_CARRIER ;
 INPUT UNIQUE_CARRIER:$2.;
DATALINES; 
AA
AA
B1                               
OO                              
QU  
;
RUN;
PROC SORT DATA=IATA;
  BY IATA;
RUN;
PROC SORT DATA=UNIQUE_CARRIER(RENAME=UNIQUE_CARRIER=IATA);
  BY IATA;
RUN;
DATA NEW_IATA;
     MERGE IATA(IN=A)
           UNIQUE_CARRIER(IN=B);
        BY IATA;
     IF A;
RUN;
PROC PRINT;
RUN;

The output it generates is here for your reference:

 


Obs	IATA
1	B1
2	OO
3	QU

 

Kannan Deivasigamani
Regular Contributor
Posts: 161

Re: Merge two datasets without common variable

I should have mentioned that the rename is in the sort step in my code above

Kannan Deivasigamani
Contributor
Posts: 48

Re: Merge two datasets without common variable

You are the most experienced man I have ever seen. Thank you, so nice of you !
Regular Contributor
Posts: 161

Re: Merge two datasets without common variable

[ Edited ]

Thanks for your kind words. I'm learning too. There are much more knowledgeable and experienced experts in this forum, you'll notice.  Not to mention, there are employees of SAS as well, you are here to help us.

 

Good luck and have fun with SAS DingDing.

Kannan Deivasigamani
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 573 views
  • 1 like
  • 2 in conversation