SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
riccardo88
Calcite | Level 5

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!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

12 REPLIES 12
PaigeMiller
Diamond | Level 26

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
riccardo88
Calcite | Level 5

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.  

cau83
Pyrite | Level 9

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

%let time1=YOUR-TIME;

And then call it:

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

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
Astounding
PROC Star

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.

riccardo88
Calcite | Level 5

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?

 

PaigeMiller
Diamond | Level 26

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

--
Paige Miller
riccardo88
Calcite | Level 5

Yes, it is.

PaigeMiller
Diamond | Level 26

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

 

ntime = input(time,hhmmss8.);

and then work with ntime

 

 

--
Paige Miller
riccardo88
Calcite | Level 5
It worked! Thank you very much!
riccardo88
Calcite | Level 5
It worked! Thank you very much!!
cau83
Pyrite | Level 9

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

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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