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

Hello:

 

I have some codes from the past (see the end).   I would like to modify the following codes as my request below.

 

1.  Bloodculture = 1 and CultureDate ^=. ( Must have)

2.  When AdmitDate is not missing, CultureDate less than AdmitDate . (Option 1)

3.  When DischargeDate is not missing, CultureDate less than DischargeDate . (Option 2)

4.  When ERAdmitDate is not missing, CultureDate less than ERAdmitDate . (Option 3)

 

I have over 30 similar codes using PROC SQL to do the inquiry.   Therefore, I am ONLY looking for using 'PROC SQL WHERE' to do this task.   Could someone help me how to modify the codes below and meet the requirement.   Thank you.

 

 

 

proc sql;
	create table Want as
 	select site, id, culturedate, dischargedate, admitdate,ERadmitdate, bloodculture
 	from have
 	where bloodculture = 1 and culturedate ^= . or (culturedate lt admitdate-1 and admitdate ne .) or (culturedate gt dischargedate and dischargedate ne .) 
			or (culturedate lt EDadmitdate-1 and EDadmitdate ne .)
 	order by site, id;
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Don't mix AND and OR without proper grouping or you won't get what you want.

Do you want this:

where bloodculture = 1
  and culturedate ^= . 
  AND ( (culturedate lt admitdate-1 and admitdate ne .)
     or (culturedate gt dischargedate and dischargedate ne .)
     or (culturedate lt EDadmitdate-1 and EDadmitdate ne .)
      )

 

Or this:

where ( bloodculture = 1
    and culturedate ^= . 
      )
  OR  ( (culturedate lt admitdate-1 and admitdate ne .)
     or (culturedate gt dischargedate and dischargedate ne .)
     or (culturedate lt EDadmitdate-1 and EDadmitdate ne .)
      )

 

View solution in original post

5 REPLIES 5
Astounding
PROC Star

Here's the overall format for the WHERE clause.  You will need to supply the correct logic for each option.

 

where bloodculture = 1 and culturedate ne . and
   ( (logic for option 1) or
     (logic for option 2) or
     (logic for option 3)
   )
Tom
Super User Tom
Super User

Don't mix AND and OR without proper grouping or you won't get what you want.

Do you want this:

where bloodculture = 1
  and culturedate ^= . 
  AND ( (culturedate lt admitdate-1 and admitdate ne .)
     or (culturedate gt dischargedate and dischargedate ne .)
     or (culturedate lt EDadmitdate-1 and EDadmitdate ne .)
      )

 

Or this:

where ( bloodculture = 1
    and culturedate ^= . 
      )
  OR  ( (culturedate lt admitdate-1 and admitdate ne .)
     or (culturedate gt dischargedate and dischargedate ne .)
     or (culturedate lt EDadmitdate-1 and EDadmitdate ne .)
      )

 

ybz12003
Rhodochrosite | Level 12

Awesome!  Thanks much, Tom!

PGStats
Opal | Level 21

Warning: Subsetting conditions involving OR clauses are handled inefficiently by SAS SQL.

 

Sometimes, such logical conditions can't be avoided. But sometimes they can. See above.

PG
PGStats
Opal | Level 21

 

proc sql;
create table Want as
select 
    site, 
    id, 
    cultureDate, 
    dischargeDate, 
    EDadmitDate, 
    ERadmitDate, 
    bloodCulture
from have
where 
    bloodCulture = 1 and 
    cultureDate is not missing and
    cultureDate < max(EDadmitDate, dischargeDate, ERadmitDate)
order by site, id;
PG

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!

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
  • 5 replies
  • 914 views
  • 2 likes
  • 4 in conversation