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

I have the following dataset:

 

data have;
  input Sl  Return;
datalines;
 1  4.5563
 2  4.8562
 3  0.0000
 4  3.5879
 5  0.0000
 6  0.0000
 7  0.0000
 8  0.0000
 9  2.5879
10  3.6879
run;

I want to do the following:

 

proc sql;

select count (return if return= 0) as zero_obs, zero_obs/count (return if return ne 0) as zero_perc from have;

quit;

 

Not getting it.

 

Help please.

 

Much thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Your syntax looks really strange (what languages uses "RETURN X IF Y" syntax?) and also the logic of your formula seems off.

 

In SAS it is simple because logical expressions evaluate to 0 or 1.

proc sql ;
select sum( return=0 ) as n_zero
     , count(*) as n 
     , calculated n_zero / calculated n as percent_zero
from have
;
quit;

In other flavors of SQL you will want to use a CASE statement.  Also since other languages do not have the CALCULATED keyword you might need to replicate the formulas or use a nested query to get both the count and the percentage in one query.

View solution in original post

2 REPLIES 2
LinusH
Tourmaline | Level 20

Instead om count(), use sum. In the expression use CASE, it's the SQL counterpart to IF.

sum(case return when 0 then 1 else 0 end)
Data never sleeps
Tom
Super User Tom
Super User

Your syntax looks really strange (what languages uses "RETURN X IF Y" syntax?) and also the logic of your formula seems off.

 

In SAS it is simple because logical expressions evaluate to 0 or 1.

proc sql ;
select sum( return=0 ) as n_zero
     , count(*) as n 
     , calculated n_zero / calculated n as percent_zero
from have
;
quit;

In other flavors of SQL you will want to use a CASE statement.  Also since other languages do not have the CALCULATED keyword you might need to replicate the formulas or use a nested query to get both the count and the percentage in one query.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 659 views
  • 1 like
  • 3 in conversation