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

Hi guys, using the sql code below suggested by Hai.Kuo, I obtained  this  table:

Final table:

race            male   female     total

black              2      1             3

latin                2      1             3     

oriental           1       0             1

but now , considering a new imput of data, there are a subject that is duplicate, and I would like using the same sql but considering only distints subjects, then latin male has to be 1, instead of 2, and total has to be 2, then the table I want is:

race            male   female     total

black              2      1             3

latin                1      1             2     

oriental           1       0             1

Can you help me with this? Thanks. V.

****************************************************

*New input with a duplicate redord (subno=3);

data have;

length  subno 4 race $10 sex $10;

input subno race  sex ;

datalines;

1  black male

2  black female

3  latin male

3 latin male

4 black male

5 latin female

6 oriental male

;

run;

*code sugggested by code Hai.Kuo for an old input:

proc sql;

  select race, sum(sex='male') as male, sum(sex='female') as female, count(race) as total

  from have

  group by race;

quit;

1 ACCEPTED SOLUTION

Accepted Solutions
Alpay
Fluorite | Level 6

It will get you the unique rows for variables specified in select clause. It is similar to 'group by' but no summary statistics computed.

select distinct race, subno, sex from have

the result of this query:

1  black male

2  black female

3  latin male

4 black male

5 latin female

6 oriental male

Using the result of this sub-query, you can get the counts by race.

View solution in original post

3 REPLIES 3
Alpay
Fluorite | Level 6

You will need to get distinct values of race, subno and sex in a subquery and then sum it up.

proc sql;

  select race, sum(sex='male') as male, sum(sex='female') as female, count(race) as total

  from (select distinct race, subno, sex from have)

  group by race;

quit;

michtka
Fluorite | Level 6

Hello alpay, thanks.

Please, could you explain the the role of  distinct in that subquery?...i.g...which variable is affected for this...race?

I was thinking of something like:

from (select distinct subno, race, sex)

instead of your line of code.

I will aprreciate you can explain to me the difference.

Thanks.

Alpay
Fluorite | Level 6

It will get you the unique rows for variables specified in select clause. It is similar to 'group by' but no summary statistics computed.

select distinct race, subno, sex from have

the result of this query:

1  black male

2  black female

3  latin male

4 black male

5 latin female

6 oriental male

Using the result of this sub-query, you can get the counts by race.

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 1340 views
  • 3 likes
  • 2 in conversation