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

I would like to do a 'where' statement within a mathematic SQL function. That is, I want to have the total number of patients in one column (N_SUBJ) and the total number of patients that meet a specific criteria to be a different column (N_DEV).

 

Code: I've been messing with the first line to see what will get rid of the initial error. 

proc sql;
create table CohortA as
/* Eligibility/enrollment */
select '_1Elig' as CAT, '_01AnyElig' as DEVTYPE, count(distinct PATID) as N_SUBJ, (count(distinct PATID from dv4 where DEV3DECOD in ('03', '04', '06', '99'))) as N_DEV
from dv4 where DVCAT = '01' and ARMCD in ('A', 'A_AND_B')
union
select '_1Elig' as CAT, '_02NoIncl' as DEVTYPE, count(distinct PATID) as N_SUBJ, count(distinct PATID) where DEV3DECOD = '03' as N_DEV
from dv4 where DVCAT = '01' and ARMCD in ('A', 'A_AND_B')
select '_1Elig' as CAT, '_03Excl' as DEVTYPE, count(distinct PATID) as N_SUBJ, count(distinct PATID) where DEV3DECOD = '04' as N_DEV
from dv4 where DVCAT = '01' and ARMCD in ('A', 'A_AND_B')
union
select '_1Elig' as CAT, '_04NoSign' as DEVTYPE, count(distinct PATID) as N_SUBJ, count(distinct PATID) where DEV3DECOD = '06' as N_DEV
from dv4 where DVCAT = '01' and ARMCD in ('A', 'A_AND_B') select '_1Elig' as CAT, '_01AnyElig' as DEVTYPE, count(distinct PATID) as N_SUBJ, count(distinct PATID) where DEV3DECOD in ('03', '04', '06', '99')
from dv4 where DVCAT = '01' and ARMCD in ('A', 'A_AND_B')
union
select '_1Elig' as CAT, '_05Oth' as DEVTYPE, count(distinct PATID) as N_SUBJ, count(distinct PATID) where DEV3DECOD = '99' as N_DEV
from dv4 where DVCAT = '01' and ARMCD in ('A', 'A_AND_B') select '_1Elig' as CAT, '_01AnyElig' as DEVTYPE, count(distinct PATID) as N_SUBJ, count(distinct PATID) where DEV3DECOD in ('03', '04', '06', '99')
from dv4 where DVCAT = '01' and ARMCD in ('A', 'A_AND_B');
quit;

Table Desire:

mariko5797_0-1629231805539.png

Thank you in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

I cant' make heads or tails out of your posted code.  So let's just look at your question instead.

 

I would like to do a 'where' statement within a mathematic SQL function. That is, I want to have the total number of patients in one column (N_SUBJ) and the total number of patients that meet a specific criteria to be a different column (N_DEV).

So get a count in SQL use the COUNT() aggregate function.  It will ignore (count as zero) missing values.  So just use a CASE clause that returns a missing value for the observation you don't want to count.

proc sql;
select count(*) as n_students
     , count(case when sex='F' then 1 else . end) as n_female
     , count(case when sex='M' then 1 else . end) as n_male
from sashelp.class
;
quit;

In SAS you can also use the fact that boolean expressions evaluate as 1 for TRUE and 0 for FALSE.

proc sql;
select count(*) as n_students
     , sum(sex='F') as n_female
     , sum(sex='M') as n_male
from sashelp.class
;
quit;

View solution in original post

4 REPLIES 4
mariko5797
Pyrite | Level 9

*Note: I just realized some other parts of my code are wrong. Please ignore them for the sake of the question posed. Thank you

Reeza
Super User
It looks like your code is roughly the same except for this:
DEV3DECOD

Is that case?

It also looks like you're trying to do totals and then subtotals in the same query? Have you tried some of more standard procs like MEANS or FREQ that do these out of the box? For the distinct count that may have to be by SQL or double PROC FREQ?
ChrisNZ
Tourmaline | Level 20

Why does N_SUBJ change when there's no condition attached to it?

Tom
Super User Tom
Super User

I cant' make heads or tails out of your posted code.  So let's just look at your question instead.

 

I would like to do a 'where' statement within a mathematic SQL function. That is, I want to have the total number of patients in one column (N_SUBJ) and the total number of patients that meet a specific criteria to be a different column (N_DEV).

So get a count in SQL use the COUNT() aggregate function.  It will ignore (count as zero) missing values.  So just use a CASE clause that returns a missing value for the observation you don't want to count.

proc sql;
select count(*) as n_students
     , count(case when sex='F' then 1 else . end) as n_female
     , count(case when sex='M' then 1 else . end) as n_male
from sashelp.class
;
quit;

In SAS you can also use the fact that boolean expressions evaluate as 1 for TRUE and 0 for FALSE.

proc sql;
select count(*) as n_students
     , sum(sex='F') as n_female
     , sum(sex='M') as n_male
from sashelp.class
;
quit;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 552 views
  • 1 like
  • 4 in conversation