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

Hi,

From the below I want to pick discharges which fall into a specific date range(Jan2012-Dec2013) and certain dept_ID's (101,102,103, 104, 105) only

some of them might not have Dept_ID so we pull Dept_Names for those!!!! We want which ever also satisfies DEPT_ID of 'SSS'

ADMIT                DISCH        DEPT_ID      DEP_NAME    PAT_ID

Jan10,2015     Jan10 ,2015         101                 OBG         1----don't want becos of date, even though DEPT_ID is matching

Mar15,2013     Jun,12,2015         102                 OPB         2----don't want becos of date ,even though DEPT_ID is matching

jul10,2013        Jul25,2015            103               ODX          3----don't want becos of date, even though DEPT_ID is matching

feb2,2012        mar3,2012              104              BDX           4---want becos DEPT_ID is matching and date is matching

mar15,2013     mar30,2013            105             YYY            5---want becos DEPT_ID is matching and date is matching

apr10,2013       may20,2013                              SSS           6 -------want becos of DEPT_NAME is SSS and date is matching

may21,2013      jun15,2013                               www           7-------don't want becos we don't want these DEPT_NAMES (not interested).

mar12,2013      mar13,2013          109               UHC          8------don't want becos of DEPT_ID

WHICH OF THE BELOW IS CORRECT USAGE????

data want;

set have;

where admit ge Jan2012 and admit le Dec2013;

where same and  DEPT_ID in (101,102,103, 104, 105);

where same and DEPT_NAME (SSS);

run;

data want;

set have;

where admit ge Jan2012 and admit le Dec2013;

where same and  DEPT_ID in (101,102,103, 104, 105 or DEPT_NAME in (SSS);

run;

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Are your dates SAS date value or strings?

As you use Jan2012 as shown that would be treated as a variable name and likely fail. If a string value then the values have to be in quotes: 'Jan2012' and are going to be case sensitive. If the variable compared is a SAS date value then you need to compare to SAS date literal which must include a day of the month: '01jan2012'D.

IF SAS date values then

where '01jan2012'd le admit le '31DEC2013'd

and (dept_id in (101,102,103, 104, 105 ) or dept_name in ('SSS'); Name strings in quotes.

Are your Dept_id variables text or numeric. If text they need to be in quotes.

If your admin variable is text then you need to convert it to a date value for comparison to the date literals.

View solution in original post

11 REPLIES 11
slchen
Lapis Lazuli | Level 10

With multiple where conditions, only last condition will be selected, for your selection, try to do:

data want;

set have;

where admit ge Jan2012 and admit le Dec2013 and (DEPT_ID in (101,102,103, 104, 105) or DEPT_NAME='SSS');

run;

Astounding
PROC Star

What should happen if DEPT_ID is 555, but DEPT_NAME is "SSS"?  Leave those out?

robertrao
Quartz | Level 8

Thanks for the replies.

We have to take those because we want SSS if its satisfying our date condition

The dept ID may not be necessarily what we desired in this case!!!!(in other words when looking at DEPT_NAME variable for SSS it can have anything under DEPT_ID as long as it satisfies SSS).

ballardw
Super User

Are your dates SAS date value or strings?

As you use Jan2012 as shown that would be treated as a variable name and likely fail. If a string value then the values have to be in quotes: 'Jan2012' and are going to be case sensitive. If the variable compared is a SAS date value then you need to compare to SAS date literal which must include a day of the month: '01jan2012'D.

IF SAS date values then

where '01jan2012'd le admit le '31DEC2013'd

and (dept_id in (101,102,103, 104, 105 ) or dept_name in ('SSS'); Name strings in quotes.

Are your Dept_id variables text or numeric. If text they need to be in quotes.

If your admin variable is text then you need to convert it to a date value for comparison to the date literals.

robertrao
Quartz | Level 8

I think this below by bakllardw is explanatory. But maybe he forgot the final brace bolded here.....

I think this works and the OR condition is within the brace????

Any comments/suggestions???

where '01jan2012'd le admit le '31DEC2013'd

and (dept_id in (101,102,103, 104, 105 ) or dept_name in ('SSS'));

ballardw
Super User

Yes I missed the final ).

Paste the line into the editor. Pick a parentheses. Hit Ctrl-(. The cursor will move to the matching one. If it doesn't move, there isn't a match.

robertrao
Quartz | Level 8

How about this:

where (('01jan2012'd le admit le '31DEC2013'd)

and (dept_id in (101,102,103, 104, 105 ) or dept_name in ('SSS')));

Thanks

Steelers_In_DC
Barite | Level 11

data have;

infile cards dsd;

informat admit disch date9.;

format admit disch mmddyy10.;

input ADMIT DISCH DEPT_ID DEP_NAME $ PAT_ID;

cards;

10Jan2015,10JAN2015,101,OBG,1

15Mar2013,12Jun2015,102,OPB,2

10jul2013,25Jul2015,103,ODX,3

2feb2012,3mar2012,104,BDX,4

15mar2013,30mar2013,105,YYY,5

10apr2013,20may2013,,SSS,6

21may2013,15jun2013,,www,7

12mar2013,13mar2013,109,UHC,8

;

run;

data want;

set have;

where (('01JAN2012'd < disch < '12DEC2013'd) and

       (dept_id in (101,102,103,104,105))) or

        dep_name = 'SSS';

run;

robertrao
Quartz | Level 8

Thanks a lot for the reply again.

I was wondering why is the OR condition outside the braces????why not I write like this below????

where (('01JAN2012'd < disch < '12DEC2013'd) and

       (dept_id in (101,102,103,104,105  or

        dep_name = 'SSS')));

Steelers_In_DC
Barite | Level 11

To get the results you want the 'SSS' has to be independent of the rest of the clause.  The dept_id in () should only have values that are valid for dept_id.

robertrao
Quartz | Level 8

Thanks you so very much.

But, would not 'SSS' which were pulled have records which do not match our date criteria.????

I mean if DEPT_NAME is 'SSS' for a record having ADMIT in 2015 , wouldn't that be pulled also????We don't want cases likethese right since its 2015????

Thansk

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 11 replies
  • 921 views
  • 6 likes
  • 5 in conversation