BookmarkSubscribeRSS Feed
hwangnyc
Quartz | Level 8

Hello everyone,

 

I'm not having much luck re-coding my time variable. What I would like is:

 

7AM-3PM = 'AM'

3PM-11PM = 'PM'

11PM-7AM = 'OVERNIGHT'

 

I have data that looks like this 

HAVE 

IDTIME
1020280274:00:00 AM
1020280326:45:00 AM
1020280883:15:00 PM
1020280899:00:00 AM
1020280491:30:00 PM

 

WANT

IDTIMESHIFT
1020280274:00:00 AMOvernight
1020280326:45:00 AMOvernight
1020280883:15:00 PMPM
1020280899:00:00 AMAM
1020280491:30:00 PMAM

 

Any help would be greatly appreciated!

 

Thank you! 

7 REPLIES 7
PeterClemmensen
Tourmaline | Level 20

Is time an actual SAS Time valie i.e. numeric?

hwangnyc
Quartz | Level 8

Good question, it's numeric with the timeampm format. 

PeterClemmensen
Tourmaline | Level 20

Do something like this

 

data have;
input ID TIME : time9.;
format TIME time9.;
datalines;
102028027 4:00:00AM
102028032 6:45:00AM
102028088 3:15:00PM
102028089 9:00:00AM
102028049 1:30:00PM
;

proc format;
   value ampm 
      '07:00't <- '15:00't = 'Overnight'
      '15:00't <- '23:00't = 'PM'
      other                = 'AM';
run;

data want;
   set have;
   shift=put(TIME, ampm.);
run;

 

Result:

 

ID          TIME       shift 
102028027   4:00:00    AM 
102028032   6:45:00    AM 
102028088   15:15:00   PM 
102028089   9:00:00    Overnight 
102028049   13:30:00   Overnight 

 

Krueger
Pyrite | Level 9

 

data have ;  
infile cards dsd dlm=","; 
  input ID $ TIME : TIME11.;
  format TIME TIMEAMPM11.;
  cards ;                       
102028027, 4:00:00 AM,
102028032, 6:45:00 AM,
102028088, 3:15:00 PM,
102028089, 9:00:00 AM,
102028049, 1:30:00 PM
;
run;

data want;
set have;
IF '15:00:00't <= TIME <= '22:59:59't THEN SHIFT = 'PM ';
IF '07:00:00't <= TIME <= '14:59:59't THEN SHIFT = 'AM ';
IF '23:00:00't <= TIME <= '23:59:59't THEN SHIFT = 'Overnight';
IF '00:00:00't <= TIME <= '06:59:59't THEN SHIFT = 'Overnight';
RUN;

 

 

Here's what I came up with. I wasn't able to come up with an alternative for the overnight time maybe someone can expand on that?

 

 

Reeza
Super User

Do you have missing values that need to be considered? 

 

data want;
set have;
IF '15:00:00't <= TIME <= '22:59:59't THEN SHIFT = 'PM ';
ELSE IF '07:00:00't <= TIME <= '14:59:59't THEN SHIFT = 'AM ';
ELSE IF not missing(time) then SHIFT = 'Overnight';

RUN;

@Krueger wrote:

 

data have ;  
infile cards dsd dlm=","; 
  input ID $ TIME : TIME11.;
  format TIME TIMEAMPM11.;
  cards ;                       
102028027, 4:00:00 AM,
102028032, 6:45:00 AM,
102028088, 3:15:00 PM,
102028089, 9:00:00 AM,
102028049, 1:30:00 PM
;
run;

data want;
set have;
IF '15:00:00't <= TIME <= '22:59:59't THEN SHIFT = 'PM ';
IF '07:00:00't <= TIME <= '14:59:59't THEN SHIFT = 'AM ';
IF '23:00:00't <= TIME <= '23:59:59't THEN SHIFT = 'Overnight';
IF '00:00:00't <= TIME <= '06:59:59't THEN SHIFT = 'Overnight';
RUN;

 

 

Here's what I came up with. I wasn't able to come up with an alternative for the overnight time maybe someone can expand on that?

 

 


 

Krueger
Pyrite | Level 9

I wasn't chaining ELSE IF which was my problem, instead I was trying...

 

IF THEN 1...

IF THEN 2...

ELSE 3...

 

and it was giving me 1 OR 3 not 1, 2 OR 3.

ScottBass
Rhodochrosite | Level 12
proc format;
   value shift
      '00:00:00't -< '07:00:00't = 'OVERNIGHT'
      '07:00:00't -< '15:00:00't = 'AM'
      '15:00:00't -< '23:00:00't = 'PM'
      '23:00:00't -< '23:59:59.9999999't = 'OVERNIGHT' /* only needed if you have fractional time */
   ;
run;

data have;
   do time='00:00:00't to '23:59:59't /* by (60*60) */ ;
      shift=put(time,shift.);
      output;
   end;

   * edge case, not needed if you don't have fractional time ;
   time='23:59:59.999't;
   shift=put(time,shift.);
   output;
   format time time15.6;
run;

proc sort data=have;
   by shift time;
run;

data review;
   set have;
   by shift time;
   if first.shift or last.shift or time in ('23:00:00't,'00:00:00't);
run;


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 682 views
  • 3 likes
  • 5 in conversation