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;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.