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!
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.
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.
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.
For "creating a variable to use in the statement" create a macro variable.
%let time1=YOUR-TIME;
And then call it:
&time1 < TIME < &time2
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.
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.
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?
Seems as if your variable TIME is character rather than numeric. Is that correct?
Yes, it is.
Then you need to convert it to numeric, for example:
ntime = input(time,hhmmss8.);
and then work with ntime
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);
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.