BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Tbaso
Calcite | Level 5

 

Im using SAS 3.7 (Enterprise Edition)

The time a dataset I am using has the time in military time with a format of 00:00:00.000

I am trying to format the Time in my data set into sections such as 6am-12pm = morning.

I run into this error when I run my code, 

 

 
71         Data TimeofDay;
 72             set Airplane ;
 73         
 74             If  00:00:00.000 <= Time <= 06:00:00.000 then Time= 'Night';
                      _
                      388
                      76
 75             If  06:00:00.000 <= Time <= 12:00:00.000 then Time= 'Morning';
                      _
                      388
                      76
 76             If  12:00:00.000 <= Time <= 18:00:00.000 then Time= 'Afternoon';
                      _
                      388
                      76
 77             If  18:00:00.000 <= Time <= 24:00:00.000 then Time= 'Evening';
                      _
                      388
                      76
 ERROR 388-185: Expecting an arithmetic operator.
 
 ERROR 76-322: Syntax error, statement will be ignored.
 
 
Here is my code:
 
 
 
Data TimeofDay;
    set Airplane ;
    If  00:00:00.000 <= Time <= 06:00:00.000 then Time= 'Night'; 
    If  06:00:00.000 <= Time <= 12:00:00.000 then Time= 'Morning'; 
    If  12:00:00.000 <= Time <= 18:00:00.000 then Time= 'Afternoon'; 
    If  18:00:00.000 <= Time <= 24:00:00.000 then Time= 'Evening'; 
    Run;
 
 
 
My question is how do I format the time in the if then statement in order to not run into this error?
I've been reading solutions from other people but nothing has worked for me. 
 
I would be grateful for any response.
1 ACCEPTED SOLUTION

Accepted Solutions
LaurieF
Barite | Level 11

SAS is very simply typed. It knows about character and numeric fields. The simple replacement for your first statement would be:

 

 

 If '00:00:00.000't <= Time <= '06:00:00.000't then Time= 'Night';

 

But that won't do what you want. Note, for example that if the time is exactly 6am, your next statement will override it with Morning. But that isn't right either! Because you've already implicitly defined Time as being 5 characters long (Night), it'll be Morni. But that won't work either, because you've already defined Time as numeric. So you need a new variable.

 

Here's how I would recommend you re-write it.

Data TimeofDay;
 set Airplane;
 length Timename $ 9;
 select;
    when('00:00:00't <= Time < '6:00:00't) Timename= 'Night';
    when('06:00:00't <= Time < '12:00:00't) Timename= 'Morning';
    when('12:00:00't <= Time < '18:00:00't) Timename= 'Afternoon';
    otherwise Timename = 'Evening';
    end;
run;

View solution in original post

2 REPLIES 2
LaurieF
Barite | Level 11

SAS is very simply typed. It knows about character and numeric fields. The simple replacement for your first statement would be:

 

 

 If '00:00:00.000't <= Time <= '06:00:00.000't then Time= 'Night';

 

But that won't do what you want. Note, for example that if the time is exactly 6am, your next statement will override it with Morning. But that isn't right either! Because you've already implicitly defined Time as being 5 characters long (Night), it'll be Morni. But that won't work either, because you've already defined Time as numeric. So you need a new variable.

 

Here's how I would recommend you re-write it.

Data TimeofDay;
 set Airplane;
 length Timename $ 9;
 select;
    when('00:00:00't <= Time < '6:00:00't) Timename= 'Night';
    when('06:00:00't <= Time < '12:00:00't) Timename= 'Morning';
    when('12:00:00't <= Time < '18:00:00't) Timename= 'Afternoon';
    otherwise Timename = 'Evening';
    end;
run;
Tbaso
Calcite | Level 5
Thank you so much for your response! Great explanation.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 995 views
  • 1 like
  • 2 in conversation