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

Hello,

I would like to create a new variable T_heure (Hour slice)

These are examples of my trying. I've tried with and without quotations marks. But I keep getting errors.

if 07:30<=NHeure<=08:15 then T_Heure='07:30-08:15';
if 08:30<=NHeure<=09:15 then T_Heure='08:30-09:15';
if 09:30<=NHeure<=10:15 then T_Heure='09:30-10:15';
.
.
.
if NHeure=>16:30 then T_Heure='16:30+';

if Nheure in (07:30,08:15) then T_Heure=1;
else if Nheure in(08:30, 09:15) then T_Heure=2;
else if Nheure in(09:30, 10:15) then T_Heure=3;
else if Nheure in(10:30, 11:15) then T_Heure=4;
.
.
.
else if Nheure in (16:30) then T_Heure=10;

select (Nheure);
 when ("07:30", "07:45", "08:00", "08:15")  T_Heure=0;
  when ("08:30", "08:45", "09:00", "09:15")  T_Heure=1;
  when ("09:30", "09:45", "10:00", "10:15")  T_Heure=2;
 .
 . 
 .
when ("16:30")  T_Heure=9;
  otherwise T_Heure="";
end;

h1=1 if Nheure is (07:30 to 08:15);
h1=2 if Nheure is (08:30 to 09:15);
h1=3 if Nheure is (09:30 to 10:15);
h1=4 if Nheure is (10:30 to 11:15);
h1=5 if Nheure is (11:30 to 12:15);

Examples of errors:

1068 if 07:30<=NHeure<=08:15 then T_Heure='07:30-08:15';
-
22
76
1069 if 08:30<=NHeure<=09:15 then T_Heure='08:30-09:15';
-
22
76

ERROR 22-322: Syntax error, expecting one of the following: <, <=, =, >, >=, EQ, GE, GT, LE, LT, NE,
NG, NL, ^=, ~=.

ERROR 76-322: Syntax error, statement will be ignored.


1164 h1=1 if Nheure is (07:30 to 08:15);
--
388
76
1165 h1=2 if Nheure is (08:30 to 09:15);
--
388
76

per hour or per 2hour.

I've tried different ways with if and select fonction but I couldn't make it work. Could someone help me please. I'm really new to SAS and my data tables are too complicated to manipulate.

My variable NHeure is (in SAS time format):

07:30
07:45
08:00
08:15
08:45
09:00

.

.

.

16:30

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Post the full log and actual code really helps as it highlights where the errors are in the code.

 

For starters - the time references in SAS are wrong. 

Time values need to be in quotes and end in t, then SAS knows to interpret that value as a time.

 

if '07:30't<=NHeure<='08:15't then T_Heure='07:30-08:15';
else if '08:30't <=NHeure<= '09:15't then T_Heure='08:30-09:15';
....

That should get you started.

 

Here's a great, but longer and in depth, reference for dates and times in SAS that is highly worth reading through:
https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/...

 

Spoiler

@Didi_b wrote:

Hello,

I would like to create a new variable T_heure (Hour slice)

These are examples of my trying. I've tried with and without quotations marks. But I keep getting errors.

if 07:30<=NHeure<=08:15 then T_Heure='07:30-08:15';
if 08:30<=NHeure<=09:15 then T_Heure='08:30-09:15';
if 09:30<=NHeure<=10:15 then T_Heure='09:30-10:15';
.
.
.
if NHeure=>16:30 then T_Heure='16:30+';

if Nheure in (07:30,08:15) then T_Heure=1;
else if Nheure in(08:30, 09:15) then T_Heure=2;
else if Nheure in(09:30, 10:15) then T_Heure=3;
else if Nheure in(10:30, 11:15) then T_Heure=4;
.
.
.
else if Nheure in (16:30) then T_Heure=10;

select (Nheure);
 when ("07:30", "07:45", "08:00", "08:15")  T_Heure=0;
  when ("08:30", "08:45", "09:00", "09:15")  T_Heure=1;
  when ("09:30", "09:45", "10:00", "10:15")  T_Heure=2;
 .
 . 
 .
when ("16:30")  T_Heure=9;
  otherwise T_Heure="";
end;

h1=1 if Nheure is (07:30 to 08:15);
h1=2 if Nheure is (08:30 to 09:15);
h1=3 if Nheure is (09:30 to 10:15);
h1=4 if Nheure is (10:30 to 11:15);
h1=5 if Nheure is (11:30 to 12:15);

Examples of errors:

1068 if 07:30<=NHeure<=08:15 then T_Heure='07:30-08:15';
-
22
76
1069 if 08:30<=NHeure<=09:15 then T_Heure='08:30-09:15';
-
22
76

