Help using Base SAS procedures

Datetime Question

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

Datetime Question

Hi,

I have variables like shown in the dataset

IN and OUT are datetimes

condition:

if the month&year is from OCT2013 to DEC 2013 and

if OUT is missing but has a datetime under the IN variable or VICE VERSA I want to export those records to a seperate dataset


ID       IN                                             OUT

101       .                                        03OCT2013:03:42

102   07OCT2013:18:58                        .

Thanks


Accepted Solutions
Solution
‎02-21-2014 12:11 PM
Trusted Advisor
Posts: 1,631

Re: Datetime Question

m1=month(datepart(in));

m2=month(datepart(out));

y1=year(datepart(in));

y2=year(datepart(out);

if (m1>=10 and m1<=12 and y1=2013) or (m2>=10 and m2<=12 and y2=2013) then do;

...

end;

View solution in original post


All Replies
Solution
‎02-21-2014 12:11 PM
Trusted Advisor
Posts: 1,631

Re: Datetime Question

m1=month(datepart(in));

m2=month(datepart(out));

y1=year(datepart(in));

y2=year(datepart(out);

if (m1>=10 and m1<=12 and y1=2013) or (m2>=10 and m2<=12 and y2=2013) then do;

...

end;

Super Contributor
Posts: 1,040

Re: Datetime Question

thats the only way??? i mean creating new variables and using if conditions ?

cant you use any code directly on the DATETIME ?

Thanks

Super User
Posts: 17,952

Re: Datetime Question

Yes you can. And you seem to know that, so what functions have you tried that didn't work?

This is a place to receive help, not have people do your work for you.

Super Contributor
Posts: 1,040

Re: Datetime Question

Honestly, I dont know if I can use any direct way.

May be the way i asked the question was not proper and gave the impression that I knew it..

Sorry about that

Thanks

Trusted Advisor
Posts: 1,631

Re: Datetime Question

robertrao wrote:

thats the only way??? i mean creating new variables and using if conditions ?

cant you use any code directly on the DATETIME ?

Thanks

No one ever said or implied that this is the only way to do something. It's an example, one way to do it, you could easily combine what I did into one statement and not create additional variables (although I fail to see a problem with creating temporary variables, you don't have to save the additional variables). What exactly is the problem with doing it with additional variables?

Frequent Contributor
Posts: 83

Re: Datetime Question

You could also do:

For the time constraint:

if ('01oct2013'd<=datepart(in) <='31dec2013'd) or ('01oct2013'd<=datepart(out) <='31dec2013'd) then do;

This way you don't have to create new variables, which it seemed that you were disinclined to do.

For the second part you could do the code below.

For the missing constraint:

if (in = . and out ~=.) or (in~=. and out=.) then do;

output;

end;

Hope this is more what you were looking for.

Super User
Super User
Posts: 6,502

Re: Datetime Question

What did you try?  Did it give you error messages?  Or did it give you wrong the results?

You seem to have three conditions in your selection criteria.

1) IN = .

2) OUT = .

The third part is a little vague. 

One way to interpret it is that the interval IN-OUT completely covers oct/nov/dec of 2013.

  (in <= '01OCT2013:00:00'dt and '01DEC2013:00:00'dt <= out)

Valued Guide
Posts: 2,175

Re: Datetime Question

condition:

if the month&year is from OCT2013 to DEC 2013 and

if OUT is missing but has a datetime under the IN variable or VICE VERSA I want to export those records to a seperate dataset

ID       IN                                             OUT

101       .                                        03OCT2013:03:42

102   07OCT2013:18:58                       

If n( in, out)=1 & "1oct2013:0:0:0"dt <= max( in, out ) <= "31Dec2013:23:59:59.999"dt then output separate_data

🔒 This topic is solved and locked.

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

Discussion stats
  • 8 replies
  • 282 views
  • 0 likes
  • 6 in conversation