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

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

CategoryNo_of_not_3_in_the_same_ categoryTotal_No_in_Category
A33
A33
A33
B22
B21

 

My expected result

CategoryNo_of_not_1_in_the_same_ categoryTotal_No_in_Category
A23
B12

 

I wonder how to achieve the expected result using only proc SQL. Thank you so much.

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
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;

View solution in original post

1 REPLY 1
novinosrin
Tourmaline | Level 20
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;
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
  • 1 reply
  • 595 views
  • 0 likes
  • 2 in conversation