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

Hello everyone, 

 

I have a  Proc sql select distinct programm on SAS that counts the number of code (one code desginates individuals pertaining to the same group, so code 5 designates all individuals in group 5) by company: 

PROC SQL;
CREATE TABLE nbcode AS
SELECT DISTINCT code, COUNT(code) AS numbercode
FROM work.example
GROUP BY COMP;
QUIT;

 

It gives me something like this: 

code          nombrecode

1                  1

2                  1

3                  1

4                 10

5                   1

5                6

6                 1

 

That means that; for example, 6 individuals of group 5 are in one company and one individual of group 5 is in a company alone. 

 

I would like to know how to add to this table the company identifier (designated by the variable COMP). I thought the by comp would put automatically the comp identifier in a column to the right, but i was wrong, and i can not manage to do this ...

 

Could you help me please?

 

More generally, how to add other variables we wish to have in a select distinct table?

 

Thank you very much for your help, 

Best, 

Eugenie

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

If you want a variable in the output then it needs to be listed in the variable list part of the SELECT statement.

Do you want the list of CODE values nested inside the list of COMP values?

If you want to count how many times each COMP x CODE combination appears you would want this code:

proc sql;
create table nbcode as
  select comp
       , code
       , count(*) as nobs
  from work.example
  group by 1,2
;
quit;

If you wanted to count how many distinct value of CODE appear within each value of COMP you would want this code:

proc sql;
create table nbcode as
  select comp
       , count(distinct code) as ncodes
  from work.example
  group by 1
;
quit;

 

 

 

 

View solution in original post

4 REPLIES 4
Reeza
Super User

Post sample data that shows what you have and what you want.

ballardw
Super User

Did you try putting the Comp variable on the select statement?

Tom
Super User Tom
Super User

If you want a variable in the output then it needs to be listed in the variable list part of the SELECT statement.

Do you want the list of CODE values nested inside the list of COMP values?

If you want to count how many times each COMP x CODE combination appears you would want this code:

proc sql;
create table nbcode as
  select comp
       , code
       , count(*) as nobs
  from work.example
  group by 1,2
;
quit;

If you wanted to count how many distinct value of CODE appear within each value of COMP you would want this code:

proc sql;
create table nbcode as
  select comp
       , count(distinct code) as ncodes
  from work.example
  group by 1
;
quit;

 

 

 

 

eugenia67
Fluorite | Level 6

It worked! 

 

Thank you! 

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
  • 4 replies
  • 963 views
  • 1 like
  • 4 in conversation