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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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