DATA Step, Macro, Functions and more

SAS Time Range

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

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:

 

Unbenannt.png

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
Super User
Posts: 6,899

Re: SAS Time Range

Posted in reply to riccardo88

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.

View solution in original post


All Replies
Respected Advisor
Posts: 3,249

Re: SAS Time Range

Posted in reply to riccardo88

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
Occasional Contributor
Posts: 6

Re: SAS Time Range

Posted in reply to PaigeMiller

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.  

Frequent Contributor
Posts: 107

Re: SAS Time Range

Posted in reply to riccardo88

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

%let time1=YOUR-TIME;

And then call it:

&time1 < TIME < &time2
Respected Advisor
Posts: 3,249

Re: SAS Time Range

Posted in reply to riccardo88

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
Solution
Wednesday
Super User
Posts: 6,899

Re: SAS Time Range

Posted in reply to riccardo88

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.

Occasional Contributor
Posts: 6

Re: SAS Time Range

Posted in reply to Astounding

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?

 

Respected Advisor
Posts: 3,249

Re: SAS Time Range

Posted in reply to riccardo88

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

--
Paige Miller
Occasional Contributor
Posts: 6

Re: SAS Time Range

Posted in reply to PaigeMiller

Yes, it is.

Respected Advisor
Posts: 3,249

Re: SAS Time Range

Posted in reply to riccardo88

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

 

ntime = input(time,hhmmss8.);

and then work with ntime

 

 

--
Paige Miller
Occasional Contributor
Posts: 6

Re: SAS Time Range

Posted in reply to PaigeMiller
It worked! Thank you very much!
Occasional Contributor
Posts: 6

Re: SAS Time Range

Posted in reply to Astounding
It worked! Thank you very much!!
Frequent Contributor
Posts: 107

Re: SAS Time Range

Posted in reply to riccardo88

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);
☑ This topic is solved.

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

Discussion stats
  • 12 replies
  • 86 views
  • 0 likes
  • 4 in conversation