View previous topic :: View next topic |
Author |
Message |
Bharath.AK Beginner
Joined: 13 Jul 2007 Posts: 34 Topics: 10 Location: Los Angeles
|
Posted: Thu Feb 18, 2010 3:20 am Post subject: SAS MERGE Issue - Please Recommend! |
|
|
Hi,
I have two datasets:
Dataset A:
Key: Misc:
AMC WIRE
AMC WIRE
AMC WIRE
ABD XXXX
Dataset B:
Key: Misc:
AMC NETTING
I need to merge these two datasets BY the variable KEY so that the output dataset should contain all the observations in Dataset A and the matched observations should have MISC variable updated to what we have in Dataset B:
Output:
Key: Misc:
AMC NETTING
AMC NETTING
AMC NETTING
ABD XXXX
I am doing the merge as below:
Code: | Data Dataset_Perm;
Merge DatasetA (IN=A)
DatasetB (IN=AP) ;
By Key;
IF B AND NOT A THEN DELETE;
Run; |
But I am getting the output dataset as:
Key: Misc:
AMC NETTING
AMC WIRE
AMC WIRE
ABD XXXX
Only the first matched observation is being updated with the new MISC value and the rest are not being updated.
Please advice me how to go about it.
Thanks,
BK |
|
Back to top |
|
|
expat Intermediate
Joined: 01 Mar 2007 Posts: 475 Topics: 9 Location: Welsh Wales
|
Posted: Thu Feb 18, 2010 9:37 am Post subject: |
|
|
I'm not sure that you can do what you want with the two variable names being the same.
I would change the variable name on one file and then use the code below
The code assumes that the SAS dataset already exists, if not then just create it with the different variable name.
Code: | PROC SORT DATA=DATASETA NODUPS;
BY KEY;
RUN;
DATA DATASETB;
SET DATASETB;
RENAME MISC = MISC2;
RUN;
PROC SORT DATA=DATASETB NODUPS;
BY KEY;
RUN;
DATA DATASET_AB;
MERGE DATASETA (IN=IN1)
DATASETB (IN=IN2);
BY KEY;
IF IN1 AND IN2 THEN MISC = MISC2;
DROP MISC2;
RUN; |
Input for DATASETA
Code: | 123 ABD
123 DEF
456 HDH
331 PPO
123 ASS |
Input for DATASETB
Output shows
Code: | KEY MISC
123 XXX
123 XXX
123 XXX
331 PPO
456 HDH |
_________________ If it's true that we are here to help others,
then what exactly are the others here for ? |
|
Back to top |
|
|
|
|