SAS Version 9.4 / Omit SAS/STATS or SAS/IML
Good day -
The message in the log reads:
ERROR: Ambiguous reference, column bird is in more than one table.
This is the source code:
data work.g_calc;
input bird $ gmass;
cards;
bird1 11
bird3 33
bird6 66
;
run;
data work.h_calc;
input bird $ hmass;
cards;
bird1 1
bird2 2
bird3 3
bird4 4
bird5 5
bird6 6
;
run;
data work.c_calc;
input bird $ cmass;
cards;
bird1 10
bird2 20
bird3 30
bird4 40
bird5 50
bird6 60
;
run;
%let gtotal=110;
%let htotal=21;
%let ctotal=210;
proc sql;
create table work.equation as
select distinct bird,
sum(H.hmass,G.gmass,C.cmass)/(&htotal. + >otal. + &ctotal.) as result
from work.h_calc as H, work.g_calc as G,work.c_calc as C
where H.bird=G.bird or G.bird=C.bird or C.bird=H.bird;
quit;
What is the best way to get rid of the error message and successfully compile to get results of the equation which is the sum of the bird masses over sum of totals.
Also tried using "select unique bird" on the second line of "proc sql", but the same error.
Output should be approximately:
bird result
bird1 0.0645
bird2 0.0645
bird3 0.1935
bird4 0.129
bird5 0.16129
bird6 0.38709
where 22/341=0.0645, 66/341=0.1935, 44/341=0.129, 55/341=0.16129, and 132/341=0.38709
Thank you in advance.
Jane
Hi @jawhitmire
What you need is a full join based on your expected result.
proc sql;
create table work.equation1 as
select h.bird,
sum(H.hmass,G.gmass,C.cmass)/(&htotal. + >otal. + &ctotal.) as result
from work.h_calc as H full join work.g_calc as G
on H.bird=G.bird
full join
work.c_calc as C
on C.bird=H.bird;
quit;
The SAS System |
bird | result |
---|---|
bird1 | 0.064516 |
bird2 | 0.064516 |
bird3 | 0.193548 |
bird4 | 0.129032 |
bird5 | 0.16129 |
bird6 | 0.387097 |
Reference correctly in your select statement
select distinct h.bird
You're matching when the two of the birds on the table agree, regardless if the third agree's. So you could have:
bird1 bird1 bird3 -> which value do you want to appear in the table?
Or, should the OR conditions in your WHERE statement be AND instead?
I suspect that's actually what you're trying to do.
@jawhitmire wrote:
SAS Version 9.4 / Omit SAS/STATS or SAS/IML
Good day -
The message in the log reads:
ERROR: Ambiguous reference, column bird is in more than one table.
This is the source code:
data work.g_calc; input bird $ gmass; cards; bird1 11 bird3 33 bird6 66 ; run; data work.h_calc; input bird $ hmass; cards; bird1 1 bird2 2 bird3 3 bird4 4 bird5 5 bird6 6 ; run; data work.c_calc; input bird $ cmass; cards; bird1 10 bird2 20 bird3 30 bird4 40 bird5 50 bird6 60 ; run; %let gtotal=110; %let htotal=21; %let ctotal=210; proc sql; create table work.equation as select distinct bird, sum(H.hmass,G.gmass,C.cmass)/(&htotal. + >otal. + &ctotal.) as result from work.h_calc as H, work.g_calc as G,work.c_calc as C where H.bird=G.bird or G.bird=C.bird or C.bird=H.bird; quit;
What is the best way to get rid of the error message and successfully compile to get results of the equation which is the sum of the bird masses over sum of totals.
Also tried using "select unique bird" on the second line of "proc sql", but the same error.
Thank you in advance.
Jane
Thank you for the feedback.
Next, I tried...
proc sql;
create table work.equation as
select distinct h.bird,
sum(H.hmass,G.gmass,C.cmass)/(&htotal. + >otal. + &ctotal.) as result
from work.h_calc as H, work.g_calc as G,work.c_calc as C
where H.bird=G.bird and G.bird=C.bird and C.bird=H.bird;
quit;
It runs successfully - thank you.
The problem is that there is only 3 observations in the output:
bird result
bird1 0.06452
bird3 0.19355
bird6 0.38710
I would like the calculation performed for all 6 birds - is this possible with "proc sql'?
Thanks,
Jane
What does your desired output look like from your example data? First, you have to reference which table bird should come from, such as C.bird in your select statement.
Hi @jawhitmire
What you need is a full join based on your expected result.
proc sql;
create table work.equation1 as
select h.bird,
sum(H.hmass,G.gmass,C.cmass)/(&htotal. + >otal. + &ctotal.) as result
from work.h_calc as H full join work.g_calc as G
on H.bird=G.bird
full join
work.c_calc as C
on C.bird=H.bird;
quit;
The SAS System |
bird | result |
---|---|
bird1 | 0.064516 |
bird2 | 0.064516 |
bird3 | 0.193548 |
bird4 | 0.129032 |
bird5 | 0.16129 |
bird6 | 0.387097 |
or something like this perhaps
select distinct coalescec(h.bird,G.bird,C.bird) as bird
proc sql; create table work.equation as select distinct bird, sum(H.hmass,G.gmass,C.cmass)/(&htotal. + >otal. + &ctotal.) as result from work.h_calc as H, work.g_calc as G,work.c_calc as C where H.bird=G.bird or G.bird=C.bird or C.bird=H.bird; quit;
So, which of the 3 birds are supposed to provide the distinct bird.
One of the color highlighted versions should be the selection.
Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.