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

Hello,

 

I have two files and I want to add the missing value from file 2 to file 1.

 

For example, File 1 has ALL IDs but incompleted variable 1 (character variable). File 2 has completed Variable1 and their corresponding IDs.

 

When ID = 1 or 3, variable1 is incomplete as highlighted in File 2. ID = 2 does not show in File 2, which means when ID = 2, Variable1 is completed. I would like to merge these two files as File 3 shows.

 

File 1                     

 

ID          Variable1

1                A10

1                B20

2                G11

2                G12

2                R13

3                E25

3                E38

3                F69

 

File 2

ID          Variable1

1                A10

1                B20

1                C30

3                E25

3                E38

3                F69

3                D87

 

File 3

ID          Variable1

1                A10

1                B20

1                C30

2                G11

2                G12

2                R13

3                E25

3                E38

3                F69

3                D87

 

what code do I need to use? Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

By merge step

 

data have1;
input ID  Variable1$;
cards;
1  A10
1   B20
2   G11
2  G12
2  R13
3   E25
3   E38
3   F69
;
 

data have2;
input ID  Variable1$;
cards;
1  A10
1  B20
1  C30
3  E25
3    E38
3  F69
3   D87
;

data want;
merge have1 have2;
by id;
run;
Thanks,
Jag

View solution in original post

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20
data one;
input ID $ Variable1 $;
datalines;
1 A10
1 B20
2 G11
2 G12
2 R13
3 E25
3 E38
3 F69
;

data two;
input ID $ Variable1 $;
datalines;
1 A10
1 B20
1 C30
3 E25
3 E38
3 F69
3 D87
;

proc sort data=one; by ID Variable1; run;
proc sort data=two; by ID Variable1; run;

data want;
    update one two;
    by ID Variable1;
run;
Jagadishkatam
Amethyst | Level 16

By merge step

 

data have1;
input ID  Variable1$;
cards;
1  A10
1   B20
2   G11
2  G12
2  R13
3   E25
3   E38
3   F69
;
 

data have2;
input ID  Variable1$;
cards;
1  A10
1  B20
1  C30
3  E25
3    E38
3  F69
3   D87
;

data want;
merge have1 have2;
by id;
run;
Thanks,
Jag
PeterClemmensen
Tourmaline | Level 20

If you do not want the PROC SORT steps, you can do

 

data _null_;
    if _N_=1 then do;
        declare hash h(dataset:'one', ordered:'Y');
        h.definekey('ID', 'Variable1');
        h.definedone();
    end;

    set two end=lr;
    h.ref();

    if lr then h.output(dataset:'want');
run;
Ksharp
Super User
data one;
input ID $ Variable1 $;
datalines;
1 A10
1 B20
2 G11
2 G12
2 R13
3 E25
3 E38
3 F69
;

data two;
input ID $ Variable1 $;
datalines;
1 A10
1 B20
1 C30
3 E25
3 E38
3 F69
3 D87
;
proc sql;
create table want as
select * from one
union
select * from two;
quit;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 699 views
  • 0 likes
  • 4 in conversation