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

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                                

 

1 ACCEPTED SOLUTION

Accepted Solutions
kannand
Lapis Lazuli | Level 10

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

4 REPLIES 4
kannand
Lapis Lazuli | Level 10

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
kannand
Lapis Lazuli | Level 10

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

Kannan Deivasigamani
DingDing
Quartz | Level 8
You are the most experienced man I have ever seen. Thank you, so nice of you !
kannand
Lapis Lazuli | Level 10

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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 4 replies
  • 6940 views
  • 1 like
  • 2 in conversation