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

Hello,

I have a dataset as below. I want to count as total,count cat where it is A and group by year.

Obs year cat12345678910

2018A
2019B
2021C
2019A
2021A
2021B
2018C
2019C
2021A
2021A

 I am using below code but it is not working.

proc sql;
select year,
count(*) as total,
(select count(cat) from try where cat = 'A') as n_a

from try
group by year;
quit;

Above code produced this:

 year total n_a

201825
201935
202155

where total is correctly grouped but n_a is not.

I wanted this code to make to work because there are other ways to solve this issue ,for instance i can use count with " case when".

Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
sascode
Quartz | Level 8

Thank you,

Unfortunately it does not work.

View solution in original post

5 REPLIES 5
ChrisNZ
Tourmaline | Level 20

select count(*) as total
,sum(cat = 'A') as n_a

sascode
Quartz | Level 8

Thank you,

Unfortunately it does not work.

sascode
Quartz | Level 8

My apologise ,

Is working correctly.

Thank you.

Ksharp
Super User
proc sql;
select year,count(*) as total,sum(cat='A') as n_a
from have
group by year;
quit;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 5 replies
  • 703 views
  • 1 like
  • 4 in conversation