DATA STEP QUESTION

Accepted Solution Solved
Reply
Super Contributor
Posts: 1,041
Accepted Solution

DATA STEP QUESTION

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;


Accepted Solutions
Solution
‎05-26-2015 04:16 PM
Super User
Posts: 11,343

Re: DATA STEP QUESTION

Posted in reply to robertrao

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


All Replies
Super Contributor
Posts: 275

Re: DATA STEP QUESTION

Posted in reply to robertrao

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;

Super User
Posts: 5,505

Re: DATA STEP QUESTION

Posted in reply to robertrao

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

Super Contributor
Posts: 1,041

Re: DATA STEP QUESTION

Posted in reply to Astounding

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).

Solution
‎05-26-2015 04:16 PM
Super User
Posts: 11,343

Re: DATA STEP QUESTION

Posted in reply to robertrao

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.

Super Contributor
Posts: 1,041

Re: DATA STEP QUESTION

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'));

Super User
Posts: 11,343

Re: DATA STEP QUESTION

Posted in reply to robertrao

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.

Super Contributor
Posts: 1,041

Re: DATA STEP QUESTION

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

Valued Guide
Posts: 860

Re: DATA STEP QUESTION

Posted in reply to robertrao

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;

Super Contributor
Posts: 1,041

Re: DATA STEP QUESTION

Posted in reply to Steelers_In_DC

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')));

Valued Guide
Posts: 860

Re: DATA STEP QUESTION

Posted in reply to robertrao

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.

Super Contributor
Posts: 1,041

Re: DATA STEP QUESTION

Posted in reply to Steelers_In_DC

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

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 11 replies
  • 284 views
  • 6 likes
  • 5 in conversation