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

I have the following two tables and I am trying to merge them by Account and Date. It is however not giving me the output that I need.

Table 1 contains Account column, Dale_old and Amount_old are populated for first row and the others rows are missing.

AccountPaidDate_oldAmount_old
11Jan-17500
10..
10..
10..
10..
10..

 

Table 2 contains all fields populated.

AccountPaidDate_newAmount_new
10Oct-16200
10Nov-16300
10Dec-16600
10Jan-17700
10Feb-17500
10Mar-17200

 

Below is the table that I want to produce after a SAS merge or Join in Sql.

AccountDate_oldDate_newAmount_oldAmount_new
1Jan-17Jan-17500700

 

I have tried using the following code but it doesn't seem to work.

proc sql;

create table test as

select A.*, B.* from table1 as A

left join table2 as B on

A.Account=B.Account

and

A.Date_old=B.Date_new;

quit;

run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
kiranv_
Rhodochrosite | Level 12

i did something like below and it worked

 

data have1;
infile datalines dlm ='09'x;
input Account	Paid	Date_old:anydtdte.	Amount_old;
format date_old date9.;
datalines;
1	1	Jan17	500
1	0	.	    .
1	0	.	    .
1	0	.	    .
1	0	.	    .
1	0	.	    .
;

data have2;
infile datalines dlm ='09'x;
input Account	Paid	Date_new:anydtdte.	Amount_new;
format date_new date9.;
datalines;
1	0	Oct16	200
1	0	Nov16	300
1	0	Dec16	600
1	0	Jan17	700
1	0	Feb17	500
1	0	Mar17	200
;

proc sql;

create table test as

select A.account, 
      date_old, 
          date_new,
      amount_old, 
      amount_new 
from have1 as A

inner join have2 as B on

A.Account=B.Account

and A.Date_old=B.Date_new;

quit;

View solution in original post

4 REPLIES 4
kiranv_
Rhodochrosite | Level 12

you need to a do your inner join. Both date_new and data_old have same values and keeping them in final output does not make it usefu.

proc sql;

create table test as

select A.account, 
      date_old, 
      date_new,
      amount_old, 
      amount_new 
from have1 as A

inner join have2 as B 
on A.Account=B.Account and A.Date_old=B.Date_new; quit;
sasuser0912
Fluorite | Level 6

Thanks for your reply. I tried this on my dataset but it is giving me nothing, no output.

kiranv_
Rhodochrosite | Level 12

i did something like below and it worked

 

data have1;
infile datalines dlm ='09'x;
input Account	Paid	Date_old:anydtdte.	Amount_old;
format date_old date9.;
datalines;
1	1	Jan17	500
1	0	.	    .
1	0	.	    .
1	0	.	    .
1	0	.	    .
1	0	.	    .
;

data have2;
infile datalines dlm ='09'x;
input Account	Paid	Date_new:anydtdte.	Amount_new;
format date_new date9.;
datalines;
1	0	Oct16	200
1	0	Nov16	300
1	0	Dec16	600
1	0	Jan17	700
1	0	Feb17	500
1	0	Mar17	200
;

proc sql;

create table test as

select A.account, 
      date_old, 
          date_new,
      amount_old, 
      amount_new 
from have1 as A

inner join have2 as B on

A.Account=B.Account

and A.Date_old=B.Date_new;

quit;
sasuser0912
Fluorite | Level 6

Thanks kiranv. Yes this solution works.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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