ERROR 22-322: Syntax error, expecting one of the following: <, <=, =, >, >=, EQ, GE, GT, LE, LT, NE,
NG, NL, ^=, ~=.

ERROR 76-322: Syntax error, statement will be ignored.


1164 h1=1 if Nheure is (07:30 to 08:15);
--
388
76
1165 h1=2 if Nheure is (08:30 to 09:15);
--
388
76

per hour or per 2hour.

I've tried different ways with if and select fonction but I couldn't make it work. Could someone help me please. I'm really new to SAS and my data tables are too complicated to manipulate.

My variable NHeure is (in SAS time format):

07:30
07:45
08:00
08:15
08:45
09:00

.

.

.

16:30


 

View solution in original post

5 REPLIES 5
Reeza
Super User

Post the full log and actual code really helps as it highlights where the errors are in the code.

 

For starters - the time references in SAS are wrong. 

Time values need to be in quotes and end in t, then SAS knows to interpret that value as a time.

 

if '07:30't<=NHeure<='08:15't then T_Heure='07:30-08:15';
else if '08:30't <=NHeure<= '09:15't then T_Heure='08:30-09:15';
....

That should get you started.

 

Here's a great, but longer and in depth, reference for dates and times in SAS that is highly worth reading through:
https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/...

 

Spoiler

@Didi_b wrote:

Hello,

I would like to create a new variable T_heure (Hour slice)

These are examples of my trying. I've tried with and without quotations marks. But I keep getting errors.

if 07:30<=NHeure<=08:15 then T_Heure='07:30-08:15';
if 08:30<=NHeure<=09:15 then T_Heure='08:30-09:15';
if 09:30<=NHeure<=10:15 then T_Heure='09:30-10:15';
.
.
.
if NHeure=>16:30 then T_Heure='16:30+';

if Nheure in (07:30,08:15) then T_Heure=1;
else if Nheure in(08:30, 09:15) then T_Heure=2;
else if Nheure in(09:30, 10:15) then T_Heure=3;
else if Nheure in(10:30, 11:15) then T_Heure=4;
.
.
.
else if Nheure in (16:30) then T_Heure=10;

select (Nheure);
 when ("07:30", "07:45", "08:00", "08:15")  T_Heure=0;
  when ("08:30", "08:45", "09:00", "09:15")  T_Heure=1;
  when ("09:30", "09:45", "10:00", "10:15")  T_Heure=2;
 .
 . 
 .
when ("16:30")  T_Heure=9;
  otherwise T_Heure="";
end;

h1=1 if Nheure is (07:30 to 08:15);
h1=2 if Nheure is (08:30 to 09:15);
h1=3 if Nheure is (09:30 to 10:15);
h1=4 if Nheure is (10:30 to 11:15);
h1=5 if Nheure is (11:30 to 12:15);

Examples of errors:

1068 if 07:30<=NHeure<=08:15 then T_Heure='07:30-08:15';
-
22
76
1069 if 08:30<=NHeure<=09:15 then T_Heure='08:30-09:15';
-
22
76

ERROR 22-322: Syntax error, expecting one of the following: <, <=, =, >, >=, EQ, GE, GT, LE, LT, NE,
NG, NL, ^=, ~=.

ERROR 76-322: Syntax error, statement will be ignored.


1164 h1=1 if Nheure is (07:30 to 08:15);
--
388
76
1165 h1=2 if Nheure is (08:30 to 09:15);
--
388
76

per hour or per 2hour.

I've tried different ways with if and select fonction but I couldn't make it work. Could someone help me please. I'm really new to SAS and my data tables are too complicated to manipulate.

My variable NHeure is (in SAS time format):

07:30
07:45
08:00
08:15
08:45
09:00

.

.

.

16:30


 

PeterClemmensen
Tourmaline | Level 20

Is NHeure an actual SAS Time variable?

Didi_b
Obsidian | Level 7

Hi yes, First converted to a SAS time Value with this 

NHeure=input(translate(Heure,':','H'),time15.);
format NHeure tod5.;
CarmineVerrell
SAS Employee

Here is an example i created to help you out .

 

Data testingT;
/*To reference Dates in code use this format "date"d (followed by a d ) */
/*To reference Times in code use this format "Time"t (followed by a t)*/
/*To reference DateTime in code use this format "DateTime"dt (followed b a dt)*/
endt='08:15't;
starttime="07:15"t;
/*use the intck to compare date values year , month and even hour;*/
hdiff=intck("hour",starttime,endt,"C");
run;

Didi_b
Obsidian | Level 7

Hi, thank you so much for the tips.

 

How could accept multiples propositions that work as a solution?

When I accept one, I can't no longer accept other solution  as a solution too.

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
  • 5 replies
  • 531 views
  • 5 likes
  • 4 in conversation