BookmarkSubscribeRSS Feed
manthan
Fluorite | Level 6

Hello, 

 

I am currently working on a data file called work.import. 

 

I need some help. I have a column that I created from timestamp called hOUR_PART 

It basically has different numbers in it reflecting time hours.. e.g. 12, 10, 22 etc. 

I wanted to have values in this column to reflect the following 

any numbers between 0-10 should be AM or 1 

Any numbers between 10-12 (should be morning or 2) 

Any numbers between 12-0 (should be evening or 3) 

 

So Basically I want 1, 2 or 3 to appear instead of the different hours in the column. How would i achieve this? 

 

2 REPLIES 2
Reeza
Super User

Just as an FYI in SAS this is not called labels (I believe that's the SPSS terminology?)

This would be recoding the data or applying a format. Labels apply to variable names

 

Note that your intervals overlap in your description so you may need to modify the ranges in your code. 

A basic way of doing this is IF/THEN statements, see below:

 

Assumes your input data set is HAVE, the output data set is WANT and that your variable is named HOUR_PART.

EDIT: for some reason I assumed you had time values but you have hours already. Modified code to correct for this.

 

data want;
set have;

if hour_part in (0:9) then day_code = 1;
else if hour_part in (10:12) then day_code=2;
else if hour_part in (13:23) then day_code = 3;

run;

@manthan wrote:

Hello, 

 

I am currently working on a data file called work.import. 

 

I need some help. I have a column that I created from timestamp called hOUR_PART 

It basically has different numbers in it reflecting time hours.. e.g. 12, 10, 22 etc. 

I wanted to have values in this column to reflect the following 

any numbers between 0-10 should be AM or 1 

Any numbers between 10-12 (should be morning or 2) 

Any numbers between 12-0 (should be evening or 3) 

 

So Basically I want 1, 2 or 3 to appear instead of the different hours in the column. How would i achieve this? 

 


 

PaigeMiller
Diamond | Level 26

@manthan wrote:

Hello, 

 

I am currently working on a data file called work.import. 

 

I need some help. I have a column that I created from timestamp called hOUR_PART 

It basically has different numbers in it reflecting time hours.. e.g. 12, 10, 22 etc. 

I wanted to have values in this column to reflect the following 

any numbers between 0-10 should be AM or 1 

Any numbers between 10-12 (should be morning or 2) 

Any numbers between 12-0 (should be evening or 3) 

 

So Basically I want 1, 2 or 3 to appear instead of the different hours in the column. How would i achieve this? 

 


You want custom formats.

 

proc format;
    value hour 0-10 = '1' 11-12 = '2' 13-23='3';
run;
data want;
    set have;
    format hour_part hour.;
run;

As pointed out by @Reeza your time intervals overlap, I have removed the overlap. If you don't like the way I removed the overlap, it's easy for you to modify.

--
Paige Miller

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 408 views
  • 0 likes
  • 3 in conversation