Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

🔒 This topic is **solved** and **locked**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 03-22-2019 11:48 AM
(5227 views)

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

1 ACCEPTED SOLUTION

Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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 |

10 REPLIES 10

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Reference correctly in your select statement

select distinct **h.bird**

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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 |

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Thank you for the feedback. This was the perfect solution. Jane

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

or something like this perhaps

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

proc sql; create table work.equation as select distinctbird, 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 whereH.bird=G.birdor G.bird=C.birdor 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.

**Available on demand!**

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

Upcoming Events

- SAS Halifax Region User Group (SHRUG) Meeting | 23-May-2024
- What’s New in SAS Optimization? | 23-May-2024
- Montreal SAS User Group (MONSUG) Meeting | 29-May-2024
- Club des utilisateurs SAS de Québec | 30-May-2024
- Optimizing Logistics Operations Using Customized Solutions | 04-Jun-2024
- Toronto SAS Users' Community Luncheon | 04-Jun-2024
- Ottawa Area SAS Users' Society (OASUS) Meeting | 06-Jun-2024

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.