SAS Programming

DATA Step, Macro, Functions and more
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;

sas-innovate-white.png

Join us for our biggest event of the year!

Four days of inspiring keynotes, product reveals, hands-on learning opportunities, deep-dive demos, and peer-led breakouts. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1246 views
  • 1 like
  • 4 in conversation