BookmarkSubscribeRSS Feed
eynerg
Calcite | Level 5

Hello all,

I have a data set which has start and stop times for hourly rates. Each subject has a rate for every hour, but since it was input as only when the rate changes and the time of change is variable, it is hard to standardize. I want to convert this into what the rate is at each hour so it is easier to compare the subjects and then pull out certain time blocks. I am having trouble doing this, I think bc I have never worked with time variables in SAS before.

 

What I have:

start_time_1

stop_time_1

rate_1

start_time_2

stop_time_2

rate_2

...etc up to 12

 

What I want:

rate000

rate100

rate200

...etc for each hour of the day

 

I started trying to figure out how to make it choose what rate to pick for each hour, but it stopped working at 200:

 

Data work.rates;

 set work.rates;
rate000 = rate_1;
run;

data WORK.rates;
set WORK.rates;
if start_time_2 > 1 then rate100 = rate_1; 
if start_time_2 <= 1 then rate100 = rate_2;

 

data WORK.rates;
set WORK.rates;
if start_time_2 > 2 then rate200 = rate_1; 

if start_time_2 <= 2 then rate200 = rate_2;

etc.

 

Thank you in advance! 

2 REPLIES 2
Reeza
Super User

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

 

I highly suggest you read that first and also look into time functions and determine if you have SAS ETS. If so some of the procs there can help you with your analysis.

 


@eynerg wrote:

Hello all,

I have a data set which has start and stop times for hourly rates. Each subject has a rate for every hour, but since it was input as only when the rate changes and the time of change is variable, it is hard to standardize. I want to convert this into what the rate is at each hour so it is easier to compare the subjects and then pull out certain time blocks. I am having trouble doing this, I think bc I have never worked with time variables in SAS before.

 

What I have:

start_time_1

stop_time_1

rate_1

start_time_2

stop_time_2

rate_2

...etc up to 12

 

What I want:

rate000

rate100

rate200

...etc for each hour of the day

 

I started trying to figure out how to make it choose what rate to pick for each hour, but it stopped working at 200:

 

Data work.rates;

 set work.rates;
rate000 = rate_1;
run;

data WORK.rates;
set WORK.rates;
if start_time_2 > 1 then rate100 = rate_1; 
if start_time_2 <= 1 then rate100 = rate_2;

 

data WORK.rates;
set WORK.rates;
if start_time_2 > 2 then rate200 = rate_1; 

if start_time_2 <= 2 then rate200 = rate_2;

etc.

 

Thank you in advance! 


 

Kurt_Bremser
Super User

Welcome to the forum!

 

Please give us an idea what your data looks like in terms of contents of these columns. Ideally, you post a data step with datalines, so we can quickly and accurately reproduce your example dataset in our environment (writing such a data step is a very useful SAS skill in itself, so you should acquire it if you're not yet familiar with it).

Use the "little running man" button to open a subwindow where you can paste your code.

 

It is not a good idea to overwrite a dataset in a data step where it is used as input. If something bad happens, your data is lost.

And you don't need those separate data steps, you can do all those assignments in a single one.

 


@eynerg wrote:

Hello all,

I have a data set which has start and stop times for hourly rates. Each subject has a rate for every hour, but since it was input as only when the rate changes and the time of change is variable, it is hard to standardize. I want to convert this into what the rate is at each hour so it is easier to compare the subjects and then pull out certain time blocks. I am having trouble doing this, I think bc I have never worked with time variables in SAS before.

 

What I have:

start_time_1

stop_time_1

rate_1

start_time_2

stop_time_2

rate_2

...etc up to 12

 

What I want:

rate000

rate100

rate200

...etc for each hour of the day

 

I started trying to figure out how to make it choose what rate to pick for each hour, but it stopped working at 200:

 

Data work.rates;

 set work.rates;
rate000 = rate_1;
run;

data WORK.rates;
set WORK.rates;
if start_time_2 > 1 then rate100 = rate_1; 
if start_time_2 <= 1 then rate100 = rate_2;

 

data WORK.rates;
set WORK.rates;
if start_time_2 > 2 then rate200 = rate_1; 

if start_time_2 <= 2 then rate200 = rate_2;

etc.

 

Thank you in advance! 


 

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!

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
  • 2 replies
  • 620 views
  • 0 likes
  • 3 in conversation