My dataset
data mydata; input Category $ Item $; datalines; A 1 A 1 A 2 B 3 B 1 ;
My attempt
proc sql;
create table mytable as
select *, count(Category) as Total_No_in_Category, count(Category)-count(item, "3") as No_of_not_3_in_the_same_category from mydata
group by Category;
run;Result
| Category | No_of_not_3_in_the_same_ category | Total_No_in_Category |
| A | 3 | 3 |
| A | 3 | 3 |
| A | 3 | 3 |
| B | 2 | 2 |
| B | 2 | 1 |
My expected result
| Category | No_of_not_1_in_the_same_ category | Total_No_in_Category |
| A | 2 | 3 |
| B | 1 | 2 |
I wonder how to achieve the expected result using only proc SQL. Thank you so much.
data mydata;
input Category $ Item $;
datalines;
A 1
A 1
A 2
B 3
B 1
;
proc sql;
create table want as
select category, sum(item ne '1') as No_of_not_1_in_the_same_category, count(Category) as Total_No_in_Category
from mydata
group by Category;
quit;
data mydata;
input Category $ Item $;
datalines;
A 1
A 1
A 2
B 3
B 1
;
proc sql;
create table want as
select category, sum(item ne '1') as No_of_not_1_in_the_same_category, count(Category) as Total_No_in_Category
from mydata
group by Category;
quit;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.