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
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/...
@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
76ERROR 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
76per 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
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/...
@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
76ERROR 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
76per 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
Is NHeure an actual SAS Time variable?
Hi yes, First converted to a SAS time Value with this
NHeure=input(translate(Heure,':','H'),time15.);
format NHeure tod5.;
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;
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.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.