DATA Step, Macro, Functions and more

Subsetting data with date and string for a do function

Reply
Contributor
Posts: 20

Subsetting data with date and string for a do function

I am getting error for this code, not knowing where I am missing.

 

data want;

data have;

if centre in ('Shai','Hub','imen') and Date_of_Birth between mdy(3,01,1953) and mdy(6,30,1974) then status = 'pass';

run;

 

 

Not sure where I am missing, I tried with where as well but not succeeding, apart from that also checked with do end , But could not get the required result, Can any one help on this

Thanks

 

 

 

 

Trusted Advisor
Posts: 1,231

Re: Subsetting data with date and string for a do function

Seems like you need set statement

 

data want;

set have;

if centre in ('Shai','Hub','imen') and Date_of_Birth between mdy(3,01,1953) and mdy(6,30,1974) then status = 'pass';

run;

Contributor
Posts: 20

Re: Subsetting data with date and string for a do function

Agree with you,

 

Actually it has to be

 

Data want;

set have;

run;

 

It was a typo sorry for that. However running the mentioned codes by replacing with Set is not deriving the expected result

Super User
Posts: 19,878

Re: Subsetting data with date and string for a do function

Also, you can't use BETWEEN with an IF statement, only a WHERE statement. So either change to where or switch the comparison to mdy(3, 1, 1953)<=date_of_birth<=mdy(6, 30, 1974)
Contributor
Posts: 20

Re: Subsetting data with date and string for a do function

Hello Reeza,

 

I got the solution, However if I use 'between and' for a date, and if I use 'Where' or 'IF' I am not getting the expected results, is it like if I am using 'between and' for a date can't I use 'where' and 'IF'

 

Thanks,

 

Super User
Posts: 11,343

Re: Subsetting data with date and string for a do function

Try:

 

if centre in ('Shai','Hub','imen') and (mdy(3,01,1953) le Date_of_Birth le mdy(6,30,1974)) then status = 'pass';

Ask a Question
Discussion stats
  • 5 replies
  • 259 views
  • 0 likes
  • 4 in conversation