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

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. + &gtotal. + &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

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

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. + &gtotal. + &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

View solution in original post

10 REPLIES 10
novinosrin
Tourmaline | Level 20

Reference correctly in your select statement

 

select distinct h.bird

Reeza
Super User

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. + &gtotal. + &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

 


 

jawhitmire
Quartz | Level 8

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. + &gtotal. + &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

 

 

 

 

 

jawhitmire
Quartz | Level 8
Changed 'or' statement to 'and'. Thank you.
Output performs calculation for just 3 birds. Is there a way to get output for all 6 as follows:

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

PeterClemmensen
Tourmaline | Level 20

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.

jawhitmire
Quartz | Level 8
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
novinosrin
Tourmaline | Level 20

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. + &gtotal. + &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
jawhitmire
Quartz | Level 8
Thank you for the feedback. This was the perfect solution. Jane
novinosrin
Tourmaline | Level 20

or something like this perhaps

 

	select distinct coalescec(h.bird,G.bird,C.bird) as bird

 

 

 

ballardw
Super User
proc sql;
	create table work.equation as
	select distinct bird,
	sum(H.hmass,G.gmass,C.cmass)/(&htotal. + &gtotal. + &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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 10 replies
  • 5228 views
  • 6 likes
  • 5 in conversation