turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- SAS Time Range

Topic Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Wednesday

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!

Accepted Solutions

Solution

Wednesday

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to riccardo88

Wednesday

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.

All Replies

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to riccardo88

Wednesday

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

Paige Miller

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to PaigeMiller

Wednesday

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.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to riccardo88

Wednesday

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

`%let time1=YOUR-TIME;`

And then call it:

`&time1 < TIME < &time2`

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to riccardo88

Wednesday

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

Paige Miller

Solution

Wednesday

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to riccardo88

Wednesday

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.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Astounding

Wednesday

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?

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to riccardo88

Wednesday

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

--

Paige Miller

Paige Miller

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to PaigeMiller

Wednesday

Yes, it is.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to riccardo88

Wednesday

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

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

and then work with ntime

--

Paige Miller

Paige Miller

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to PaigeMiller

Wednesday

It worked! Thank you very much!

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Astounding

Wednesday

It worked! Thank you very much!!

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to riccardo88

Wednesday

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