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

Hi I need some help !

basically i need table 1 and table 2 become table 3

 

table 1

key          date             desc1

012          1/1/2019     check

013          1/1/2019     nothing

013          9/1/2019     left

 

table 2

key         date              desc2

012         1/1/2019      up

012         9/1/2019      down

013         1/1/2019      right

 

table 3

key         date              desc1          desc2

012        1/1/2019       check           up

012        9/1/2019       check           down

013        1/1/2019       nothing         right

013        9/1/2019       left                right

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

For the data you posted, a simple MERGE does it:

data table1;
input key $ date :ddmmyy10. desc1 $;
format date yymmdd10.;
datalines;
012          1/1/2019     check
013          1/1/2019     nothing
013          9/1/2019     left
;
 
data table2;
input key $ date :ddmmyy10. desc2 $;
format date yymmdd10.;
datalines;
012         1/1/2019      up
012         9/1/2019      down
013         1/1/2019      right
;

data table3;
merge
  table1
  table2
;
by key;
run;

but this is only true until you run into a situation where there is more than one observation for a given key in both datasets; in this case, you will need a SQL full join, and some logic to deal with the dates, like

proc sql;
create table table3 as
  select
    coalesce(t1.key,t2.key) as key,
    max(t1.date,t2.date) as date format=yymmdd10.,
    t1.desc1,
    t2.desc2
  from
    table1 t1
  full join
    table2 t2
  on t1.key = t2.key
  order by calculated key, calculated date
;
quit;

View solution in original post

2 REPLIES 2
Cynthia_sas
SAS Super FREQ
Hi:
Take a look at the sample code here: https://communities.sas.com/t5/SAS-Programming/Merging-Tables/td-p/580772 on the 8/17/2019 posting. It shows 5 of the possible combinations from 2 datasets being merged. Your example seems very similar.
Cynthia
Kurt_Bremser
Super User

For the data you posted, a simple MERGE does it:

data table1;
input key $ date :ddmmyy10. desc1 $;
format date yymmdd10.;
datalines;
012          1/1/2019     check
013          1/1/2019     nothing
013          9/1/2019     left
;
 
data table2;
input key $ date :ddmmyy10. desc2 $;
format date yymmdd10.;
datalines;
012         1/1/2019      up
012         9/1/2019      down
013         1/1/2019      right
;

data table3;
merge
  table1
  table2
;
by key;
run;

but this is only true until you run into a situation where there is more than one observation for a given key in both datasets; in this case, you will need a SQL full join, and some logic to deal with the dates, like

proc sql;
create table table3 as
  select
    coalesce(t1.key,t2.key) as key,
    max(t1.date,t2.date) as date format=yymmdd10.,
    t1.desc1,
    t2.desc2
  from
    table1 t1
  full join
    table2 t2
  on t1.key = t2.key
  order by calculated key, calculated date
;
quit;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 2 replies
  • 313 views
  • 0 likes
  • 3 in conversation