BookmarkSubscribeRSS Feed
attjooo
Calcite | Level 5

I have one table A with one variable ID ( for identity ) and another table B, with variables ID, CITY, VAR3, VAR4.

In table A each ID occurs only once.

In table B each ID can have more then one record.

I want the number of ID:s in each CITY. An ID should be counted in each city it occurs, so the total number could be more than the number of individuals in table A.

The result should be presented in a table C, with variables CITY and N, where N = number of individuals in each city.

I think there is no need to use table A.

Do you have any suggestions how to solve this problem?

Thanks for any help.

3 REPLIES 3
DBailey
Lapis Lazuli | Level 10

proc sql;

create table want as

select

     b.city,

     count(distinct a.id) as Distinct_IDs

from

     a

     inner join b

     on a.id=b.id

group by b.city;

quit;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

Well, you wouldn't need A as it doesn't add anything.  Try:

proc sql;

     create table C as

     select     distinct CITY,

                    count(ID) as N

     from          B

     group by CITY;

quit;

DBailey
Lapis Lazuli | Level 10

true...unless there may be IDs in table B that you don't want counted...or if you needed other attributes on table A that weren't included in B....(assumes facts not in evidence).

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!

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
  • 3 replies
  • 957 views
  • 0 likes
  • 3 in conversation