Hi ,
I'm facing a problem while joining the two tables in the below fashion. not able to get the proper out put.
proc sql;
create table Windscreen_RM as
select t1.repyr,t1.repmthdt,sum(case when t1.flg_NilSettledInc="N" then t1.cnt_Settle end ) as Settled_with_pay label="Settled count with Pay",
sum(case when t1.flg_NilSettledInc="Y" then t1.cnt_Settle end ) as Settled_wO_pay label="Settled count without Pay",
sum(case when t1.rpnmth=. then t1.cnt_Outstand end) as outstand label="Outstanding Count",t2.Notified_cnt
from WORK.RM_MOD_Windscreen t1 left outer join (select mdy(month(not_dat),01,year(not_dat)) as notif_mnth format=ddmmyy10.,count(distinct cats(clm_key,not_dat)) as Notified_cnt label="Reported Claims"
from WORK.RM_MOD_Windscreen
where notyr=repyr
group by calculated notif_mnth) as t2
on t1.notyr=t1.repyr and t1.repmthdt=t2.notif_mnth
group by t1.repyr,t1.repmthdt
;
quit;
The individual outputs are below.
Individual out put of outer query | Individual output of inner query | ||||||
Reporting year | Reporting month | settled count with pay | Nil settled | out | Notif_mnth | Notified_cnt | |
2013 | 01/01/2013 | . | . | 2 | 01/01/2013 | 2 | |
2013 | 01/02/2013 | 3 | . | 0 | 01/02/2013 | 1 | |
2013 | 01/03/2013 | . | . | 6 | 01/03/2013 | 7 | |
2013 | 01/04/2013 | 2 | . | 4 | 01/06/2013 | 3 | |
2013 | 01/05/2013 | 1 | . | 4 | 01/07/2013 | 1 | |
2013 | 01/06/2013 | 1 | . | 5 | 01/12/2013 | 1 | |
2013 | 01/07/2013 | . | . | 6 | 01/05/2014 | 2 | |
2013 | 01/08/2013 | 3 | 1 | 3 | 01/08/2014 | 1 | |
2013 | 01/09/2013 | . | 1 | 2 | 01/02/2015 | 2 | |
2013 | 01/10/2013 | . | . | 2 | 01/04/2015 | 3 | |
2013 | 01/11/2013 | . | . | 2 | 01/06/2015 | 1 | |
2013 | 01/12/2013 | . | . | 3 | 01/09/2015 | 2 | |
2014 | 01/05/2014 | . | . | 1 | 01/11/2015 | 1 | |
2014 | 01/06/2014 | . | . | 1 | 01/02/2016 | 1 | |
2014 | 01/07/2014 | . | . | 1 | 01/03/2016 | 2 | |
2014 | 01/08/2014 | . | . | 1 | 01/04/2016 | 3 | |
2014 | 01/09/2014 | . | . | 1 | 01/07/2016 | 1 | |
2014 | 01/10/2014 | . | . | 1 | 01/11/2016 | 19 | |
2014 | 01/11/2014 | 1 | . | 1 | 01/12/2016 | 22 | |
2014 | 01/12/2014 | . | . | 1 | 01/01/2017 | 26 | |
2015 | 01/02/2015 | . | . | 1 | 01/02/2017 | 23 | |
2015 | 01/03/2015 | . | 1 | 0 | |||
2015 | 01/04/2015 | 1 | . | 0 | |||
2015 | 01/06/2015 | 1 | . | . | |||
2015 | 01/07/2015 | . | . | 1 | |||
2015 | 01/09/2015 | . | . | 1 | |||
2015 | 01/10/2015 | 4 | . | 0 | |||
2015 | 01/11/2015 | . | . | 1 | |||
2015 | 01/12/2015 | . | . | 1 | |||
2016 | 01/02/2016 | 1 | . | 0 | |||
2016 | 01/03/2016 | . | . | 1 | |||
2016 | 01/04/2016 | 2 | 1 | 1 | |||
2016 | 01/05/2016 | . | . | 1 | |||
2016 | 01/06/2016 | . | . | 1 | |||
2016 | 01/07/2016 | 1 | . | 1 | |||
2016 | 01/08/2016 | . | . | 1 | |||
2016 | 01/09/2016 | . | . | 1 | |||
2016 | 01/10/2016 | 1 | . | 1 | |||
2016 | 01/11/2016 | 6 | . | 13 | |||
2016 | 01/12/2016 | 7 | . | 31 | |||
2017 | 01/01/2017 | 6 | . | 19 | |||
2017 | 01/02/2017 | 19 | 1 | 30 |
Can someone tweak the code if i went somewhere wrong?
Outputs are not aligned properly.
the last two columns are output of inner query.
Use the macro from https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... to create a datastep of a usable subset of your input data and post it here, and also post the expected result for that subset, so we have something to play around with.
And do some visual formatting of your code, like
proc sql;
create table Windscreen_RM as
select
t1.repyr,
t1.repmthdt,
sum(case when t1.flg_NilSettledInc="N" then t1.cnt_Settle end ) as Settled_with_pay label="Settled count with Pay",
sum(case when t1.flg_NilSettledInc="Y" then t1.cnt_Settle end ) as Settled_wO_pay label="Settled count without Pay",
sum(case when t1.rpnmth=. then t1.cnt_Outstand end) as outstand label="Outstanding Count",
t2.Notified_cnt
from WORK.RM_MOD_Windscreen t1
left outer join (
select
mdy(month(not_dat),01,year(not_dat)) as notif_mnth format=ddmmyy10.,
count(distinct cats(clm_key,not_dat)) as Notified_cnt label="Reported Claims"
from WORK.RM_MOD_Windscreen
where notyr=repyr
group by calculated notif_mnth
) as t2
on t1.notyr=t1.repyr and t1.repmthdt=t2.notif_mnth
group by t1.repyr,t1.repmthdt
;
quit;
Shouldn't
t1.notyr=t1.repyr
be used in a where condition? You could even move that into a dataset option:
from WORK.RM_MOD_Windscreen (where=(notyr=repyr)) t1
Hi @Kurt_Bremser,
Thanks for the reply,
The issue i was facing will be expalined if you can help me in undersating the difference between the queries attached.
Also It would be helpfull if you can explain how does the follwing conditions execute while joining the two tables .
1. from table1 t1 right join table2 t2 on t1.repmthdt le t2.notif_mnth
2.from table1 t1 left join table2 t2 on t1.repmthdt le t2.notif_mnth
3.from table1 t1 left join table2 t2 on t2.notif_mnth le t1.repmthdt
4.from table1 t1 right join table2 t2 on t2.notif_mnth le t1.repmthdt
5.from table1 t1 right join table2 t2 on t2.notyr=t1.repyr and t1.repmthdt le t2.notif_mnth
Sorry i'm poor in understanding the non equal joins.
Sample data is below.
Table1:
Reporting year | Reporting month | settled count with pay | Nil settled |
2013 | 01/01/2013 | . | . |
2013 | 01/02/2013 | 3 | . |
2013 | 01/03/2013 | . | . |
2013 | 01/04/2013 | 2 | . |
2013 | 01/05/2013 | 1 | . |
2013 | 01/06/2013 | 1 | . |
2013 | 01/07/2013 | . | . |
2013 | 01/08/2013 | 3 | 1 |
2013 | 01/09/2013 | . | 1 |
2013 | 01/10/2013 | . | . |
2013 | 01/11/2013 | . | . |
2013 | 01/12/2013 | . | . |
Table 2:
Notif_mnth | Notified_cnt |
01/01/2013 | 2 |
01/02/2013 | 1 |
01/03/2013 | 7 |
01/06/2013 | 3 |
Thanks in advance...!
For the types of joins, do a google search on "sas sql type of joins", and you'll find a myriad of documents dealing with this.
I also recommend to set up some test data and test the different joins for yourself, and then interpret the results in light of the knowledge you gained from those documents.
And, as I already stated, provide example data in a data step, as I'm not typing from the screen.
Dear sir.
There was no much information about the non-equi joins in the google , please send me link if you find some.
data is below,
data table1;
informat repmthdt ddmmyy10.;
format repmthdt ddmmyy10.;
input repyr repmthdt Settled_with_pay Settled_wO_pay ;
cards;
2013 01/01/2013 . .
2013 01/02/2013 3 .
2013 01/03/2013 . .
2013 01/04/2013 2 .
2013 01/05/2013 1 .
2013 01/06/2013 1 .
2013 01/07/2013 . .
2013 01/08/2013 3 1
2013 01/09/2013 . 1
2013 01/10/2013 . .
2013 01/11/2013 . .
2013 01/12/2013 . .
;
run;
data table2;
informat Notif_mnth ddmmyy10.;
format Notif_mnth ddmmyy10.;
input Notif_mnth Notified_cnt ;
cards;
01/01/2013 2
01/02/2013 1
01/03/2013 7
01/06/2013 3
;
run;
A non-equi join works like an equi join, it's just that the condition uses a different comparison operator. non-equi joins might result in larger outputs than equi joins, but that is to be expected, as more observations might meet the condition.
A left join takes all observations from the first dataset, and will join matching observations from the second dataset. Observations from the first dataset that have no match will be included, observations from the second dataset that have no match will be dropped.
A right join does the same thing the other way round.
So, in light of this, run this code:
data table1;
informat repmthdt ddmmyy10.;
format repmthdt ddmmyy10.;
input repyr repmthdt Settled_with_pay Settled_wO_pay ;
cards;
2013 01/01/2013 . .
2013 01/02/2013 3 .
2013 01/03/2013 . .
2013 01/04/2013 2 .
2013 01/05/2013 1 .
2013 01/06/2013 1 .
2013 01/07/2013 . .
2013 01/08/2013 3 1
2013 01/09/2013 . 1
2013 01/10/2013 . .
2013 01/11/2013 . .
2013 01/12/2013 . .
;
run;
data table2;
informat Notif_mnth ddmmyy10.;
format Notif_mnth ddmmyy10.;
input Notif_mnth Notified_cnt ;
cards;
01/01/2013 2
01/02/2013 1
01/03/2013 7
01/06/2013 3
;
run;
proc sql;
create table test1 as select *
from table1 t1 right join table2 t2 on t1.repmthdt le t2.notif_mnth;
create table test2 as select *
from table1 t1 left join table2 t2 on t1.repmthdt le t2.notif_mnth;
create table test3 as select *
from table1 t1 left join table2 t2 on t2.notif_mnth le t1.repmthdt;
create table test4 as select *
from table1 t1 right join table2 t2 on t2.notif_mnth le t1.repmthdt;
create table test5 as select *
from table1 t1 right join table2 t2 on year(t2.notif_mnth)=t1.repyr and t1.repmthdt le t2.notif_mnth;
quit;
proc print data=test1 noobs;
run;
proc print data=test2 noobs;
run;
proc print data=test3 noobs;
run;
proc print data=test4 noobs;
run;
proc print data=test5 noobs;
run;
And then start to interpret the results:
Settled_ Settled_ Notified_ repmthdt repyr with_pay wO_pay Notif_mnth cnt 01/01/2013 2013 . . 01/01/2013 2 01/01/2013 2013 . . 01/02/2013 1 01/01/2013 2013 . . 01/03/2013 7 01/01/2013 2013 . . 01/06/2013 3 01/02/2013 2013 3 . 01/02/2013 1 01/02/2013 2013 3 . 01/03/2013 7 01/02/2013 2013 3 . 01/06/2013 3 01/03/2013 2013 . . 01/03/2013 7 01/03/2013 2013 . . 01/06/2013 3 01/04/2013 2013 2 . 01/06/2013 3 01/05/2013 2013 1 . 01/06/2013 3 01/06/2013 2013 1 . 01/06/2013 3 Settled_ Settled_ Notified_ repmthdt repyr with_pay wO_pay Notif_mnth cnt 01/01/2013 2013 . . 01/01/2013 2 01/01/2013 2013 . . 01/02/2013 1 01/02/2013 2013 3 . 01/02/2013 1 01/01/2013 2013 . . 01/03/2013 7 01/02/2013 2013 3 . 01/03/2013 7 01/03/2013 2013 . . 01/03/2013 7 01/01/2013 2013 . . 01/06/2013 3 01/02/2013 2013 3 . 01/06/2013 3 01/03/2013 2013 . . 01/06/2013 3 01/04/2013 2013 2 . 01/06/2013 3 01/05/2013 2013 1 . 01/06/2013 3 01/06/2013 2013 1 . 01/06/2013 3 01/07/2013 2013 . . . . 01/08/2013 2013 3 1 . . 01/09/2013 2013 . 1 . . 01/10/2013 2013 . . . . 01/11/2013 2013 . . . . 01/12/2013 2013 . . . . Settled_ Settled_ Notified_ repmthdt repyr with_pay wO_pay Notif_mnth cnt 01/01/2013 2013 . . 01/01/2013 2 01/02/2013 2013 3 . 01/01/2013 2 01/03/2013 2013 . . 01/01/2013 2 01/04/2013 2013 2 . 01/01/2013 2 01/05/2013 2013 1 . 01/01/2013 2 01/06/2013 2013 1 . 01/01/2013 2 01/07/2013 2013 . . 01/01/2013 2 01/08/2013 2013 3 1 01/01/2013 2 01/09/2013 2013 . 1 01/01/2013 2 01/10/2013 2013 . . 01/01/2013 2 01/11/2013 2013 . . 01/01/2013 2 01/12/2013 2013 . . 01/01/2013 2 01/02/2013 2013 3 . 01/02/2013 1 01/03/2013 2013 . . 01/02/2013 1 01/04/2013 2013 2 . 01/02/2013 1 01/05/2013 2013 1 . 01/02/2013 1 01/06/2013 2013 1 . 01/02/2013 1 01/07/2013 2013 . . 01/02/2013 1 01/08/2013 2013 3 1 01/02/2013 1 01/09/2013 2013 . 1 01/02/2013 1 01/10/2013 2013 . . 01/02/2013 1 01/11/2013 2013 . . 01/02/2013 1 01/12/2013 2013 . . 01/02/2013 1 01/03/2013 2013 . . 01/03/2013 7 01/04/2013 2013 2 . 01/03/2013 7 01/05/2013 2013 1 . 01/03/2013 7 01/06/2013 2013 1 . 01/03/2013 7 01/07/2013 2013 . . 01/03/2013 7 01/08/2013 2013 3 1 01/03/2013 7 01/09/2013 2013 . 1 01/03/2013 7 01/10/2013 2013 . . 01/03/2013 7 01/11/2013 2013 . . 01/03/2013 7 01/12/2013 2013 . . 01/03/2013 7 01/06/2013 2013 1 . 01/06/2013 3 01/07/2013 2013 . . 01/06/2013 3 01/08/2013 2013 3 1 01/06/2013 3 01/09/2013 2013 . 1 01/06/2013 3 01/10/2013 2013 . . 01/06/2013 3 01/11/2013 2013 . . 01/06/2013 3 01/12/2013 2013 . . 01/06/2013 3 Settled_ Settled_ Notified_ repmthdt repyr with_pay wO_pay Notif_mnth cnt 01/01/2013 2013 . . 01/01/2013 2 01/02/2013 2013 3 . 01/01/2013 2 01/02/2013 2013 3 . 01/02/2013 1 01/03/2013 2013 . . 01/01/2013 2 01/03/2013 2013 . . 01/02/2013 1 01/03/2013 2013 . . 01/03/2013 7 01/04/2013 2013 2 . 01/01/2013 2 01/04/2013 2013 2 . 01/02/2013 1 01/04/2013 2013 2 . 01/03/2013 7 01/05/2013 2013 1 . 01/01/2013 2 01/05/2013 2013 1 . 01/02/2013 1 01/05/2013 2013 1 . 01/03/2013 7 01/06/2013 2013 1 . 01/01/2013 2 01/06/2013 2013 1 . 01/02/2013 1 01/06/2013 2013 1 . 01/03/2013 7 01/06/2013 2013 1 . 01/06/2013 3 01/07/2013 2013 . . 01/01/2013 2 01/07/2013 2013 . . 01/02/2013 1 01/07/2013 2013 . . 01/03/2013 7 01/07/2013 2013 . . 01/06/2013 3 01/08/2013 2013 3 1 01/01/2013 2 01/08/2013 2013 3 1 01/02/2013 1 01/08/2013 2013 3 1 01/03/2013 7 01/08/2013 2013 3 1 01/06/2013 3 01/09/2013 2013 . 1 01/01/2013 2 01/09/2013 2013 . 1 01/02/2013 1 01/09/2013 2013 . 1 01/03/2013 7 01/09/2013 2013 . 1 01/06/2013 3 01/10/2013 2013 . . 01/01/2013 2 01/10/2013 2013 . . 01/02/2013 1 01/10/2013 2013 . . 01/03/2013 7 01/10/2013 2013 . . 01/06/2013 3 01/11/2013 2013 . . 01/01/2013 2 01/11/2013 2013 . . 01/02/2013 1 01/11/2013 2013 . . 01/03/2013 7 01/11/2013 2013 . . 01/06/2013 3 01/12/2013 2013 . . 01/01/2013 2 01/12/2013 2013 . . 01/02/2013 1 01/12/2013 2013 . . 01/03/2013 7 01/12/2013 2013 . . 01/06/2013 3 Settled_ Settled_ Notified_ repmthdt repyr with_pay wO_pay Notif_mnth cnt 01/06/2013 2013 1 . 01/06/2013 3 01/05/2013 2013 1 . 01/06/2013 3 01/03/2013 2013 . . 01/03/2013 7 01/03/2013 2013 . . 01/06/2013 3 01/01/2013 2013 . . 01/03/2013 7 01/01/2013 2013 . . 01/01/2013 2 01/01/2013 2013 . . 01/02/2013 1 01/01/2013 2013 . . 01/06/2013 3 01/02/2013 2013 3 . 01/03/2013 7 01/02/2013 2013 3 . 01/02/2013 1 01/02/2013 2013 3 . 01/06/2013 3 01/04/2013 2013 2 . 01/06/2013 3
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.