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

Hi I have two dasets and I need to combine them both even though dates for the both tables do not match

A:

Site DTM measure1 measure2
ABC 2/2/2020 9:53 32.1 20.5
ABC 2/2/2020 10:31 12.1 12.6
DEF 2/3/2020 9:33 42.1 44.1
DEF 2/4/2020 14:23 31.1 41.4

 

B:

Site DTM measure1
ABC 2/2/2020 9:53 32.1
ABC 2/2/2020 10:31 12.1
ABC 2/3/2020 9:33 42.1
DEF 2/2/2020 9:53 41.1
DEF 2/2/2020 10:31 12.2
DEF 2/3/2020 9:33 42.1
DEF 2/4/2020 14:23 31.1

 

Want:

Site DTM measure1 measure2
ABC 2/2/2020 9:53 32.1 20.5
ABC 2/2/2020 10:31 12.1 12.6
ABC 2/3/2020 9:33 42.1  
DEF 2/2/2020 9:53 41.1  
DEF 2/2/2020 10:31 12.2  
DEF 2/3/2020 9:33 42.1 44.1
DEF 2/4/2020 14:23 31.1 41.4

 

Here's my attempt thus far:

select b.site, b.measure1, b.measure2, b.dtm, a.measure1

from dataset_a a
left outer join dataset_b on a.site = b.site 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

I think this is what you want

 

data a;
input Site $ DTM $ 5-18 measure1 measure2;
datalines;
ABC 2/2/2020 9:53  32.1 20.5
ABC 2/2/2020 10:31 12.1 12.6
DEF 2/3/2020 9:33  42.1 44.1
DEF 2/4/2020 14:23 31.1 41.4
;

data b;
input Site $ DTM $ 5-18 measure1;
datalines;
ABC 2/2/2020 9:53 32.1
ABC 2/2/2020 10:31 12.1
ABC 2/3/2020 9:33 42.1
DEF 2/2/2020 9:53 41.1
DEF 2/2/2020 10:31 12.2
DEF 2/3/2020 9:33 42.1
DEF 2/4/2020 14:23 31.1
;

proc sql;
   create table want as
   select b.*, a.measure2
   from b left join a
   on a.Site=b.Site and a.DTM=b.DTM;
quit;

 

Result:

 

Site  DTM             measure1  measure2 
ABC   2/2/2020 10:31  12.1      12.6 
ABC   2/2/2020 9:53   32.1      20.5 
ABC   2/3/2020 9:33   42.1      . 
DEF   2/2/2020 10:31  12.2      . 
DEF   2/2/2020 9:53   41.1      . 
DEF   2/3/2020 9:33   42.1      44.1 
DEF   2/4/2020 14:23  31.1      41.4 

 

View solution in original post

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

"even though dates for the both tables do not match". What do you mean by that? The dates do match?

monday89
Fluorite | Level 6
Hi. Yes. They dates dont have to match but I would to show the values of
measure 1 and keep measure 2 as blank where dates from both datasets dont
match
PeterClemmensen
Tourmaline | Level 20

I think this is what you want

 

data a;
input Site $ DTM $ 5-18 measure1 measure2;
datalines;
ABC 2/2/2020 9:53  32.1 20.5
ABC 2/2/2020 10:31 12.1 12.6
DEF 2/3/2020 9:33  42.1 44.1
DEF 2/4/2020 14:23 31.1 41.4
;

data b;
input Site $ DTM $ 5-18 measure1;
datalines;
ABC 2/2/2020 9:53 32.1
ABC 2/2/2020 10:31 12.1
ABC 2/3/2020 9:33 42.1
DEF 2/2/2020 9:53 41.1
DEF 2/2/2020 10:31 12.2
DEF 2/3/2020 9:33 42.1
DEF 2/4/2020 14:23 31.1
;

proc sql;
   create table want as
   select b.*, a.measure2
   from b left join a
   on a.Site=b.Site and a.DTM=b.DTM;
quit;

 

Result:

 

Site  DTM             measure1  measure2 
ABC   2/2/2020 10:31  12.1      12.6 
ABC   2/2/2020 9:53   32.1      20.5 
ABC   2/3/2020 9:33   42.1      . 
DEF   2/2/2020 10:31  12.2      . 
DEF   2/2/2020 9:53   41.1      . 
DEF   2/3/2020 9:33   42.1      44.1 
DEF   2/4/2020 14:23  31.1      41.4 

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

LIBNAME 101

Follow along as SAS technical trainer Dominique Weatherspoon expertly answers all your questions about SAS Libraries.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 1519 views
  • 0 likes
  • 2 in conversation