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
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
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
I should have mentioned that the rename is in the sort step in my code above
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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.