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 a table a and Table b and i how to get table c . Can anyone pls help

 

Table a

 

item id     score
dis     1       351
ins     2       2.12 
ins     3       2.15 
ins     4       0.05 
gbs     5       2.0

gbs     6       6.0

 

 

and table b

 

ins   gbs
3       7
2       5
5       6

 

 

and how to get table c

 

disinsgbs
3512.156
3512.122
3510.056

 

 

and how do i change below query so that i get table c without missing values

 

proc sql;
create table xyz as
 select (select sale_amt from a where item='dis') as dis,
       a.ins as x_id, b.sale_amt as ins,
       a.gbs as y_id,c.sale_amt as gbs

from b as a

left join
(select * from a where item='ins') as b
on a.ins=b.id
left join
( select * from a where item='gbs') as c
on a.gbs=c.id
;
quit;

 

and  when ins and gbs values in b table are not in id coloumn for table a we take the value from previous one like we dont have gbs 7 in table a so we take the value for for gbs 6 which is 6.0

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data a;
infile cards expandtabs;
input item $ id sale_amt ;
cards;
dis     1       351
ins     2       2.12 
ins     3       2.15 
ins     4       0.05 
gbs     5       2.0
gbs     6       6.0
;
run;
data b;
infile cards expandtabs;
input ins   gbs;
cards;
3       7
2       5
5       6
;
run;



proc sql;
create table xyz as
 select (select sale_amt from a where item='dis') as dis,
       a.ins as x_id, b.sale_amt as ins,
       a.gbs as y_id,c.sale_amt as gbs
from b as a
left join
(select * from a where item='ins') as b
on a.ins=b.id
left join
( select * from a where item='gbs') as c
on a.gbs=c.id
;

create table want as
 select (select sale_amt from a where item='dis') as dis,
       x_id, 
       coalesce(ins,(select sale_amt from a having abs(x_id-id)=min(abs(x_id-id)))) as ins,
       y_id,
       coalesce(gbs,(select sale_amt from a having abs(y_id-id)=min(abs(y_id-id)))) as gbs
from xyz;
quit;
 

View solution in original post

4 REPLIES 4
Astounding
PROC Star

At a minimum, you will have to post more data.  Your SELECT statement refers to SALE_AMT, but there is no such field anywhere.

 

At the same time, the SQL code tries to extract from table c.  But the problem states that table c is the output.

 

it might be easier to figure this out if you describe your intentions.  Posting nonworking code might not be enough in this case.

jhh197
Pyrite | Level 9

Hi ,

 

Sure

My table A

 

itemidscore
dis1351
ins22.12
ins32.15
ins40.05
gbs52
gbs 66

 

Table B

insgbs
37
25
56

 

and when i execute below query i get Table C as output but i get missing values for gbs 7 so it should take value from 6 which is 6  and ins 5 is missing so it should take from 3  which is 2.15

 

proc sql;
create table want as
select (select score from a  where item='dis') as dis,
       a.ins as dxnum,b.score as ins,
       a.gbs as hxnum,c.score as gbs
from
B as a
left join
(select * from a where item='ins') as b
on  a.ins = b.id
left join
(select * from a where item='gbs') as c
on a.gbs=c.id;
quit;

 

TABLE C

 

disdxnuminshxnumgbs
35122.1252
3515.66
35132.157.

 

 

Ksharp
Super User
data a;
infile cards expandtabs;
input item $ id sale_amt ;
cards;
dis     1       351
ins     2       2.12 
ins     3       2.15 
ins     4       0.05 
gbs     5       2.0
gbs     6       6.0
;
run;
data b;
infile cards expandtabs;
input ins   gbs;
cards;
3       7
2       5
5       6
;
run;



proc sql;
create table xyz as
 select (select sale_amt from a where item='dis') as dis,
       a.ins as x_id, b.sale_amt as ins,
       a.gbs as y_id,c.sale_amt as gbs
from b as a
left join
(select * from a where item='ins') as b
on a.ins=b.id
left join
( select * from a where item='gbs') as c
on a.gbs=c.id
;

create table want as
 select (select sale_amt from a where item='dis') as dis,
       x_id, 
       coalesce(ins,(select sale_amt from a having abs(x_id-id)=min(abs(x_id-id)))) as ins,
       y_id,
       coalesce(gbs,(select sale_amt from a having abs(y_id-id)=min(abs(y_id-id)))) as gbs
from xyz;
quit;
 
jhh197
Pyrite | Level 9

Hi Keshan,

 

Thank you so much for your help . Thanks a lot .

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 1003 views
  • 1 like
  • 3 in conversation