SAS Enterprise Guide

Desktop productivity for business analysts and programmers
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
I need to make a condition where if time is <= 09:00 then 1; if time is > 09:00 and < 17:00 then 2 and if > 17:00 then 3.
1 ACCEPTED SOLUTION

Accepted Solutions
HB
Barite | Level 11 HB
Barite | Level 11

I would guess a CASE statement would get it done, but if all you are doing is setting a flag, I would be tempted to do it in the data step (and this is coming from an SQL guy).

 

 

 

data mytimes;
   input id time time.;
datalines;
1105 08:00
1106 08:30
1107 09:10
1108 10:10
1109 12:56
1110 16:59
1111 17:01
1112 23:45
;
run;

* method 1;
data mytime_flags;
	set mytimes;
	mytimeflag= 0;
	if time <= '09:00't then mytimeflag = 1;
	if time > '09:00't and time < '17:00't then mytimeflag = 2;
	if time >= '17:00't then mytimeflag = 3;
run;

*method 2;
proc sql;
	create table mytime_flags2 as 
	select id, time,
	case 
	when time <= '09:00't then 1
	when time > '09:00't and time < '17:00't then 2
	when time >= '17:00't then 3
	end as mytimeflag
	from mytimes;
quit;

 

 

View solution in original post

1 REPLY 1
HB
Barite | Level 11 HB
Barite | Level 11

I would guess a CASE statement would get it done, but if all you are doing is setting a flag, I would be tempted to do it in the data step (and this is coming from an SQL guy).

 

 

 

data mytimes;
   input id time time.;
datalines;
1105 08:00
1106 08:30
1107 09:10
1108 10:10
1109 12:56
1110 16:59
1111 17:01
1112 23:45
;
run;

* method 1;
data mytime_flags;
	set mytimes;
	mytimeflag= 0;
	if time <= '09:00't then mytimeflag = 1;
	if time > '09:00't and time < '17:00't then mytimeflag = 2;
	if time >= '17:00't then mytimeflag = 3;
run;

*method 2;
proc sql;
	create table mytime_flags2 as 
	select id, time,
	case 
	when time <= '09:00't then 1
	when time > '09:00't and time < '17:00't then 2
	when time >= '17:00't then 3
	end as mytimeflag
	from mytimes;
quit;

 

 

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 1 reply
  • 63665 views
  • 5 likes
  • 2 in conversation