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

Hi ,

 

I have two datasets in oracle test1 and test2 and test1 has 3 columns pid, mid and sid which we need to concatenate to form a join key premid1 so that we can join to premid in test2 table in oracle 

Test1 

 

StatePIDMIDSIDAmountMDTRCRD_INDSCOUNTMFLAG
VA100988700520650000100202111Y1Y
VA100345156341040200202111N1N

 

I need to form premid1 using lpad and concatenation of PID,MID and SID to form join key (premid1) and also have filters like in code below to select records from test1 

 

Test2 

StatePrem_IDSPL
VA000100988-0700-00052065000023540
VA000100345-0001-00005634104042522
VA000200346-0009-00003698470034521

 

Now it has Prem_ID 

 

I have used below code 

 

PROC SQL ;
CONNECT to ORACLE as ORA(authdomain=xxxx path= xxxx connection=global);
Create Table pt_level as
Select * From CONNECTION TO ORA
(
WITH TMP_HDR AS
(
select
Case when SS.State = 'VA'
then LPAD (ss.PID, 9, 0) || '-' || LPAD (ss.MID, 4, 0) || '-' || LPAD (ss.SID,12,0)
else 'ERROR'
End as prem_id1,
SS.state,
ss.amount

From
M_v.TEST1 SS
Where State = 'VA'
AND MDT= 202112
and RCRD_IND = 'Y'
and SCOUNT = 1
and MFLAG = 'Y'
),

TMP_DTL AS
(
SELECT DISTINCT
SP.STATE,
SP.PID,
SP.SID
PC.SPL,
sp.amount

From TMP_HDR SP
left JOIN TEST2 PC on SP.STATE = PC.STATE
and SP.PREMID1 = PC.PREM_ID

GROUP BY STATE,PID,SID
),
SELECT H.*
FROM TMP_DTL H

);
DISCONNECT FROM ORACLE ;
QUIT;

 

Is there any other way i can try to join both tables ?

 

Can anyone please help . Thank you 

1 ACCEPTED SOLUTION

Accepted Solutions
s_lassen
Meteorite | Level 14

I do not think you need to make it that complicated. Something like this should work:

PROC SQL ;
CONNECT to ORACLE as ORA(authdomain=xxxx path= xxxx connection=global);
Create Table pt_level as
Select * From CONNECTION TO ORA
(select
    test1.STATE,
    test1.PID,
    test1.SID
    test2.SPL,
    test1.amount
 from  
    test1 left join test2 on
	  test1.state=test2.state and 
	  test2.PREM_ID=LPAD (test1.PID, 9, 0) || '-' || LPAD (test1.MID, 4, 0) || '-' || LPAD (test1.SID,12,0)
  where 
	test1.state='VA' and 
	test1.MDT= 202112 and
    test1.RCRD_IND = 'Y' and
    test1.SCOUNT = 1 and
    test1.MFLAG = 'Y'
  );	

DISCONNECT FROM ORACLE ;
QUIT;

View solution in original post

5 REPLIES 5
LinusH
Tourmaline | Level 20

First, why are you not happy with your current solution?

Second, this is pure Oracle PL SQL. If intend to stick with that, you should probably turn to an Oracle forum instead.

Data never sleeps
s_lassen
Meteorite | Level 14

I do not think you need to make it that complicated. Something like this should work:

PROC SQL ;
CONNECT to ORACLE as ORA(authdomain=xxxx path= xxxx connection=global);
Create Table pt_level as
Select * From CONNECTION TO ORA
(select
    test1.STATE,
    test1.PID,
    test1.SID
    test2.SPL,
    test1.amount
 from  
    test1 left join test2 on
	  test1.state=test2.state and 
	  test2.PREM_ID=LPAD (test1.PID, 9, 0) || '-' || LPAD (test1.MID, 4, 0) || '-' || LPAD (test1.SID,12,0)
  where 
	test1.state='VA' and 
	test1.MDT= 202112 and
    test1.RCRD_IND = 'Y' and
    test1.SCOUNT = 1 and
    test1.MFLAG = 'Y'
  );	

DISCONNECT FROM ORACLE ;
QUIT;
jhh197
Pyrite | Level 9
Hi ,
I am checking this one .

Thank you so much for helping
jhh197
Pyrite | Level 9
Hi ,
I am checking this one

Thank you so much for helping
jhh197
Pyrite | Level 9
Thank you so much for all your help

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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