Calcite | Level 5

## SAS Time Range

Hallo everybody,

I'm a beginner with SAS and I'm stucked on a Point.

I have a datasets of this kind:

I'm interested in having a dataset that contains only the data beetwen some time ranges and delete the rest.

Is there a way to simplify the process with only one "if" statement?

For example:

Starting_time_1 = 10:02:32

Starting_time_2 = 10:08:52

Starting_time_3 = 10:21:04

Time Ranges:          Starting _time_ 1 <= Time <=  Starting_time_1  + 3 Minutes

Starting _time_ 2 <= Time <=  Starting_time_2  + 3 Minutes

Starting _time_ 3 <= Time <=  Starting_time_3 + 3 Minutes

I tried different ways but they didn't work.

I hope my explanation was clear. Thank you very much for your help!

1 ACCEPTED SOLUTION

Accepted Solutions
PROC Star

## Re: SAS Time Range

If your Time variable is a true SAS time, you could use:

starting_time_1 = '10:02:32't;

starting_time_2 = '10:08:52't;

starting_time_3 = '10:21:04't;

if (starting_time_1 <= time <= starting_time_1 + 180)

or (starting_time_2 <= time <= starting_time_2 + 180)

or (starting_time_3 <= time <= starting_time_3 + 180);

Time variables are measured in number of seconds, so adding 180 is 3 minutes later.

12 REPLIES 12
Diamond | Level 26

## Re: SAS Time Range

So if the time is between any of those three ranges then you keep it, if it is outside these three ranges then you don't keep it? Is that correct?

Show us your SASLOG. Explain what went wrong.

--
Paige Miller
Calcite | Level 5

## Re: SAS Time Range

Exaclty. If it is in those ranges I Keep them (Actually I have to do it for five ranges, that was only an example), if they are outside I delete them.

I don't think what I wrote made a lot of sense. I was looking for a way to create a variable and then use it in the if Statement but I´m not sure this is possible in SAS.

Pyrite | Level 9

## Re: SAS Time Range

For "creating a variable to use in the statement" create a macro variable.

``%let time1=YOUR-TIME;``

And then call it:

``&time1 < TIME < &time2``
Diamond | Level 26

## Re: SAS Time Range

So @Astounding has provided code for the case where you have 3 time ranges. It should be easy to modify if you have 5 (or any number) of time ranges.

--
Paige Miller
PROC Star

## Re: SAS Time Range

If your Time variable is a true SAS time, you could use:

starting_time_1 = '10:02:32't;

starting_time_2 = '10:08:52't;

starting_time_3 = '10:21:04't;

if (starting_time_1 <= time <= starting_time_1 + 180)

or (starting_time_2 <= time <= starting_time_2 + 180)

or (starting_time_3 <= time <= starting_time_3 + 180);

Time variables are measured in number of seconds, so adding 180 is 3 minutes later.

Calcite | Level 5

## Re: SAS Time Range

Thank you Astounding. This is my code:

data eeg_inf.out_100_test; set eeg_inf.out_100;

starting_time_1 = '10:02:32't;

starting_time_2 = '10:08:52't;

starting_time_3 = '10:21:04't;

if (starting_time_1 <= time <= starting_time_1 + 180)

or (starting_time_2 <= time <= starting_time_2 + 180)

or (starting_time_3 <= time <= starting_time_3 + 180);

run;

I get a series of error Messages like this:

NOTE: Ungültige numerische Daten, Time='10:05:34', in Zeile 1253 Spalte 24.

NOTE: Ungültige numerische Daten, Time='10:05:34', in Zeile 1254 Spalte 24.

NOTE: Ungültige numerische Daten, Time='10:05:34', in Zeile 1255 Spalte 24.

Translated from the german it means "Invalid numeric data". Is my code right?

Diamond | Level 26

## Re: SAS Time Range

Seems as if your variable TIME is character rather than numeric. Is that correct?

--
Paige Miller
Calcite | Level 5

## Re: SAS Time Range

Yes, it is.

Diamond | Level 26

## Re: SAS Time Range

Then you need to convert it to numeric, for example:

``ntime = input(time,hhmmss8.);``

and then work with ntime

--
Paige Miller
Calcite | Level 5

## Re: SAS Time Range

It worked! Thank you very much!
Calcite | Level 5

## Re: SAS Time Range

It worked! Thank you very much!!
Pyrite | Level 9

## Re: SAS Time Range

Do you yet know how dates and times are stored in SAS?

Date: # of days since 1/1/11960

Date time: # of secs since 1/1/1960

Time: Can be a date time, where you're only showing the time; commonly, this is just the of seconds since midnight and if you were to reformat as a date time your date would be 1/1/1960.

I'd recommend that you convert your time to a # and make sure you know what is there. You can also do that with your thresholds. Use the SAS function hms like so:

``````format time1 best8.;
time1=hms(10,2,32);``````
Discussion stats
• 12 replies
• 2223 views
• 0 likes
• 4 in conversation